import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class BankApp extends JFrame {
//Protected instance variables
  protected static BankApp frame;
  protected JButton view, add, panel, update, OK, cancel;
  protected JPanel p1, p2;
  protected JLabel fnamelab, lnamelab, milab, streetlab, citylab, statelab;
  protected JLabel ziplab, phonelab, emaillab;
  protected JLabel messlab, messlab2, messlab3, messlab4, messlab5, messlab6;
  //Read-Only variables
  protected JTextArea fnamero, lnamero, miro, streetro, cityro, statero;
  protected JTextArea zipro, phonero, ero;
  //Editable variables
  protected JTextField fname, lname, mi, street, city, state;
  protected JTextField zip, phone, e;
  //Internationalization
  protected static ResourceBundle messages=null;

  public BankApp(String language, String country) {
  //Internationalization variables
    Locale currentLocale;
    currentLocale = new Locale(language, country);
    messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
  //Create initial UI (Panel 1)
    getContentPane().setLayout(new GridLayout(1,2));
    p1 = new JPanel();
    p1.setLayout(new GridLayout(11,1));
    p2 = new JPanel();
    p1.setBackground(Color.white);
    p2.setBackground(Color.white);
    getContentPane().add(p1);
    getContentPane().add(p2);
    view = new JButton(messages.getString("ViewButton"));
    add = new JButton(messages.getString("AddButton"));
    update = new JButton(messages.getString("UpdateButton"));
    messlab = new JLabel();
    messlab2 = new JLabel();
    messlab3 = new JLabel();
    messlab4 = new JLabel();
    messlab5 = new JLabel();
    messlab6 = new JLabel();
    p1.add(view);
    p1.add(add);
    p1.add(update);
    p1.add(new JLabel());
    p1.add(messlab);
    p1.add(messlab2);
    p1.add(messlab3);
    p1.add(messlab4);
    p1.add(messlab5);
    p1.add(messlab6);
    //Create Panel 2 buttons
    //So EventHandle constructor can add as action listeners
    OK = new JButton(messages.getString("OKButton"));
    cancel = new JButton(messages.getString("CancelButton"));

//Add functionality to close window
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent event) {
        System.exit(0);
      }
    });
  }

  protected void clearMessages() {
      messlab.setText(null);
      messlab2.setText(null);
      messlab3.setText(null);
      messlab4.setText(null);
      messlab5.setText(null);
      messlab6.setText(null);
  }

  protected void resetPanelTwo() {
    frame.clearMessages();
    frame.p2.removeAll();
    frame.p2.setLayout(new java.awt.FlowLayout());
    frame.p2.validate();
    frame.p2.repaint();
  }

  protected void createPanel2Labels() {
    fnamelab = new JLabel(messages.getString("FnameLab"));
    lnamelab = new JLabel(messages.getString("LnameLab"));
    milab = new JLabel(messages.getString("MiLab"));
    streetlab = new JLabel(messages.getString("StreetLab"));
    citylab = new JLabel(messages.getString("CityLab"));
    statelab = new JLabel(messages.getString("StateLab"));
    ziplab = new JLabel(messages.getString("ZipLab"));
    phonelab = new JLabel(messages.getString("PhoneLab"));
    emaillab = new JLabel(messages.getString("EmailLab"));
  }

  protected void createROFields(String first, String last, String mid,
        String str, String cty, String st, String zp, String tel, String mail){
    frame.p2.removeAll();
    frame.p2.setLayout(new java.awt.GridLayout(0, 2));
    frame.createPanel2Labels();
    fnamero = new JTextArea(first);
    lnamero = new JTextArea(last);
    miro = new JTextArea(mid);
    streetro = new JTextArea(str);
    cityro = new JTextArea(cty);
    statero = new JTextArea(st);
    zipro =   new JTextArea(zp);
    phonero = new JTextArea(tel);
    ero = new JTextArea(mail);
    p2.add(fnamelab);
    p2.add(fnamero);
    p2.add(lnamelab);
    p2.add(lnamero);
    p2.add(milab);
    p2.add(miro);
    p2.add(streetlab);
    p2.add(streetro);
    p2.add(citylab);
    p2.add(cityro);
    p2.add(statelab);
    p2.add(statero);
    p2.add(ziplab);
    p2.add(zipro);
    p2.add(phonelab);
    p2.add(phonero);
    p2.add(emaillab);
    p2.add(ero);
    p2.add(OK);
    p2.add(cancel);
    p2.validate();
    p2.repaint();
  }

  protected void createEditableFields(String first, String last, String mid,
        String str, String cty, String st, String zp, String tel, String mail){
    frame.p2.removeAll();
    frame.p2.setLayout(new java.awt.GridLayout(0, 2));
    frame.createPanel2Labels();
    fname = new JTextField(first);
    lname = new JTextField(last);
    mi = new JTextField(mid);
    street = new JTextField(str);
    city = new JTextField(cty);
    state = new JTextField(st);
    zip =   new JTextField(zp);
    phone = new JTextField(tel);
    e = new JTextField(mail);
    p2.add(fnamelab);
    p2.add(fname);
    p2.add(lnamelab);
    p2.add(lname);
    p2.add(milab);
    p2.add(mi);
    p2.add(streetlab);
    p2.add(street);
    p2.add(citylab);
    p2.add(city);
    p2.add(statelab);
    p2.add(state);
    p2.add(ziplab);
    p2.add(zip);
    p2.add(phonelab);
    p2.add(phone);
    p2.add(emaillab);
    p2.add(e);
    p2.add(OK);
    p2.add(cancel);
    p2.validate();
    p2.repaint();
  }

  public static void main(String args[]) {
    String language, country;
    if(args.length != 2) {
      language = new String("en");
      country = new String("US");
    } else {
      language = new String(args[0]);
      country = new String(args[1]);
    }
    frame = new BankApp(language, country);
    frame.setTitle("Banking Administration Client");
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	System.exit(0);
      }
    };
    frame.addWindowListener(l);
    frame.pack();
    frame.setVisible(true);
//Create event handling and data modeling objects
    EventHandle ehandle = new EventHandle(frame, messages);
    // Wait on anonymous object to prevent main from completing
    //Remove this code after bug is fixed
    Object o = new Object();
    synchronized(o) {
      try {
        o.wait();
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
    //Remove to here
  }
}
