package com.sun.sysid.panels;

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

public class GeoRegionPanel extends WizardLeaf
{
  
  Panel mainPanel = new Panel(new VerticalLayout());
  String geoRegionString;
  Panel geoPanel = null;
  Panel listsPanel = null;
  
  Hashtable zoneTable = new Hashtable(13);
  PanelServices psvc = new PanelServices();
  String timeZone = null;
  
  
  String regions[] = { 
    "Africa", 
    "Asia, Eastern", 
    "Asia, Western",
    "Australia / New Zealand", 
    "Canada", 
    "Europe",
    "Mexico", 
    "South America", 
    "United States"
  };
  
  String AfricaZones[] = {
    "Egypt", 
    "Libya"
  };
  Object AfricaZonesObj = AfricaZones;
  
  String AfricaCodes[] = {
    "Egypt", 
    "Libya"
  };
  Object AfricaCodesObj = AfricaCodes;
  
  String WAsiaZones[] = {
    "Turkey", 
    "Western Soviet Union",
    "Iran", 
    "Israel", 
    "Saudi Arabia"
  };
  Object WAsiaZonesObj = WAsiaZones;
  
  String WAsiaCodes[] = {
    "Turkey", 
    "W-SU",
    "Iran", 
    "Israel", 
    "Mideast/Riyadh89"
  };
  Object WAsiaCodesObj = WAsiaCodes;
  
  String EAsiaZones[] = {
    "People's Republic of China",
    "Republic of China / Taiwan", 
    "Hong Kong",
    "Japan", 
    "Republic of Korea", 
    "Singapore"
  };
  Object EAsiaZonesObj = EAsiaZones;
  
  String EAsiaCodes[] = {
    "PRC",
    "ROC",
    "Hongkong",
    "Japan",
    "ROK",
    "Singapore"
  };
  Object EAsiaCodesObj = EAsiaCodes;
  
  String AustraliaZones[] = {
    "Tasmania", 
    "Queensland", 
    "North",
    "West", 
    "South", 
    "Victoria", 
    "New South Wales", 
    "Broken Hill",
    "Yancowinna", 
    "LHI", 
    "New Zealand"
  };
  Object AustraliaZonesObj = AustraliaZones;
  
  String AustraliaCodes[] = {
    "Australia/Tasmania",
    "Australia/Queensland",
    "Australia/North",
    "Australia/West",
    "Australia/South",
    "Australia/Victoria",
    "Australia/NSW",
    "Australia/Broken_Hill",
    "Australia/Yancowinna",
    "Australia/LHI",
    "NZ"
  };
  Object AustraliaCodesObj = AustraliaCodes;
  
  String CanadaZones[] = {
    "Newfoundland", 
    "Atlantic", 
    "Eastern",
    "Central", 
    "East Saskatchewan", 
    "Mountain", 
    "Pacific",
    "Yukon"
  };
  Object CanadaZonesObj = CanadaZones;
  
  String CanadaCodes[] = {
    "Canada/Newfoundland",
    "Canada/Atlantic",
    "Canada/Eastern",
    "Canada/Central",
    "Canada/East-Saskatchewan",
    "Canada/Mountain",
    "Canada/Pacific",
    "Canada/Yukon"
  };
  Object CanadaCodesObj = CanadaCodes;
  
  String EuropeZones[] = {
    "Great Britain", 
    "Ireland", 
    "Iceland",
    "Poland", 
    "Western Europe", 
    "Middle Europe", 
    "Eastern Europe"
  };
  Object EuropeZonesObj = EuropeZones;
  
  String EuropeCodes[] = {
    "GB",
    "Eire",
    "Iceland",
    "Poland",
    "WET",
    "MET",
    "EET"
  };
  Object EuropeCodesObj = EuropeCodes;
  
  String MexicoZones[] = {
    "Mexico / Baja Norte", 
    "Mexico / Baja Sur",
    "Mexico / General"
  };
  Object MexicoZonesObj = MexicoZones;
  
  String MexicoCodes[] = {
    "Mexico/BajaNorte",
    "Mexico/BajaSur",
    "Mexico/General"
  };
  Object MexicoCodesObj = MexicoCodes;
  
  String SAmericaZones[] = {
    "Brazil East", 
    "Brazil West", 
    "Brazil Acre",
    "Brazil De Noronha", 
    "Chile Continental", 
    "Chile Easter Island"
  };
  Object SAmericaZonesObj = SAmericaZones;
  
