package com.sun.solarisinstall.panels;

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

import com.sun.solarisinstall.*;

/**
 * ScanKioskPanel - Not yet implemented 
 * Place Holder panel based on TextImagePanel 
 */

public class ScanKioskPanel extends TextImagePanel 
{
  /*
   * Creation Methods
   */

  public ScanKioskPanel()
    {
    }

  public ScanKioskPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }

  public ScanKioskPanel(String name, Route route, WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }

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

    String heading = MsgCat.getString(
		"com.sun.solarisinstall.SolarisInstall", 
		SolarisInstallResources.MSG_DOWNLOAD);

    if(getTreeManager().hasGUI())
      {
	add(new HeaderPanel(heading, null), "North");
      }

    Object[] msgArgs = { ProfileConstants.downloadDir };

    addText(MsgCat.getString(
		"com.sun.solarisinstall.SolarisInstall", 
		SolarisInstallResources.MSG_SCAN_AT, msgArgs), 0, 0);
  }

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

    WizardTreeManager wizardManager = getTreeManager();

    wizardManager.callServerObjectMethod(
                getRoute(),
                "setData",
                new String[] {"com.sun.wizards.core.Route.class",
                              "java.lang.String.class",
                              "java.lang.Object.class"},
                new Object[] {null, ProfileConstants.wsrClassDir,
                        ProfileConstants.downloadDir});

    wizardManager.callServerObjectMethod(
                getRoute(),
                "setData",
                new String[] {"com.sun.wizards.core.Route.class",
                              "java.lang.String.class",
                              "java.lang.Object.class"},
                new Object[] {null, ProfileConstants.wsrExpandDir,
                        ProfileConstants.WSR_tables});

    /*
     * Perform a sequence that extracts downloaded wizard WSR data 
     */
    wizardManager.callServerObjectMethod(
        getRoute(),
        "performSequence",
        new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Boolean.TYPE",
                      "java.lang.String.class"},
        new Object[] {getRoute(null),
                      ProfileConstants.wsrDataExtract,
                      new Boolean(false),
                      "sequenceComplete"});

  }

  /*
   * Check the result of WSR data extraction.  If OK, continue to next
   * panel. Otherwise, give user a popup.
   */

  public boolean sequenceComplete()
  {
     WizardTreeManager wizardManager = getTreeManager();

     Boolean filesExist =
           (Boolean)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {ProfileConstants.WSRFilesExist});

    if ((filesExist == null) || (filesExist.booleanValue())) {
      /*
       * WSR file extraction successfull, next panel
       */
       if (wizardManager.hasGUI())
       {
          wizardManager.nextButtonPressed();
       }

       return(true);

    } else {
      /*
       * No WSR downloaded class files found.
       */

        if (wizardManager.hasGUI()) {
	   wsrNotFoundNotice();
	}

	return(false);
    }
  }

  protected void wsrNotFoundNotice() 
  {
    WizardTreeManager wizardManager = getTreeManager();
      /*
       * No WSR downloaded class files found.  Pop up the no 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_NO_KIOSK_WSR),
        new String[] {MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                            SolarisInstallResources.MSG_OK)},

        new String[] {"goBack"});  
  }

  public boolean isDisplayComplete()
  {
    WizardTreeManager wizardManager = getTreeManager();

    /*
     * Scan for and store WSR product data structures 
     */

    Boolean wsrProductsFound =
        (Boolean) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "wsrFileScan",
                  new String[] {"java.lang.String.class",
				"java.lang.Integer.TYPE"},
                  new Object[] {ProfileConstants.WSR_tables,
                                new Integer(CDNode.KIOSK)} );


    if ( wsrProductsFound == null || 
         wsrProductsFound.booleanValue() == false) {

	wsrNotFoundNotice();

        if (wizardManager.hasGUI()) {
	   return(false);
	} 

	/* Note: CLI mode always goes forward, no back button */
    }

    /*
     * WSR file scan was successfull (or we are in CLI mode) 
     * 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)});

    return(true);  // Scan completed, go forward to product panel
  }

   public void consoleInteraction()
   {
     TTYDisplay.showText(MsgCat.getString(
		"com.sun.solarisinstall.SolarisInstall", 
		SolarisInstallResources.MSG_SCAN_KIOSK));
   }

   public void goBack()
   {
     WizardTreeManager wizardManager = getTreeManager();

     /*
      * If in GUI mode, go back one panel and prepare for retry.
      * Don't go back in CLI mode.  Continue forward to loops end.
      * (ie generally no "back button" in CLI mode).
      */
     if (wizardManager.hasGUI()) {
       wizardManager.backButtonPressed();
     }
   }

   public int getButtonMask()
   {
     return(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.solarisinstall.panels.ScanKioskPanel"});
  }
}
