package com.sun.launcher.panels;

import java.awt.*;
import java.util.*;
import com.sun.wizards.core.*;
import com.sun.wizards.awt.*;
import com.sun.launcher.*;
import com.sun.launcher.core.*;

/*
 *  This panel tells user we are reading from CD. A task is called to
 *  actually mount the CD.
 */
public class LoadingCDPanel extends ParentPanel
{
  private transient Panel mainPanel = null;
  private transient Panel textPanel = null;
  private transient FlowLabel LoadingCD = null;
  private boolean noExitSkipButtons = false;
  private String CDtext = null;
  private boolean readingCD = false;

  public LoadingCDPanel()
  {
  }

  public LoadingCDPanel(WizardState wizardState, String name)
  {
    super(wizardState, name);
  }
    
  public LoadingCDPanel(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.launcher.Launcher", 
                                       LauncherResources.MSG_HDR_LOADING_CD);
    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()
  {
     String[] spinner = {"/", "-", "\\", "|"};
     
     TTYDisplay.showNewline();
     TTYDisplay.print(CDtext+"  ");
	 
     int i=0;
     while(readingCD) 
     {
	try
	{ 
	   TTYDisplay.print("\b"+spinner[i++ % spinner.length]);
	   Thread.sleep(800); 
	}
	catch(InterruptedException ie)
	{
	}
     }
     TTYDisplay.print("\b");
     TTYDisplay.showNewline();
     TTYDisplay.showNewline();
     TTYDisplay.showNewline();
  }

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

    if (LoadingCD != null) textPanel.remove(LoadingCD);

    WizardTreeManager wizardManager = getTreeManager();

    String noExSkButtons =
           (String)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {LaunchConstants.NoExitSkipButtons});

    if ((noExSkButtons != null) && ( noExSkButtons.equals("YES"))){
      noExitSkipButtons = true;
    } else {
      noExitSkipButtons = false;
    }

    String CDName=
           (String)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {LaunchConstants.CDName});


    Object[] CDnameArg = { CDName };
    CDtext =  MsgCat.getString("com.sun.launcher.Launcher", 
                LauncherResources.MSG_READING_CD, CDnameArg); 

    LoadingCD = new FlowLabel(CDtext);

    textPanel.add(LoadingCD, "0");

    readingCD = true;

    /*
     * Perform a sequence to mount the CDMount the CD
     */
    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),
                      "mountCDSequence",
                      new Boolean(false),
                      "mountCDresult"});

   }


   /*
    * check the result of mounting the cd. If OK, continue to next 
    * panel. Otherwise, give user a popup.
    */
  public void mountCDresult()
  {
     WizardTreeManager wizardManager = getTreeManager();
     readingCD = false;

     Boolean mounted =
           (Boolean)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {LaunchConstants.CDmountOK});

    /*
     *  If we successfully mounted the CD, continue on to next panel. 
     *  Otherwise, give the user a pop-up
     */
    if ((mounted == null) || (mounted.booleanValue())) {
       if (wizardManager.hasGUI())
       {
	  wizardManager.nextButtonPressed();
       }
    } else {
      /*
       * Pop up a dialog box, do logging.
       */
      String reason =
           (String)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {LaunchConstants.CDmountFailCode});

      String CDName=
           (String)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {LaunchConstants.CDName});


      Object[] CDnameArg = { CDName };
      String dialogMessage = null;
      String logMessage = null;

      if ((reason != null) && (reason.equals(LaunchConstants.WrongCD))) {
        dialogMessage = MsgCat.getString("com.sun.launcher.Launcher", 
                     LauncherResources.MSG_CD_MOUNT_FAILED_WRONGCD, CDnameArg);
        logMessage = MsgCat.getString("com.sun.launcher.Launcher", 
                  LauncherResources.MSG_LOG_CD_MOUNT_FAILED_WRONGCD, CDnameArg);
      } else {
        dialogMessage = MsgCat.getString("com.sun.launcher.Launcher", 
                     LauncherResources.MSG_CD_MOUNT_FAILED, CDnameArg);
        logMessage = MsgCat.getString("com.sun.launcher.Launcher", 
                     LauncherResources.MSG_LOG_CD_MOUNT_FAILED, CDnameArg);
      }

      callWriteToLog(logMessage);

      wizardManager.displayQuery( this, dialogMessage,
            new String[] {MsgCat.getString("com.sun.launcher.Launcher", 
                                            LauncherResources.MSG_OK)}, 
            new String[] {"goBack"});
    }

  }


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

     wizardManager.backButtonPressed();
   }


   public int getButtonMask()
   {
     if (noExitSkipButtons) {
       return(0);
     } else {
       return(0);
       //return(EXIT_BUTTON);
     }
   }
    

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