package com.sun.solarisinstall.panels;

import java.awt.*;
import java.io.*;
import java.util.*;
import com.sun.wizards.*;
import com.sun.wizards.core.*;
import com.sun.wizards.nodes.*;
import com.sun.wizards.awt.*;
import com.sun.solarisinstall.*;
import com.sun.solarisinstall.panels.*;
import com.sun.launcher.*;
import com.sun.launcher.core.*;

/*
 *  This panel prompts user for the network file system path
 */
public class ScanDirPanel extends ParentPanel 
{
  private transient Panel mainPanel = null;
  private transient Panel textPanel = null;
  private transient Panel entryFlowPanel = null;
  private transient Panel entryPanel = null;
  private transient FlowLabel CDprompt = null;
  private String CDtext = null;
  private boolean cliSkip = false;

 /**
   * The text field used for entering the media path
   */
  private TextField mediaPathField = null;


  public ScanDirPanel()
  {
  }

  public ScanDirPanel(WizardState wizardState, String name)
  {
    super(wizardState, name);
  }
    
  public ScanDirPanel(String name,Route route,WizardTreeManager wizardManager) 
  {
    super(name, route, wizardManager);
  }

  /**
   * This method creates the user interface.
   */
  public void createUI()
  {
    super.createUI();
    setLayout(new BorderLayout());

    WizardTreeManager wizardManager = getTreeManager();

    /*
     * Add the header.
     */
    String heading = MsgCat.getString("com.sun.launcher.Launcher", 
                                       LauncherResources.MSG_HDR_SPECIFY_PATH);
    add(new HeaderPanel(heading, null, 38), "North");
   
    add(mainPanel = new Panel(new BorderLayout()));

    textPanel = new Panel(new VerticalLayout());
    mainPanel.add(textPanel, "Center");

  }

  public void consoleInteraction()
  {
    super.consoleInteraction();
    cliSkip = false;
    TTYDisplay.showNewline();
    TTYDisplay.showText(CDtext);
    TTYDisplay.showNewline();

    TTYDisplay.showText(MsgCat.getString("com.sun.launcher.Launcher", 
                                          LauncherResources.MSG_PATH_EXAMPLE));
    TTYDisplay.showNewline();

    String skipVal = "";

    String res = TTYDisplay.queryValue("", "",  "");
    if (res.equalsIgnoreCase(skipVal)) {
      handleSkip();
      return;
    }

    mediaPathField.setText(res);
  }


  public void handleSkip()
  {
   WizardTreeManager wizardManager = getTreeManager();
   cliSkip = true;
  }

  public void beginDisplay()
  {
    super.beginDisplay();

    WizardTreeManager wizardManager = getTreeManager();

    int row = 1;
    if (CDprompt != null) textPanel.remove(CDprompt);

    String Name = MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                SolarisInstallResources.MSG_ADD_PRODUCTS);

    Object[] nameArg = { Name };
    CDtext =  MsgCat.getString("com.sun.launcher.Launcher", 
                LauncherResources.MSG_PLEASE_SPECIFY_PATH, nameArg); 

    CDprompt = new FlowLabel(CDtext);

    textPanel.add(CDprompt, new Integer(row++).toString());

