package com.sun.solarisinstall.panels;

import com.sun.solarisinstall.SolarisInstallResources;
import com.sun.wizards.core.*;
import com.sun.wizards.awt.*;
import com.sun.install.products.*;
import java.awt.*;
import java.io.*;
import java.util.*;

/**
 * The FileSysLayOutPanel allows the user to set and browse
 * a set of profile settings
 */
public class FileSysLayOutPanel extends WizardLeaf
{
  /**
   * The panel that will display the lists of available and selected disks
   */
  private Panel infoPanel = null;
  private Panel textPanel = null;
  private Checkbox root, export, exportRoot, exportSwap;
  private Checkbox opt, usr, usrOW, var, swap;
  private Profile profile = null;


  /**
   * Creates a FileSysLayOutPanel with no name.
   */
  public FileSysLayOutPanel()
    {
    }

  /**
   * Creates a FileSysLayOutPanel with the specified name,
   * and contains profile entries indexed by the specified key list.
   *
   * @param wizardState	The buildtime WizardState.
   * @param name		The name for this FileSysLayOutPanel.
   */
  public FileSysLayOutPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }

  /**
   * Creates a FileSysLayOutPanel with the specified name, the specified
   * route and wizard manager.
   *
   * @param name	The name for this FileSysLayOutPanel.
   * @param route	The route to the appropriate server side object.
   * @param wizardManager	The wizardManager responsible for this component.
   */
  public FileSysLayOutPanel(String name, Route route, WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }

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

      setLayout(new BorderLayout());

      /*
       * Add the header.
       */
      String heading = MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                                       124);
      add(new HeaderPanel(heading, null), "North");

      /*
       * Add the text.
       */
      textPanel = new Panel(new VerticalLayout());
      Label textLabel = new Label(MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 38));
      textLabel.setFont(new Font(getFont().getName(),
          Font.BOLD, getFont().getSize()));
      textPanel.add(textLabel, "0");

      add(textPanel, "Center");
    }

  public void beginDisplay()
    {
      if (infoPanel != null) {
        textPanel.remove(infoPanel);
        infoPanel = null;
      }
      super.beginDisplay();

      WizardTreeManager wizardManager = getTreeManager();

/*
// if this is uncommented, change the InstallConstants.productName to
// something else
      String prodName =
            (String)wizardManager.callServerObjectMethod(getRoute(),
                "getData",
                 new String[]{"com.sun.wizards.core.Route.class",
                              "java.lang.String.class"},
                new Object[] {null, ProfileConstants.mediaName});
*/

      profile =
            (Profile)wizardManager.callServerObjectMethod(getRoute(),
                "getData",
                 new String[]{"com.sun.wizards.core.Route.class",
                              "java.lang.String.class"},
                new Object[] {null, "SolarisInstallProfile"});
      ProfileUtil.setProfile(profile);

      //ProfileUtil.loadInfo(prodName);
      //Profile profile = ProfileUtil.getProfile();

      infoPanel = new Panel(new ColumnLayout());

      Panel choicePanel = new Panel();
      choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
      choicePanel.add(new Label(""));
      root = new Checkbox("  /", null, true);
      root.setEnabled(false);
      choicePanel.add(root);
      infoPanel.add(choicePanel);
  
      Vector pinfo = profile.getPartitionList(true);

/* *************
      boolean server = profile.isServer();
      if (server) {
        choicePanel = new Panel();
        choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
        choicePanel.add(new Label(""));
        export = new Checkbox("  /export",
          null, selected("/export", pinfo));
        choicePanel.add(export);
        infoPanel.add(choicePanel);

        choicePanel = new Panel();
        choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
        choicePanel.add(new Label(""));
        exportRoot = new Checkbox("  /export/root",
          null, selected("/export/root", pinfo));
        choicePanel.add(exportRoot);
        infoPanel.add(choicePanel);

        choicePanel = new Panel();
        choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
        choicePanel.add(new Label(""));
        exportSwap = new Checkbox("  /export/swap",
          null, selected("/export/swap", pinfo));
        choicePanel.add(exportSwap);
        infoPanel.add(choicePanel);
      } else {
******* */
        export = new Checkbox("", null, false);
        exportRoot = new Checkbox("", null, false);
        exportSwap = new Checkbox("", null, false);
      /* } */

      choicePanel = new Panel();
      choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
      choicePanel.add(new Label(""));
      opt = new Checkbox("  /opt", null,
        selected("/opt", pinfo));
      choicePanel.add(opt);
      infoPanel.add(choicePanel);

      choicePanel = new Panel();
      choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
      choicePanel.add(new Label(""));
      usr = new Checkbox("  /usr", null,
        selected("/usr", pinfo));
      choicePanel.add(usr);
      infoPanel.add(choicePanel);

      choicePanel = new Panel();
      choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
      choicePanel.add(new Label(""));
      usrOW = new Checkbox("  /usr/openwin",
        null, selected("/usr/openwin", pinfo));
      choicePanel.add(usrOW);
      infoPanel.add(choicePanel);

      choicePanel = new Panel();
      choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
      choicePanel.add(new Label(""));
      var = new Checkbox("  /var", null,
        selected("/var", pinfo));
      choicePanel.add(var);
      infoPanel.add(choicePanel);

      choicePanel = new Panel();
      choicePanel.setLayout(new RowLayout(LAYOUT_ALIGNMENT.EXPAND));
      choicePanel.add(new Label(""));
      swap = new Checkbox("  swap", null,
        selected("swap", pinfo));
      choicePanel.add(swap);
      infoPanel.add(choicePanel);

      textPanel.add(infoPanel, "30");

    }

  /**
   * Returns true if this FileSysLayOutPanel is finished displaying.
   * This method can be used to verify user input.
   *
   * @return	true if the display of this FileSysLayOutPanel is
   *		complete; false otherwise.
   */
  public boolean isDisplayComplete()
    {
      return fixFSList();
/*
      if (fixFSList(!ProfileUtil.fsState())) {
        if ((profile.getPartitionList(true).size() > 7) &&
          (profile.getUsediskVector().size() == 1)) {
          // not enough slices on the disk
          // display warning dialog
        } else {
          PFInstall pf = profile.testProfile();
          if (!pf.successful()) {
            // Need to pop up dialogs
            System.err.println("Pfinstall failed");
          } else {
            profile.updatePartitions(pf);
          }
        }
      }
      return(true);
*/
    }

  /**
   * Reset this component, given the specified information.
   * This method may be called when a user backtracks in
   * the client wizard tree and changes a fundamental setting.
   * The panel that enabled the modification might traverse
   * subsequent panels and call this method resynchronize
   * all panels with the user's choice.
   *
   * When this method is called, the user selected "Default" installation.
   * This panel must reset its install directory information.
   *
   * @param Object	The information required to perform a reset.
   */
  public void reset(Object info)
    {
    }

  /**
   * Serialize this profileEntryPanel to the specified output
   * stream.  This method does not save its children.
   *
   * @see	com.sun.wizards.core.WizardComposite.serializeChildren
   *
   * @param out	The output stream to which this WizardComponent will be
   *		serialized.
   */
  protected void serialize(DataOutput out)
    {
      super.serialize(out);
    }

  /**
   * Deserialize this WizardComponent from the specified input
   * stream.  This method does not deserialize its children.
   *
   * @see	com.sun.wizards.core.WizardComposite.deserializeChildren
   *
   * @param in	The input stream from which this WizardComponent will be
   *		deserialized.
   */
  protected void deserialize(DataInput in)
    {
      super.deserialize(in);
    }

  /**
   * Returns information suggesting which buttons should be
   * displayed for this panel.
   *
   * The FileSysLayOutPanel displays all the navigation
   * buttons.
   *
   * @return	A button mask identifying valid navigation buttons.
   */
  public int getButtonMask()
    {
      return(BACK_BUTTON | NEXT_BUTTON | 
	EXIT_BUTTON );
    }

  // Checks if the filesystem fs exists in the vector plist (a partition
  // info vector
  //
  private boolean selected(String fs, Vector plist) {
    boolean found = false;
    for (int i=0; (i < plist.size()) && (!found); i++) {
      PartitionInfo pi = (PartitionInfo) plist.elementAt(i);
      if (pi.name.equals(fs)) {
        found = true;
      }
    }

    return found;
  }

  // fixFSList
  //
  // Change is whether we should recreate the FS list even if they are
  // not changed.  This is useful if the disklist has changed and we
  // must rerun pfinstall after setting slices on filesystems back to
  // "any".  If change is true, fixFSList will always return true.
  //
  protected boolean fixFSList() {
    int numfs = 0;

    WizardTreeManager wizardManager = getTreeManager();

    Vector prodList = ProfileUtil.getProductList();
    Vector installedList = new Vector();
    for (int i=0; i < prodList.size(); i++) {
      SolarisProduct p = (SolarisProduct) prodList.elementAt(i);
      if (p.orig) {
        installedList.addElement(p);
      }
    }
    SolarisFileSystem fsinfo = new SolarisFileSystem(installedList,
      profile.getPartitionList(true));
    int rootsize = 10;
    int swapsize = 300;
    ////if (profile.isServer()) {
      ////rootsize = profile.getClientRoot() * profile.getNumClients() * 1024;
      ////swapsize = profile.getClientSwap() * profile.getNumClients() * 1024;
      ////int numarchs = profile.getNumSupportedArch();
      ////fsinfo.updateExport(rootsize, swapsize, numarchs);
    ////}

    if (root.getState()) {
      if (profile.findFilesys("/") == null) {
        int size = fsinfo.getSize("/");
        String rootslice = ProfileUtil.getDiskInfo().getRootDisk() + "s0";
        if (size > 0)
          profile.file.addFilesys("filesys\t" + rootslice + "\t" + size + "\t/");
        else
          profile.file.addFilesys("filesys\t" + rootslice + "\t" + rootsize + "\t/");
      }
      ++numfs;
    } else {
      if (profile.findFilesys("/") != null)
        profile.file.delFilesys("/");
    }

/* ********
    if (profile.isServer()) {
      if (export.getState()) {
        if (profile.findFilesys("/export") == null) {
          String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
            profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
          int size = fsinfo.getSize("/export");
          if (size > 0)
            profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/export");
          else
            profile.file.addFilesys("filesys\t" + slice + "\t10\t/export");
        }
        ++numfs;
      } else {
        if (profile.findFilesys("/export") != null)
          profile.file.delFilesys("/export");
      }

      if (exportRoot.getState()) {
        if (profile.findFilesys("/export/root") == null) {
          String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
            profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
          int size = fsinfo.getSize("/export/root");
          if (size > 0)
            profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/export/root");
          else
            profile.file.addFilesys("filesys\t" + slice + "\t10\t/export/root");
        }
        ++numfs;
      } else {
        if (profile.findFilesys("/export/root") != null)
          profile.file.delFilesys("/export/root");
      }

      if (exportSwap.getState()) {
        if (profile.findFilesys("/export/swap") == null) {
          String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
            profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
          int size = fsinfo.getSize("/export/swap");
          if (size > 0)
            profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/export/swap");
          else
            profile.file.addFilesys("filesys\t" + slice + "\t10\t/export/swap");
        }
        ++numfs;
      } else {
        if (profile.findFilesys("/export/swap") != null)
          profile.file.delFilesys("/export/swap");
      }
    }
******* */

    if (opt.getState()) {
      if (profile.findFilesys("/opt") == null) {
        String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
          profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
        int size = fsinfo.getSize("/opt");
        if (size > 0)
          profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/opt");
        else
          profile.file.addFilesys("filesys\t" + slice + "\t10\t/opt");
      }
      ++numfs;
    } else {
      if (profile.findFilesys("/opt") != null)
        profile.file.delFilesys("/opt");
    }

    if (usr.getState()) {
      if (profile.findFilesys("/usr") == null) {
        String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
          profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
        int size = fsinfo.getSize("/usr");
        if (size > 0)
          profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/usr");
        else
          profile.file.addFilesys("filesys\t" + slice + "\t10\t/usr");
      }
      ++numfs;
    } else {
      if (profile.findFilesys("/usr") != null)
        profile.file.delFilesys("/usr");
    }

    if (usrOW.getState()) {
      if (profile.findFilesys("/usr/openwin") == null) {
        String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
          profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
        int size = fsinfo.getSize("/usr/openwin");
        if (size > 0)
          profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/usr/openwin");
        else
          profile.file.addFilesys("filesys\t" + slice + "\t10\t/usr/openwin");
      }
      ++numfs;
    } else {
      if (profile.findFilesys("/usr/openwin") != null)
        profile.file.delFilesys("/usr/openwin");
    }

    if (var.getState()) {
      if (profile.findFilesys("/var") == null) {
        String slice = ProfileUtil.getDiskInfo().getRootDisk() + "s" +
          profile.getNextSliceNum(ProfileUtil.getDiskInfo().getRootDisk());
        int size = fsinfo.getSize("/var");
        if (size > 0)
          profile.file.addFilesys("filesys\t" + slice + "\t" + size + "\t/var");
        else
          profile.file.addFilesys("filesys\t" + slice + "\t10\t/var");
      }
      ++numfs;
    } else {
      if (profile.findFilesys("/var") != null)
        profile.file.delFilesys("/var");
    }

    if (swap.getState()) {
      if (profile.findFilesys("swap") == null) {
        int size = fsinfo.getSize("swap");
        String swapslice = ProfileUtil.getDiskInfo().getRootDisk() + "s1";
        if (size > 0)
          profile.file.addFilesys("filesys\t" + swapslice + "\t" + size + "\tswap");
        else
          profile.file.addFilesys("filesys\t" + swapslice + "\t" + swapsize + "\tswap");
      }
      ++numfs;
    } else {
      if (profile.findFilesys("swap") != null)
        profile.file.delFilesys("swap");
    }

    if (numfs > 5) {
      String errStr = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 90);
      String okStr = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 91);
      String errorHeader = MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
					    SolarisInstallResources.MSG_ERROR);
      wizardManager.displayQuery(wizardManager, errorHeader, errStr,
				 new String[] {okStr},
				 new String[] {null});
      return false;
    }

    return true;
