package com.sun.solarisinstall.panels;

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

/**
 * The SolarisBackupMediaPanel allows the user to select
 * an install directory for a product or a product
 * component.
 */
public class SolarisBackupMediaPanel extends WizardLeaf implements ItemListener
{

  /* The main panel*/
  private Panel mainPanel;
  private Panel entryFlowPanel;
  private Panel choicePanel;
  private Panel choiceFlowPanel;
  private Panel entryPanel;
  private static final int NONE = 0;
  private static final int DISK = 1;
  private static final int TAPE = 2;
  private static final int REMOTE = 3;

  /**
   * The text field used for entering the backup media type
   */
  private Choice mediaType = null;
  
  /**
   * The text field used for entering the backup media path
   */
  private TextField mediaPathField = null;
  
  /**
   * The key used for looking up the default value
   * in the WizardState.
   */
  private String defaultKey = "";

  /**
   * The key used for looking up the current value
   * in the WizardState.
   */
  private String currentKey = "";

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


  /**
   * Creates a SolarisBackupMediaPanel with the specified name,
   * which defaults to the value indexed by the specified key.
   *
   * @param wizardState	The buildtime WizardState.
   * @param name		The name for this ProgressPanel.
   * @param defaultKey		The key to the default type/path
   * @param currentKey		The key to the current type/path
   */
  public SolarisBackupMediaPanel(
	WizardState wizardState, String name)
    {
      super(wizardState, name);
    }

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

  public void beginDisplay()
    {
      super.beginDisplay();
      mediaType.requestFocus();
    }

  public void consoleInteraction()
    {
      String desc = 
	MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			 SolarisInstallResources.MSG_UPGRADE_SELECTED);

      String[] types = new String[mediaType.getItemCount()];
      for (int i=0; i<types.length; i++)
	{
	  types[i] = mediaType.getItem(i);
	}

      int result = 
	TTYDisplay.queryValue(MsgCat.getString("com.sun.wizards.Wizard",
				WizardResources.MSG_CLI_CLC_PROMPT),
			      desc,
			      types,
			      mediaType.getSelectedIndex(), "");
      mediaType.select(result);

      /*
       * Fake an AWT event (since we are in CLI mode they are not fired)
       */
      ItemSelectable source = mediaType;
      int id = ItemEvent.ITEM_FIRST;
      Object item = this;
      int stateChange = ItemEvent.SELECTED;
      itemStateChanged(new ItemEvent(source, id, item, stateChange));

      if (result != NONE)
	{
	  String pathExample = 
	    MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			     SolarisInstallResources.MSG_BACKUP_PATH) + 
	    " " + getExample(result);
	    