    if (entryFlowPanel != null) textPanel.remove(entryFlowPanel);
    entryFlowPanel = new Panel(new FlowLayout());
    entryPanel = new Panel(new VerticalLayout());
    mediaPathField = new TextField("", 15);
    FlowLabel pathLabel = new FlowLabel("<bold>" +
                          MsgCat.getString("com.sun.launcher.Launcher",
                          LauncherResources.MSG_PATH_EXAMPLE));
    entryPanel.add(pathLabel, "1");
    entryPanel.add(mediaPathField, "2");
    entryFlowPanel.add(entryPanel, FlowLayout.LEFT);
    textPanel.add(entryFlowPanel, new Integer(row++).toString());

   }

   public int getButtonMask()
   {
     return(BACK_BUTTON | NEXT_BUTTON | EXIT_BUTTON);
   }
    

  /**
   * Returns true if this panel is finished displaying.
   * This method can be used to verify user input.
   *
   * @return    true if the display of this panel is
   *            complete; false otherwise.
   */
  public boolean isDisplayComplete()
  {
    WizardTreeManager wizardManager = getTreeManager();
    Route launchobjectRoute = getRoute().getChildServerRoute("launchObject");
    boolean status = false;

    if (cliSkip) {
      return (true);
    } 

    String mediapath = mediaPathField.getText().trim();

    /*
     * try to mount the server:/path or /net/path
     */
    Integer serverMounted = 
           (Integer)wizardManager.callServerObjectMethod(
                         launchobjectRoute.copy(),
                         "mountPathFromUser",
          new String[] {"java.lang.String.class"},
          new Object[] { mediapath });

    int mountStatus = serverMounted.intValue();
    if ( mountStatus != LauncherResources.MOUNT_SUCCESS ) {
      /*
       * there was a problem, give a popup
       */
       String message =
          MsgCat.getString("com.sun.launcher.Launcher", mountStatus);

        String okStr =
          MsgCat.getString("com.sun.launcher.Launcher",
                           LauncherResources.MSG_OK);

        String hdrStr =
          MsgCat.getString("com.sun.launcher.Launcher",
                           LauncherResources.MSG_QUERY_HDR_UNABLE_TO_MOUNT);

        wizardManager.displayQuery(this, hdrStr,  message,
                                   new String[] {okStr},
                                   new String[] {null});
      status = false;

    } else {
      /*
       * The directory has been mounted.  Check if it's Web Start Ready.
       */
      String mountLocation =
             (String)wizardManager.callServerObjectMethod(getRoute(),
              "getData",
              new String[] {"java.lang.String.class"},
              new Object[] {LaunchConstants.launchLocation});

      Boolean wsrFilesExist =
          (Boolean) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "doesFileExist",
                  new String[] {"java.lang.String.class"},
                  new Object[] {mountLocation +
                                ProfileConstants.wsrDir});


      if (wsrFilesExist.booleanValue()) {

        Integer copyStatus =
            (Integer) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "importWSR",
                  new String[] {"java.lang.String.class"},
                  new Object[] {mountLocation});

        status = true;

        String cdNameDir = 
            (String) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "readFileLine",
                  new String[] {"java.lang.String.class"},
                  new Object[] {mountLocation +
                                ProfileConstants.wsrCDName});

        String cdsDirPath = ProfileConstants.CDsBaseDir + File.separator 
                              + cdNameDir;

        wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "writeStringToFile",
                  new String[] {"java.lang.String.class",
                                "java.lang.String.class"},
                  new Object[] {cdsDirPath + ProfileConstants.addPath,
                                 mediapath});

        wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "writeStringToFile",
                  new String[] {"java.lang.String.class",
                                "java.lang.String.class"},
                  new Object[] {cdsDirPath + ProfileConstants.addSource,
                                 String.valueOf(CDNode.FILE_SYSTEM)});

        /*
         * Reset product source back to NONE for next loop
         */
        wizardManager.callServerObjectMethod(
                getRoute(),
                "setData",
                new String[] {"com.sun.wizards.core.Route.class",
                              "java.lang.String.class",
                              "java.lang.Object.class"},
                new Object[] {null, "prodSource",
                        new Integer(AddProductsPanel.PROD_SOURCE_NONE)});


      } else {
        /*
         * Pop up the not WSR notice box
         */
        String title = MsgCat.getString("com.sun.wizards.Wizard",
                                       WizardResources.MSG_NOTICE);
        wizardManager.displayQuery( this, title,
            MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                             SolarisInstallResources.MSG_WSR_FILE_NOTFOUND),
            new String[] {MsgCat.getString(
                            "com.sun.solarisinstall.SolarisInstall",
                            SolarisInstallResources.MSG_OK)},
            new String[] {null});
        status = false;
      }
    }

    return(status);
  }


  public void abortDisplay()
  {
  }



   public void addRuntimeResources(Vector resourceVector)
   {
     super.addRuntimeResources(resourceVector);
     resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.ScanDirPanel"});
     resourceVector.addElement(new String[] {null, "com.sun.launcher.LauncherResources"});
   }
}