  String SAmericaCodes[] = {
    "Brazil/East",
    "Brazil/West",
    "Brazil/Acre",
    "Brazil/DeNoronha",
    "Chile/Continental",
    "Chile/EasterIsland"
  };
  Object SAmericaCodesObj = SAmericaCodes;
  
  String USAZones[] = {
    "Eastern", 
    "Central", 
    "Mountain", 
    "Pacific",
    "East-Indiana", 
    "Arizona",
    "Michigan", 
    "Samoa", 
    "Alaska",
    "Aleutian", 
    "Hawaii"
  };	
  Object USAZonesObj = USAZones;
  
  String USACodes[] = {
    "US/Eastern",
    "US/Central",
    "US/Mountain",
    "US/Pacific",
    "US/East-Indiana",
    "US/Arizona",
    "US/Michigan",
    "US/Samoa",
    "US/Alaska",
    "US/Aleutian",
    "US/Hawaii"
  };	
  Object USACodesObj = USACodes;
  
  String ArrayOfZoneStrs[][] = {AfricaZones, EAsiaZones, WAsiaZones,
				AustraliaZones, CanadaZones, EuropeZones, MexicoZones,
				SAmericaZones, USAZones};
  
  String ArrayOfCodeStrs[][] = {AfricaCodes, EAsiaCodes, WAsiaCodes,
				AustraliaCodes, CanadaCodes, EuropeCodes, MexicoCodes,
				SAmericaCodes, USACodes};
  
  Object ArrayOfZones[] = {AfricaZonesObj, EAsiaZonesObj, WAsiaZonesObj,
			   AustraliaZonesObj, CanadaZonesObj, EuropeZonesObj, MexicoZonesObj,
			   SAmericaZonesObj, USAZonesObj};
  
  Object ArrayOfCodes[] = {AfricaCodesObj, EAsiaCodesObj, WAsiaCodesObj,
			   AustraliaCodesObj, CanadaCodesObj, EuropeCodesObj, MexicoCodesObj,
			   SAmericaCodesObj, USACodesObj};
  
  
  String regionStr;
  String zoneStr;
  
  java.awt.List regionList;
  java.awt.List timeZoneList;
  
  int selectedRegion;
  int selectedZone;
  
  
  int numCodes = AfricaCodes.length + EAsiaCodes.length + WAsiaCodes.length + 
  AustraliaCodes.length + CanadaCodes.length + EuropeCodes.length + 
  MexicoCodes.length + SAmericaCodes.length + USACodes.length;
  Hashtable codeTable= new Hashtable(numCodes);
  
  public GeoRegionPanel()
    {
    }
  
  public GeoRegionPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public GeoRegionPanel(WizardState wizardState)
    {
      super(wizardState, "Geographic Region");
    }
  
  public GeoRegionPanel(String name, Route route, WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }
  
  public void createUI()
    {
      WizardTreeManager wizardManager = getTreeManager();
      super.createUI();

      /*
       * Set the header text.
       */
      HeaderPanel headerPanel = getHeaderPanel();

      String heading = MsgCat.getString("com.sun.sysid.Sysid", 
						"Geographic Region");

      if(headerPanel != null) {
        headerPanel.setText(heading);
      }

      
      selectedRegion = 0; 
      selectedZone = 0;
      String timeZone = (String) wizardManager.callServerObjectMethod(getRoute(),
								      "getData",
								      new String[] { "java.lang.String.class" },
								      new Object[] { "timeZone"});
      String geoString = MsgCat.getString("com.sun.sysid.Sysid", 
					  "geoRegionString");
      
      mainPanel.add(new FlowLabel(geoString), "1");
      
      for (int i=0; i < regions.length; i++) {
	regions[i] = MsgCat.getString("com.sun.sysid.Sysid", 
				      regions[i]);
      }
      
      for (int i=0; i<ArrayOfZoneStrs.length; i++) {
	for (int j=0; j<ArrayOfZoneStrs[i].length; j++) {
	  ArrayOfZoneStrs[i][j] = MsgCat.getString("com.sun.sysid.Sysid", 
						   ArrayOfZoneStrs[i][j]);
	  
	  String key = null;
	  Integer fst = new Integer(i);
	  Integer scnd = new Integer(j);
	  key = fst.toString() + "/" + scnd.toString();
	  codeTable.put(key, ArrayOfCodeStrs[i][j]);
	  if (timeZone.equals(ArrayOfCodeStrs[i][j])) {
	    selectedRegion = i;
	    selectedZone = j;
	    if (Def.DEBUG) {
	      System.out.println("Marking " + 
				 ArrayOfCodeStrs[i][j] + " at " +i + ", " + 
				 j  + " as selected");
	    }
	  }

	}
      }

				
      for (int i=0; i<regions.length; i++)
	zoneTable.put(regions[i], ArrayOfZones[i]);
	
      Panel p = psvc.createDualLists(regionList, regionStr,
				     timeZoneList, zoneStr,
				     regions, ArrayOfZones,
				     zoneTable, 10, selectedRegion, selectedZone);

		
		
					
      mainPanel.add(p, "3");
      add(mainPanel, "Center");
    }

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