	  String path = TTYDisplay.queryValue(pathExample, "", "");
	  mediaPathField.setText(path);
	}
    }
  /**
   * This method creates the user interface.
   */
  public void createUI()
    {
      super.createUI();

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

      String descriptionText;
      
      /* initialize the panels */
      choicePanel = new Panel(new VerticalLayout());
      choiceFlowPanel = new Panel(new FlowLayout());
      entryFlowPanel = new Panel(new FlowLayout());
      entryPanel = new Panel(new FlowLayout());
      mainPanel = new Panel(new FlowLayout());
      /*
       * Add the text.
       */
      descriptionText = 
             MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
             SolarisInstallResources.MSG_UPGRADE_SELECTED);
      FlowLabel textLabel = new FlowLabel(descriptionText);
      mainPanel.add(textLabel, "0");

      /* Create the choice Box */
      String none = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
             SolarisInstallResources.MSG_NONE);
      String disk = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
             SolarisInstallResources.MSG_BACKUP_DISK);
      String tape = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
             SolarisInstallResources.MSG_BACKUP_TAPE);
      String remote = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
             SolarisInstallResources.MSG_BACKUP_REMOTE);

      FlowLabel typeLabel = 
	new FlowLabel("<bold>" + 
		      MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
				       SolarisInstallResources.MSG_BACKUP_TYPE));
      mediaType = new Choice();
      mediaType.add(none);
      mediaType.add(disk);
      mediaType.add(tape);
      mediaType.add(remote);

      // add the listener
      mediaType.addItemListener(this);

      choicePanel.add(typeLabel, "1");
      choicePanel.add(mediaType, "2");
      choiceFlowPanel.add(choicePanel, FlowLayout.LEFT);
      mainPanel.add(choiceFlowPanel, "3");
      add(mainPanel, "Center");
    }


  /**
   * Returns true if this SolarisBackupMediaPanel is finished displaying.
   * This method can be used to verify user input.
   *
   * @return	true if the display of this SolarisBackupMediaPanel is
   *		complete; false otherwise.
   */
  public boolean isDisplayComplete()
    {
      WizardTreeManager wizardManager = getTreeManager();
      String type = "";
      String path = "";
      String backupDisplayString = "";

      switch (mediaType.getSelectedIndex()) 
	{
	case NONE :
	  backupDisplayString = 
	    MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			     SolarisInstallResources.MSG_NONE);
	  type = "none";
	  path = "";
	  break;
	case DISK :
	  backupDisplayString =
	    MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			     SolarisInstallResources.MSG_BACKUP_DISK);
	  type = "local_filesystem";
	  path = mediaPathField.getText();
	  break;
	case TAPE :
	  backupDisplayString =
	    MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			 SolarisInstallResources.MSG_BACKUP_TAPE);
	  type = "local_tape";
	  path = mediaPathField.getText();
	  break;
	case REMOTE :
	  backupDisplayString =
	    MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
			     SolarisInstallResources.MSG_BACKUP_REMOTE);
	  type = "remote_filesystem";
	  path = mediaPathField.getText();
	  break;
	default :
	  System.err.println("Invalid backup media selection");
	  break;
	}
       wizardManager.callServerObjectMethod(getRoute(),
			"setData",
			new String[] {"com.sun.wizards.core.Route.class",
				       "java.lang.String.class",
				       "java.lang.Object.class"},
		        new Object[] {null,
				      "backupDisplayString", 
				       backupDisplayString});

      wizardManager.callServerObjectMethod(getRoute(),
					   "setData",
					   new String[] {"com.sun.wizards.core.Route.class",
							   "java.lang.String.class",
							   "java.lang.Object.class"},
					   new Object[] {null,
							   "backupType", 
							   type});
      
      wizardManager.callServerObjectMethod(getRoute(),
					   "setData",
					   new String[] {"com.sun.wizards.core.Route.class",
							   "java.lang.String.class",
							   "java.lang.Object.class"},
					   new Object[] {null,
							   "backupPath", 
							    path});
      Boolean retVal = (Boolean)wizardManager.callServerObjectMethod(
					   getRoute().getChildServerRoute("profileObject"),
					   "verifyBackupMedia",
					   null, null);
      if (!retVal.booleanValue()) {
	String error =
            MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                             SolarisInstallResources.MSG_ERROR);
	String message = 
	  MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			   SolarisInstallResources.MSG_BACKUP_ACCESS_ERROR);
	String okStr =
	  MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			   SolarisInstallResources.MSG_OK);
	
	wizardManager.displayQuery(this, error, message,
				   new String[] {okStr},
				   new String[] {null});
      } else {
	wizardManager.callServerObjectMethod(
					   getRoute().getChildServerRoute("profileObject"),
					   "setProfileBackupMedia",
					   null, null);
      }
      return retVal.booleanValue();
      
    }

      private String getExample(int type)
      {
	 String example = "";
	 switch (type)
	 {
	    case NONE :
	       break;
	    case DISK :
	       example = "ex. /dev/dsk/c0t0d0s3";
	       break;
	    case TAPE :
	       example = "ex. /dev/rmt/0";
	       break;
	    case REMOTE :
	       example = "ex. server:/writeable/path";
	       break;
	    default :
	       System.err.println("Invalid backup media selection");
	       break; 
	 }
	 return example;
      }
  public void itemStateChanged(ItemEvent e)
    {

      Choice c = (Choice) e.getItemSelectable();
      String example = getExample(c.getSelectedIndex());
      if (c.getSelectedIndex() != 0)
	{
	   FlowLabel pathLabel = 
	    new FlowLabel("<bold>" + 
			  MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
					   SolarisInstallResources.MSG_BACKUP_PATH) + " " + example);
	  mediaPathField = new TextField("", 15);
	  mainPanel.remove(entryFlowPanel);
	  entryFlowPanel = new Panel(new FlowLayout());
	  entryPanel = new Panel(new VerticalLayout());
	  entryPanel.add(pathLabel, "1");
	  entryPanel.add(mediaPathField, "2");
	  entryFlowPanel.add(entryPanel, FlowLayout.LEFT);
	  mainPanel.add(entryFlowPanel, "4");
	  mainPanel.validate();
	} else {
	  mainPanel.remove(entryFlowPanel);
	  mainPanel.invalidate();
	}
    }

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

  /**
   * Get the runtime classes required by this panel.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.SolarisInstallResources"});
    resourceVector.addElement(new String[] {null, "com.sun.install.InstallResources"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.SolarisBackupMediaPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.install.products.InstallConstants"});
    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"});
  }
}