/*
    boolean changed = change;

    // Should be changed if user selected one of the following that is
    // not already a file system, user didn't select one of the following
    // that is already a file system, or if any file system is too small

    PartitionInfo pi = null;
    Vector v = new Vector();
    pi = profile.findFilesys("/");
    if (root.getState()) {
      v.addElement("/");
      if ((pi == null) || (pi.getSize() < fsinfo.getSize("/"))) {
        changed = true;
      }
    } else if (pi != null) {
      changed = true;
    }

//////////// this section is already commented out 
    if (profile.isServer()) {
        pi = profile.findFilesys("/export");

        if (export.getState()) {
          v.addElement("/export");
          if ((pi == null) || (pi.getSize() < fsinfo.getSize("/export"))) {
            changed = true;
          }
        } else if (pi != null) {
          changed = true;
        }

        pi = profile.findFilesys("/export/root");
        if (exportRoot.getState()) {
          v.addElement("/export/root");
          if ((pi == null) || (pi.getSize() < fsinfo.getSize("/export/root"))) {
            changed = true;
          }
        } else if (pi != null) {
          changed = true;
        }

        pi = profile.findFilesys("/export/swap");
        if (exportSwap.getState()) {
          v.addElement("/export/swap");
          if ((pi == null) || (pi.getSize() < fsinfo.getSize("/export/swap"))) {
            changed = true;
          }
        } else if (pi != null) {
          changed = true;
        }
    }
////////////////////////

    pi = profile.findFilesys("/opt");
    if (opt.getState()) {
      v.addElement("/opt");
      if ((pi == null) || (pi.getSize() < fsinfo.getSize("/opt"))) {
        changed = true;
      }
    } else if (pi != null) {
      changed = true;
    }

    pi = profile.findFilesys("/usr");
    if (usr.getState()) {
      v.addElement("/usr");
      if ((pi == null) || (pi.getSize() < fsinfo.getSize("/usr"))) {
        changed = true;
      }
    } else if (pi != null) {
      changed = true;
    }

    pi = profile.findFilesys("/usr/openwin");
    if (usrOW.getState()) {
      v.addElement("/usr/openwin");
      if ((pi == null) || (pi.getSize() < fsinfo.getSize("/usr/openwin"))) {
        changed = true;
      }
    } else if (pi != null) {
      changed = true;
    }

    pi = profile.findFilesys("/var");
    if (var.getState()) {
      v.addElement("/var");
      if ((pi == null) || (pi.getSize() < fsinfo.getSize("/var"))) {
        changed = true;
      }
    } else if (pi != null) {
      changed = true;
    }
    pi = profile.findFilesys("swap");
    if (swap.getState()) {
      v.addElement("swap");
      // Swap can be auto
      if ((pi == null) ||
          ((pi.getSize() != pi.AUTO) && (pi.getSize() < fsinfo.getSize("swap")))) {
        changed = true;
      }
    } else if (pi != null) {
      changed = true;
    }

    if (changed) {
      profile.createDefaultPartitions(v);

      // We actually want explcit partitioning
      profile.setExplicitPartitioning(true);
    }
    return changed;
*/
  }

  /**
   * Get the runtime classes required by this panel.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.LAYOUT_ALIGNMENT"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.RowLayout"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.Updatable"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.MyRun"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.SolarisWebStartError"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.SolarisFSObj"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.SolarisFileSystem"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.PFInstall"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.MyLocale"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.ProfileUtil"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.Profile"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.ProfileFile"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.SolarisProduct"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.FileAgent"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.FileAgentException"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.ListSelectorPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.FileSysLayOutPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.HeaderPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.InsetsPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.VerticalLayout"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.FlowLabel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.Separator"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.ColumnLayout"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.Header"});
  }
}