   public void consoleInteraction()
     {
       geoRegionString = MsgCat.getString("com.sun.sysid.Sysid", 
                                          "cli_geoRegionString");
 
         String prompt1 = MsgCat.getString("com.sun.sysid.Sysid", "cli_regionStr");
         String desc1 = MsgCat.getString("com.sun.sysid.Sysid", "cli_regionStr2");
         String prompt2 = MsgCat.getString("com.sun.sysid.Sysid", "cli_zoneStr");
         String desc2 = MsgCat.getString("com.sun.sysid.Sysid", "cli_zoneStr2");
 
         selectedRegion =  TTYDisplay.queryValue( prompt1, desc1, regions, selectedRegion, 
                                                  geoRegionString);
         
         String zones[] = (String[])zoneTable.get(regions[selectedRegion]);
         
         selectedZone = TTYDisplay.queryValue( prompt2, desc2, zones, selectedZone,
                                               geoRegionString);
     }

  public boolean isDisplayComplete()
    {
      WizardTreeManager wizardManager = getTreeManager();
      int sel1=0;
      int sel2=0;
      if (wizardManager.hasGUI())
      {
	 //PanelServices psvc = new PanelServices();
	 sel1 = psvc.listChoice1;
	 sel2 = psvc.listChoice2;
      }
      else
      {
	 sel1 = selectedRegion;
	 sel2 = selectedZone;
      }
		
      String key = sel1 + "/" + sel2;

      String timeZone = (String)codeTable.get(key);

      if (Def.DEBUG) System.out.println("timeZone = " + timeZone);
		
      if (timeZone != null) {
	  wizardManager.callServerObjectMethod(
		getRoute(), "setData",
		new String[] { "java.lang.String.class",
		"java.lang.Object.class" },
		new Object[] { "timeZone", timeZone } );

	  String timeZoneStr = ArrayOfZoneStrs[sel1][sel2];
          if (timeZoneStr == null) timeZoneStr = timeZone; 
          if (Def.DEBUG) System.out.println("timeZoneStr = " + timeZoneStr); 

	  wizardManager.callServerObjectMethod(
		getRoute(), "setData",
		new String[] { "java.lang.String.class",
		"java.lang.Object.class" },
		new Object[] { "timeZoneStr", timeZoneStr } );

	  return(true);
      }

      if (Def.DEBUG) System.out.println("Can not determine timeZone");
      return (false);
    }

	

  public void addRuntimeResources(Vector resourceVector)
    {
      super.addRuntimeResources(resourceVector);
      resourceVector.addElement(new String[] {null, 
						"com.sun.sysid.panels.GeoRegionPanel"});
      resourceVector.addElement(new String[] {null, 
						"com.sun.sysid.SysidResources"});
      resourceVector.addElement(new String[] {null, 
						"com.sun.sysid.core.PanelServices"});
      resourceVector.addElement(new String[] {null,
						"com.sun.wizards.awt.InsetsPanel"});
      resourceVector.addElement(new String[] {null, 
						"com.sun.wizards.awt.FullWidthPanel"});
      resourceVector.addElement(new String[] {null, 
						"com.sun.wizards.awt.FlowLabel"});
      resourceVector.addElement(new String[] {null, 
						"com.sun.wizards.awt.VerticalLayout"});
      resourceVector.addElement(new String[] {null,
						"com.sun.wizards.awt.Separator"});
    }

	
}

