import java.util.ResourceBundle;
import com.sun.ebank.util.EJBGetter;
import javax.naming.InitialContext;
import javax.swing.JTextField;
import java.rmi.RemoteException;
import com.sun.ebank.ejb.customer.CustomerController;
import com.sun.ebank.ejb.customer.CustomerControllerHome;
import com.sun.ebank.ejb.exception.CustomerNotFoundException;
import com.sun.ebank.util.CustomerDetails;

public class DataModel {
//Private instance variables
  private int which; 
  private String returned;
  private BankApp frame;
  private ResourceBundle messages;
//Private bean lookup variables
  private CustomerControllerHome customerControllerHome = null;
  private static CustomerController customer = null;

//Protected instance variables
  protected String first = null, last=null, mid=null, str=null, cty=null;
  protected String st=null, zp=null, tel=null, mail=null, custID=null;

//Constructor 
  public DataModel(BankApp frame, ResourceBundle messages) {
   this.frame = frame;
   this.messages = messages;

//Look up and create CustomerController bean
    try {
      customerControllerHome = EJBGetter.getCustomerControllerHome();
      customer = customerControllerHome.create();
    } catch (Exception NamingException) {
      NamingException.printStackTrace();
    }

  }

  private String getData(JTextField component) {
    String text, trimmed;
    if(component.getText().length() > 0) {
      text = component.getText();
      trimmed = text.trim();
      return trimmed;
    } else {
      text = null;
      return text;
    }
  }

  protected int checkData(String returned, int which) {
    int i, j, k;
    this.which = which;
    this.returned=returned;

    last = getData(frame.lname);
    first = getData(frame.fname);
    mid = getData(frame.mi);
    str = getData(frame.street);
    cty = getData(frame.city);
    st = getData(frame.state);
    zp = getData(frame.zip);
    tel = getData(frame.phone);
    mail = getData(frame.e);

    frame.messlab.setText(null);
    frame.messlab2.setText(null);
    frame.messlab3.setText(null);
    frame.messlab4.setText(null);
    frame.messlab6.setText(null);

    if((last != null) && (first != null) && (str != null)
                      && (cty != null) && (st != null)) {
      i = 0;
    } else {
      frame.messlab6.setText(" " + messages.getString("MissingRequiredException"));
      i = 1;
    }
    if(frame.mi.getText().length() > 1) {
      frame.messlab2.setText(" " + messages.getString("MILimitException"));
      j = 1;
    } else {
      j = 0;
    }
    if(frame.state.getText().length() > 2) {
      frame.messlab3.setText(" " + messages.getString("StateLimitException"));
      k = 1;
    } else {
      k = 0;
    }
    if((i == 0) && (j == 0) && (k == 0)) {
      int success = writeData();
      return success; 
    } else { 
      return 1;
    } 
  }
   
  private int writeData() {
    if(which == 2){ //Update customer information
      try {
        customer.setName(last, first, mid, returned);
        customer.setAddress(str, cty, st, zp, tel, mail, returned);
        return 0;
      } catch (RemoteException ex) {
        frame.messlab.setText(" " + messages.getString("RemoteException"));
        return 1;
      } catch (CustomerNotFoundException ex) {
        frame.messlab4.setText(" " + messages.getString("CustomerException") +
		" " + returned + " " + messages.getString("NotFoundException"));
        return 1;
      }
    } 

    if(which == 1) { //Add new customer information
      try {
        custID = customer.createCustomer(last, first, mid, str, cty, st, 
			zp, tel, mail);
	return 0;
      } catch (RemoteException ex) {
        frame.messlab.setText(" " + messages.getString("RemoteException"));
        return 1;
      }
    }
    return 0;
  }

  protected void createCustInf(int which, String returned) {
    CustomerDetails details = null;
  //View Data
    if((which == 3) && (returned.length() > 0)) {
      try {
        details = customer.getDetails(returned);
        frame.createROFields(details.getFirstName(), details.getLastName(),
                details.getMiddleInitial(), details.getStreet(),
                details.getCity(), details.getState(), details.getZip(),
                details.getPhone(), details.getEmail());
      } catch (RemoteException ex) {
        frame.messlab.setText(" Remote Exception");
      } catch (CustomerNotFoundException ex) {
        frame.messlab4.setText(" " + messages.getString("CustomerException") + " " +
                returned + " " + messages.getString("NotFoundException"));
      }
    }

  //Update Data
    if((which == 2) && (returned.length() > 0)) {
      try {
        details = customer.getDetails(returned);
        frame.createEditableFields(details.getFirstName(), details.getLastName(),
                details.getMiddleInitial(), details.getStreet(),
                details.getCity(), details.getState(), details.getZip(),
                details.getPhone(), details.getEmail());
      } catch (RemoteException ex) {
        frame.messlab.setText(" Remote Exception");
      } catch (CustomerNotFoundException ex) {
        frame.messlab4.setText(" " + messages.getString("CustomerException") + " " +
                returned + " " + messages.getString("NotFoundException"));
   	frame.resetPanelTwo();
      }
    }

  //Add Data
    if(which == 1) {
      frame.createEditableFields(null, null, null, null, null,
                null, null, null, null);
    }
  }
}
