import java.awt.*;
import java.util.*;
import java.applet.*;
import Account;
import TransactionPanel;
import User;

public class AccountManager extends PicturePanel
{

int index;
int startDisplay;

Image mbg, tbg1, tbg2;

Main mainApplet;

Label checkingAccounts; 
Label accountName; 
Label owner; 
Label ir; Label balance;
Label an, o, i, b;
TextField ta[] = new TextField[5];
TextField to[] = new TextField[5];
TextField ti[] = new TextField[5];
TextField tb[] = new TextField[5];
TextField anx, ox, ix, bx;
Button nw, delete, open, rturn, ok;

Checkbox c[] = new Checkbox[6];
CheckboxGroup cg;

Button pup, pdn, up, dn;

User user;

void handleBox()
   {
	int which = getSelectedCheckbox() + startDisplay;
	
      if(which < user.accounts.size()) {
	
 Account temp = (Account)user.accounts.elementAt(which);
                anx.setText(temp.name);
                ox.setText(temp.owner);
                ix.setText((new Double(temp.interestRate)).toString());
                bx.setText((new Double(temp.balance)).toString());
	}
   }

public boolean action(Event evt, Object arg) {

      if(evt.target instanceof Checkbox)
      {
         handleBox();
         return true;
      }

	if ("New".equals(arg)) {
		anx.setText("");
		ox.setText("");
		ix.setText("");
		bx.setText(""); 

		startDisplay = user.accounts.size() - 1;
		refreshAccountList(true);
		cg.setCurrent(c[1]);		
	}

	if ("up".equals(arg)) {
	   if (startDisplay > 0)
		startDisplay--;
		refreshAccountList(true);	
		anx.setText("");
		ox.setText("");
		ix.setText("");
		bx.setText(""); 
	}

	if ("dn".equals(arg)) {
	   if (startDisplay < (user.accounts.size() - 1))
		startDisplay++;
		refreshAccountList(true);	
		anx.setText("");
		ox.setText("");
		ix.setText("");
		bx.setText(""); 
	}

	if ("pup".equals(arg)) {
		startDisplay -= 5;
		if (startDisplay < 0)
			startDisplay = 0;
		refreshAccountList(true);	
		anx.setText("");
		ox.setText("");
		ix.setText("");
		bx.setText(""); 
	}

	if ("pdn".equals(arg)) {
		startDisplay += 5;
		if (startDisplay > (user.accounts.size() - 1))
			startDisplay = user.accounts.size() - 1;
		refreshAccountList(true);	
		anx.setText("");
		ox.setText("");
		ix.setText("");
		bx.setText(""); 
	}

	if ("Delete".equals(arg)) {

	int gsc = getSelectedCheckbox();
	   if (gsc < 5) {
		try
		user.accounts.removeElementAt(gsc +
			startDisplay);	
		catch(java.lang.ArrayIndexOutOfBoundsException e);
		refreshAccountList(false);
		anx.setText(ta[gsc].getText());
		ox.setText(to[gsc].getText());
		ix.setText(ti[gsc].getText());
		bx.setText(tb[gsc].getText());  }
	} 

	if ("OK".equals(arg)) {

	int gsc = getSelectedCheckbox();
		if (gsc < 5) { 
		Account temp = new Account();
                temp.name = anx.getText();
                temp.owner = ox.getText();
                temp.interestRate = (new Double(ix.getText())).doubleValue();
                temp.balance = (new Double(bx.getText())).doubleValue();
		if (user.accounts.size() > gsc + startDisplay) {
                user.accounts.setElementAt(temp, gsc + startDisplay);
                }
                else {
                user.accounts.addElement(temp);
                }
		refreshAccountList(false);
		}

	}

	if ("Open".equals(arg)) {
	   if (getSelectedCheckbox() < 5)  {

		mainApplet.LoadTransactionManager(
(Account)user.accounts.elementAt(startDisplay+getSelectedCheckbox()));

		}
	}

	return true; 
} 


void refreshAccountList(boolean resetCheckBox) {
	int size = user.accounts.size();
	Account temp;
	if (size > 5)
		size = 5;
	for (index = 0; index < size ; index++)
	{

		c[5].setState(resetCheckBox);  // dummy checkbox

	   if (index+startDisplay < user.accounts.size()) {
		temp = (Account)user.accounts.elementAt(index+startDisplay);
		ta[index].setText(temp.name);
		to[index].setText(temp.owner);
		ti[index].setText((new Double(temp.interestRate)).toString());
		tb[index].setText((new Double(temp.balance)).toString()); }
	}


	index = user.accounts.size() - startDisplay;

	for (; index < 5; index++) {
		ta[index].setText("");
		to[index].setText("");
		ti[index].setText("");
		tb[index].setText("");

	} 

   }

// constructor

