import java.awt.*;
import java.applet.Applet;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.io.*;
import java.util.*;
// import com.pconcepts.lib.Stopwatch;

//======================================================
public class CalcApplet extends Applet implements ActionListener {

	//--------------------------------------------------
//	private static final String SER_FILE = "LoanData.ser";
	private TextField 	fAmount;
	private TextField 	fInterestPercent;
	private TextField 	fYears;
	private Button 		fCalcButton;
	private Label		fResult;
	private LoanData    fLoanData;
	private String      fFile;
	private NumberFormat    fCurrencyFormat;

	//--------------------------------------------------
	public static void main(String[] args) {
		CalcApplet applet = new CalcApplet();
		applet.init();
		if (args.length == 2) {
			applet.runActionLoop(args);
		} else {
			applet.runAsApplication();
		}
	}

	//--------------------------------------------------
	protected void runActionLoop(String[] args) {
		String option0 = args[0];
		int loops = Integer.parseInt(args[1]);
		if (option0.equals("-loops")) {
			ActionEvent evt = new ActionEvent(fCalcButton, 0, "");
			System.out.println("Start Loop");
			for (int i = 0; i < loops; i++) {
				actionPerformed(evt);
			}
			System.out.println("Stop Loop");
		}
	}

	//--------------------------------------------------
	protected void runAsApplication() {
		Frame frame = new Frame("Loan Calc");
		frame.addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
		}});
		frame.add(this);
		frame.pack();
		frame.setVisible(true);
	}

	//--------------------------------------------------
	public void init() {
		fCurrencyFormat = DecimalFormat.getCurrencyInstance();
		fLoanData = new LoanData();
/*		fFile = (String)System.getProperty("user.home")
			    + (String)System.getProperty("file.separator")
				+ SER_FILE;
		System.out.println("File location: " + fFile);
		loadData();
*/
		createGUI();
	}

	//--------------------------------------------------
	public Insets insets() {
		return new Insets(5, 5, 5, 5);
	}

	//--------------------------------------------------
	protected void storeData() {
		try {
			File file = new File(fFile);
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(fLoanData);
			oos.close();
			fos.close();
		} catch (Exception e) {e.printStackTrace();}
	}

	//--------------------------------------------------
	protected void loadData() {
		try {
			File file = new File(fFile);
			if (file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				ObjectInputStream ois = new ObjectInputStream(fis);
				fLoanData = (LoanData)ois.readObject();
				ois.close();
				fis.close();
			}
		} catch (Exception e) {e.printStackTrace();}
	}

	//--------------------------------------------------
	protected void printProperties() {
		Properties props = System.getProperties();
		Enumeration enum = props.propertyNames();
		while (enum.hasMoreElements()) {
			String key = (String)enum.nextElement();
			String value = (String)props.get(key);
			System.out.println(key + ":\t" + value);
		}
	}

	//--------------------------------------------------
	protected void createGUI() {
		int rows = 4;
		int cols = 2;
		int kGap = 4;
		GridLayout layout = new GridLayout(rows, cols, kGap, kGap);
		setLayout(layout);

		setFont(new java.awt.Font("Lucida Grande", 0, 14));

 		add (new Label("Amount"));
		cols = 7;
		double amount = fLoanData.getAmount();
		fAmount = new TextField(amount +"", cols);
		add(fAmount);

		add (new Label("Interest"));
		cols = 4;
		double percent = fLoanData.getInterestPercent();
		fInterestPercent = new TextField(percent+"", cols);
		add(fInterestPercent);

		add (new Label("Years"));
		cols = 3;
		double years = fLoanData.getYears();
		fYears = new TextField(years+"", cols);
		add(fYears);

		fCalcButton = new Button("Payment =");
		fCalcButton.addActionListener(this);
		add(fCalcButton);

		fResult = new Label("Payment goes here");
		add(fResult);
	}


	//--------------------------------------------------
	protected String computePayment() {
		fLoanData.setAmount( new Double(fAmount.getText()).doubleValue() );
		fLoanData.setInterestPercent( new Double(fInterestPercent.getText()).doubleValue() );
		fLoanData.setYears( new Double(fYears.getText()).doubleValue() );
 		double payment = LoanEngine.payment(fLoanData);
		return fCurrencyFormat.format(payment);
 	}

	//--------------------------------------------------
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == fCalcButton) {
			String monthlyPayment = computePayment();
			fResult.setText(monthlyPayment + " / month");
//		    storeData();
		}
	}

	//--------------------------------------------------
}

