import java.awt.*;
import java.awt.Toolkit;
import java.awt.event.*;
import java.applet.*;

public class LocationFrame extends Frame
  implements ActionListener, ItemListener, FocusListener {
  
  PVPanel controller = null;
  
  // Font
  static final Font fontHeading = new Font("SanSerif", Font.BOLD, 14);
  static final Font fontText = new Font("Helvetica", Font.PLAIN, 8);

  // Size
  static final int size_width = 520;
  static final int size_height = 350;
  
  // Number of Australian locations in the array
  static final int AU_Location = 22;
  
  // Number of U.S.A. Locations in the array
  static final int USA_Location = 23;
  
  protected FixedData fixed_data = new FixedData();
  //protected VariableData variable_data = new VariableData();
  
  protected Choice cFirst = new Choice();
  protected Choice cName = new Choice();
  protected TextField tName = new TextField();
  protected TextField tLatitude = new TextField();
  protected TextField tIrrad[] = new TextField[12];
  protected CheckboxGroup cbgHemisphere;
  protected Checkbox cbNorth;
  protected Checkbox cbSouth;
  protected Label lHemisphere = new Label("");
  
  protected Button bDone = new Button("Done");

  // Variable Data
  protected int choice_first;
  protected int choice_location;
  protected String text_location = new String();
  protected double latitude;
  protected int hemisphere;
  protected double irrad[] = new double[12];

  protected String months[] = {
    "January:", "February:", "March:", "April:", "May:", "June:", 
    "July:", "August:", "September:", "October:", "November:", "December:"};

  Dimension d;

  // Initialise the values to default and draw screen
  LocationFrame(PVPanel pv_panel) {
    super("Location");

    setSize(size_width, size_height);  

    d = new Dimension(size_width, size_height);
    
    setLayout(null);    

    this.controller = pv_panel;

    cFirst.add("Australia");
    cFirst.add("U.S.A.");
    cFirst.add("New location");

    Label lLocation = new Label("Location:");
    lLocation.setFont(fontHeading);
    lLocation.setLocation(10, 44);
    lLocation.setSize(80, 16);
    lLocation.setVisible(true);
    add(lLocation);
    cFirst.setLocation(95, 42);
    cFirst.setSize(80, 20);
    cFirst.setVisible(true);
    add(cFirst);
    cName.setLocation(230, 42);
    cName.setSize(80, 20);
    cName.setVisible(true);
    add(cName);
    tName.setFont(fontText);
    tName.setLocation(385, 41);
    tName.setSize(110, 25);
    tName.setVisible(true);
    add(tName);
    
    Label lLatitude = new Label("Latitude:");
    lLatitude.setFont(fontHeading);
    lLatitude.setLocation(10, 76);
    lLatitude.setSize(80, 16);
    lLatitude.setVisible(true);
    add(lLatitude);
    tLatitude.setFont(fontText);
    tLatitude.setLocation(95, 73);
    tLatitude.setSize(50, 25);
    tLatitude.setVisible(true);
    add(tLatitude);

    // Hemisphere
    
    lHemisphere.setLocation(160, 76);
    lHemisphere.setSize(140, 20);
    lHemisphere.setVisible(true); 
    add(lHemisphere);

    cbgHemisphere = new CheckboxGroup();

    cbNorth = new Checkbox("North", cbgHemisphere, false);
    cbNorth.setLocation(160, 73);
    cbNorth.setSize(80, 20);
    cbNorth.setVisible(true);   
    add(cbNorth);

    cbSouth = new Checkbox("South", cbgHemisphere, true);
    cbSouth.setLocation(260, 73);
    cbSouth.setSize(80, 20);
    cbSouth.setVisible(true);
    add(cbSouth);
    
    Label lIrrad = new Label("Global Irradiation Data (kWh/m^2)");
    lIrrad.setFont(fontHeading);
    lIrrad.setLocation(140, 105);
    lIrrad.setSize(250, 20);
    lIrrad.setVisible(true);
    add(lIrrad);

    Label lMonth[] = new Label[12];
    for (int i = 0; i < 6; i++) {
      lMonth[i] = new Label(months[i]);
      lMonth[i].setLocation(10, 130+(i*25));
      lMonth[i].setSize(60, 25);
      lMonth[i].setVisible(true);
      add(lMonth[i]);
      tIrrad[i] = new TextField();
      tIrrad[i].setFont(fontText);
      tIrrad[i].setLocation(70, 127+(i*25));
      tIrrad[i].setSize(80, 25);
      tIrrad[i].setVisible(true);
      add(tIrrad[i]);
    }

    for (int i = 6; i < 12; i++) {
      lMonth[i] = new Label(months[i]);
      lMonth[i].setLocation(300, 130+((i-6)*25));
      lMonth[i].setSize(75, 25);
      lMonth[i].setVisible(true);
      add(lMonth[i]);
      tIrrad[i] = new TextField();
      tIrrad[i].setFont(fontText);
      tIrrad[i].setLocation(385, 127+((i-6)*25));
      tIrrad[i].setSize(80, 25);
      tIrrad[i].setVisible(true);
      add(tIrrad[i]);
    }

    bDone.setLocation(235, 300);
    bDone.setSize(50, 25);
    bDone.setVisible(true);
    add(bDone);

    cFirst.addItemListener(this);
    cName.addItemListener(this);
    tName.addFocusListener(this);
    tLatitude.addFocusListener(this);
    for (int i = 0; i < 12; i++)
      tIrrad[i].addFocusListener(this);
    bDone.addActionListener(this);
			    
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent event) {
	setVisible(false);
      }
    });

    this.setBackground(Color.lightGray);
    this.setForeground(Color.black);

    validate();
    DefaultValues();
  }


  public void DefaultValues() {
    choice_first = 0;
    cName.setVisible(true);
    for (int i = 0; i < AU_Location; i++)
      cName.add(fixed_data.location_name[i]);
    
    choice_location = 0;
    cName.select(choice_location);
    
    text_location = fixed_data.location_name[choice_location];
    tName.setText(text_location);
    tName.setEditable(false);
    
    latitude = fixed_data.location_latitude[choice_location];
    cbNorth.setVisible(false);
    cbSouth.setVisible(false);
    lHemisphere.setVisible(true);
    if (latitude > 0) {
      tLatitude.setText(String.valueOf(latitude));
      lHemisphere.setText("Hemisphere North");
      hemisphere = 1;
    } else {
      tLatitude.setText(String.valueOf(-latitude));
      lHemisphere.setText("Hemisphere South");
      hemisphere = -1;
    }
    tLatitude.setEditable(false);
    
    for (int i = 0; i < 12; i++) {
      irrad[i] = fixed_data.location_irradiation[choice_location*12+i];
      tIrrad[i].setText(String.valueOf(irrad[i]));
      tIrrad[i].setEditable(false);
    }
    Update();
  }

  public void actionPerformed(ActionEvent event) {
    if (bDone == event.getSource()) {
      Update();
      setVisible(false);
    }    
  } 
  
  public void focusGained (FocusEvent event) {
  }
  
  public void focusLost (FocusEvent event) {
    if (tName ==  event.getSource())
      changedName();
    if (tLatitude == event.getSource())
      changedLatitude();
    for (int i = 0; i < 12; i++)
      if (tIrrad[i] == event.getSource())
	changedIrradiation(i);
  }

  public void itemStateChanged(ItemEvent event) {
    if (cFirst == event.getSource())
      changedFirstChoice();
    if (cName == event.getSource())
      changedLocation();
  }
  
  private void changedFirstChoice() {
    if (choice_first != cFirst.getSelectedIndex()) {
      choice_first = cFirst.getSelectedIndex();
	if (choice_first == 0) {
	  cName.setVisible(true);
	  cName.removeAll();
	  for (int i = 0; i < AU_Location; i++)
	    cName.add(fixed_data.location_name[i]);
	  
	  choice_location = 0;
	  cName.select(choice_location);
	  
	  text_location = fixed_data.location_name[choice_location];
	  tName.setText(text_location);
	  tName.setEditable(false);
	  
	  latitude = fixed_data.location_latitude[choice_location];
	  cbNorth.setVisible(false);
	  cbSouth.setVisible(false);
	  lHemisphere.setVisible(true);
	  if (latitude > 0) {
	    tLatitude.setText(String.valueOf(latitude));
	    lHemisphere.setText("Hemisphere North");
	    hemisphere = 1;
	  } else {
	    tLatitude.setText(String.valueOf(-latitude));
	    lHemisphere.setText("Hemisphere South");
	    hemisphere = -1;
	  }
	  tLatitude.setEditable(false);
	  
	  for (int i = 0; i < 12; i++) {
	    irrad[i] = fixed_data.location_irradiation[choice_location*12+i];
	    tIrrad[i].setText(String.valueOf(irrad[i]));
	    tIrrad[i].setEditable(false);
	  }
	}
	if (choice_first == 1) {
	  cName.setVisible(true);
	  cName.removeAll();
	  for (int i = AU_Location; i < USA_Location + AU_Location; i++)
	    cName.add(fixed_data.location_name[i]);
	  
	  choice_location = 0;
	  cName.select(choice_location);
	  
	  text_location = fixed_data.location_name[choice_location+AU_Location];
	  tName.setText(text_location);
	  tName.setEditable(false);

	  latitude = fixed_data.location_latitude[choice_location+AU_Location];
	  cbNorth.setVisible(false);
	  cbSouth.setVisible(false);
	  lHemisphere.setVisible(true);
	  if (latitude > 0) {
	    tLatitude.setText(String.valueOf(latitude));
	    lHemisphere.setText("Hemisphere North");
	    hemisphere = 1;
	  } else {
	    tLatitude.setText(String.valueOf(-latitude));
	    lHemisphere.setText("Hemisphere South");
	    hemisphere = -1;
	  }
	  tLatitude.setEditable(false);
	  
	  for (int i = 0; i < 12; i++) {
	    irrad[i] = fixed_data.location_irradiation[(choice_location+AU_Location)*12+i];
	    tIrrad[i].setText(String.valueOf(irrad[i]));
	    tIrrad[i].setEditable(false);
	  }
	}
	if (choice_first == 2) {
	  cName.setVisible(false);
	  choice_location = 0;
	  
	  tName.setEditable(true);
	  text_location = new String("");
	  tName.setText(text_location);
	  
	  tLatitude.setEditable(true);
	  latitude = 0.0;
	  tLatitude.setText(new String("0.0"));
	  lHemisphere.setVisible(false);
	  cbNorth.setVisible(true);
	  cbSouth.setVisible(true);
	  cbNorth.setState(true);
	  hemisphere = 1;
	  
	  for (int i = 0; i < 12; i++) {
	    tIrrad[i].setEditable(true);
	    irrad[i] = 0.0;                       // TO DEBUG irrad[i] = 4.0;
	    tIrrad[i].setText(new String("0.0")); // TO DEBUG String("4.0")
	  }
	}
    }
  }
  
  private void changedLocation() {
    if (choice_location != cName.getSelectedIndex()) {
      choice_location = cName.getSelectedIndex();
      int index = choice_first * AU_Location + choice_location;
      text_location = fixed_data.location_name[index];
      tName.setText(text_location);
      
      latitude = fixed_data.location_latitude[index];
      if (latitude > 0) {
	tLatitude.setText(String.valueOf(latitude));
	cbNorth.setState(true);
	hemisphere = 1;
      } else {
	tLatitude.setText(String.valueOf(-latitude));
	cbSouth.setState(true);
	hemisphere = -1;
      }
      tLatitude.setEditable(false);
      
      for (int i = 0; i < 12; i++) {
	irrad[i] = fixed_data.location_irradiation[index*12+i];
	tIrrad[i].setText(String.valueOf(irrad[i]));
	tIrrad[i].setEditable(false);
      }
    }
  }
  
  private void changedName() {
  }
  
  private void changedLatitude() {
    try {
      latitude = Double.valueOf(tLatitude.getText()).doubleValue();
    } catch (java.lang.NumberFormatException e) {
      latitude = 0.0;
      tLatitude.setText("0.0");
    }
    if (latitude < 0.0) {
      latitude = 0.0;
      tLatitude.setText("0.0");
      Toolkit.getDefaultToolkit().beep();
    }
    if (latitude > 89.9) {
      latitude = 0.0;
      tLatitude.setText("0.0");
      Toolkit.getDefaultToolkit().beep();
    }
  }
  
  private void changedIrradiation(int i) {
    try {
	Double.valueOf(tIrrad[i].getText()).doubleValue();
    } catch (java.lang.NumberFormatException e) {
      Toolkit.getDefaultToolkit().beep();
      tIrrad[i].setText("0.0");
    }
  }
  
  private void Update() {
    String location_name = tName.getText();
    if (choice_first == 2) {
      if (cbNorth.getState())
	hemisphere = 1;
      if (cbSouth.getState())
	hemisphere = -1;
    }
    double location_latitude = (double)hemisphere 
      * Double.valueOf(tLatitude.getText()).doubleValue();
      for (int i = 0; i < 12; i++) {
	try {
	  irrad[i] = Double.valueOf(tIrrad[i].getText()).doubleValue();
	} catch (java.lang.NumberFormatException e) {
	}
      }
      controller.sim_panel.setPanel(location_name,
				    location_latitude,
				    irrad);
  }
  
  public void paint(Graphics g) {
    Dimension new_d = getSize();
    if ((d.width != new_d.width) || (d.height != new_d.height))
      setSize(size_width, size_height);  
    g.setColor(getBackground());
    g.fillRect(0, 0, d.width, d.height);
    g.draw3DRect(5, 25, d.width - 11, d.height - 45, true);
    g.draw3DRect(8, 28, d.width - 17, d.height - 49, false);   
  }                                
}