	AccountManager(Main a, Image bg, Image bg1, Image bg2,  User u)
   	{

	super(bg);

	mbg = bg;
	tbg1 = bg1;
	tbg2 = bg2;
	user = u;

	mainApplet = a;

	cg = new CheckboxGroup();	

	checkingAccounts = new Label("Checking Accounts");
	accountName = new Label("Account Name"); 
	an = new Label("Account Name"); 
	owner = new Label("Owner");
	o = new Label("Owner");
	ir = new Label("I.R.");
	i = new Label("Interest Rate");
	balance = new Label("Balance");
	b = new Label("Balance");
	top.add(checkingAccounts); 
        top.add(accountName); 
        top.add(owner); 
	top.add(ir); top.add(balance);
        


for(index = 0; index < 5; index++)
      {
         ta[index] = new TextField();
	top.add(ta[index]);
	ta[index].setEditable(false);
         to[index] = new TextField();
	top.add(to[index]);
	to[index].setEditable(false);
         ti[index] = new TextField();
	top.add(ti[index]);
	ti[index].setEditable(false);
         tb[index] = new TextField();
	top.add(tb[index]);
	tb[index].setEditable(false);

	c[index] = new Checkbox("", cg, false);
	top.add(c[index]);
	
	}

	c[5] = new Checkbox("dummy", cg, true);

	anx = new TextField(); ox= new TextField(); ix= new TextField();
	bx = new TextField();


	nw = new Button("New");
	delete = new Button("Delete");
	open = new Button("Open");
	rturn = new Button("Return");
	ok = new Button("OK");

	pup = new Button("pup");
	pdn = new Button("pdn");
	up = new Button("up");
	dn = new Button("dn");

	top.add(nw);
	top.add(delete); 
	top.add(open);
	top.add(rturn);
	bottom.add(ok);

	top.add(pup);
	top.add(pdn);
	top.add(up);
	top.add(dn);

	bottom.add(an);
	bottom.add(o); 
	bottom.add(i);
	bottom.add(b);

	bottom.add(anx);
	bottom.add(ox);
	bottom.add(ix);	
	bottom.add(bx); 

	refreshAccountList(true);

	repaint(); 

	
   	}

public int getSelectedCheckbox() {
	int found = 5;
	for (index = 0; index < 5; index++) 
		if (c[index] == cg.getCurrent())
			found = index;
	return found;
	}

 public void paint (Graphics g) {

	int textFieldHeight;
	int atextFieldWidth;

        Graphics p = top.getGraphics();
        p.drawImage(mbg, 1, 1, this);
	g.drawImage(tbg1,1,1,this);
	checkingAccounts.reshape(10,10, checkingAccounts.size().width, 
			checkingAccounts.size().height);
	accountName.reshape(30,30, accountName.size().width,
			accountName.size().height);
	owner.reshape(240,30, owner.size().width,
			owner.size().height);
	ir.reshape(390,30, ir.size().width, ir.size().height);
	balance.reshape(450,30, balance.size().width,
			balance.size().height); 

	textFieldHeight = ta[1].size().height;
	atextFieldWidth = 240-30;

	ta[0].reshape(30,50, 240-30, textFieldHeight);	
	c[0].reshape(10,53,20,c[0].size().height);
	ta[1].reshape(30,50+textFieldHeight, 240-30, textFieldHeight);	
	c[1].reshape(10,53+textFieldHeight,20,c[1].size().height);
	ta[2].reshape(30,50+2*textFieldHeight, 240-30, textFieldHeight);	
	c[2].reshape(10,53+2*textFieldHeight,20,c[2].size().height);
	ta[3].reshape(30,50+3*textFieldHeight, 240-30, textFieldHeight);	
	c[3].reshape(10,53+3*textFieldHeight,20,c[3].size().height);
	ta[4].reshape(30,50+4*textFieldHeight, 240-30, textFieldHeight);	
	c[4].reshape(10,53+4*textFieldHeight,20,c[4].size().height);
	to[0].reshape(240,50,390-240, textFieldHeight); 
	to[1].reshape(240,50+textFieldHeight,390-240, textFieldHeight); 
	to[2].reshape(240,50+2*textFieldHeight,390-240, textFieldHeight); 
	to[3].reshape(240,50+3*textFieldHeight,390-240, textFieldHeight); 
	to[4].reshape(240,50+4*textFieldHeight,390-240, textFieldHeight); 
	ti[0].reshape(390,50,450-390, textFieldHeight); 
	ti[1].reshape(390,50+textFieldHeight,450-390, textFieldHeight); 
	ti[2].reshape(390,50+2*textFieldHeight,450-390, textFieldHeight); 
	ti[3].reshape(390,50+3*textFieldHeight,450-390, textFieldHeight); 
	ti[4].reshape(390,50+4*textFieldHeight,450-390, textFieldHeight); 
	tb[0].reshape(450,50,550-470, textFieldHeight); 
	tb[1].reshape(450,50+textFieldHeight,550-470, textFieldHeight); 
	tb[2].reshape(450,50+2*textFieldHeight,550-470, textFieldHeight); 
	tb[3].reshape(450,50+3*textFieldHeight,550-470, textFieldHeight); 
	tb[4].reshape(450,50+4*textFieldHeight,550-470, textFieldHeight); 

	pup.reshape(530,50,20,20);
	up.reshape(530,90,20,20);
	dn.reshape(530,130,20,20);
	pdn.reshape(530,170,20,20); 
	

	int y = 50+5*textFieldHeight+10;

	nw.reshape(30+30,40+5*textFieldHeight+10,100,30);
	delete.reshape(160+20,40+5*textFieldHeight+10,100,30);
	open.reshape(280+30,40+5*textFieldHeight+10,100,30);
	rturn.reshape(410+20,40+5*textFieldHeight+10,100,30);

	y += 50;

	an.reshape(30,y-240,an.size().width,an.size().height);
	o.reshape(30,y+30-240,o.size().width,o.size().height);
	i.reshape(30,y+50+o.size().height-240,i.size().width,
			i.size().height);
	b.reshape(30,y+60+o.size().height+i.size().height-240,
			b.size().width, b.size().height);

	anx.reshape(150, y-240, 250, anx.size().height); 
	ox.reshape(150, y+30-240,250, ox.size().height);
	ix.reshape(150, y+50+o.size().height-240,250,ix.size().height);
	bx.reshape(150, y+60+o.size().height+i.size().height-240, 250,
			bx.size().height);

	ok.reshape(400,400-240,50,50); 
	}   

}
