import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

public class CompanyApplication extends Frame implements CompanyTransactionProxy, ActionListener {
	private CardCompany company;
	private CompanyPresentation presentation = new CompanyPresentation(this);
	FileDialog chooseSaveFile = new FileDialog(this, "Save Company", FileDialog.SAVE);
	FileDialog chooseLoadFile = new FileDialog(this, "Load Company", FileDialog.LOAD);
	CompanyInfo infoBox = new CompanyInfo(this);
	MenuItem newCompany = new MenuItem("New");
	MenuItem save = new MenuItem("Save");
	MenuItem load = new MenuItem("Load");
	MenuItem quit = new MenuItem("Quit");
	MenuItem info = new MenuItem("Info");
	AreYouSure areYouSure = new AreYouSure(this);
	boolean isChanged = false;

	// The Un-name initialization method
	{
		setLayout(new FlowLayout());
		add(presentation);
 
		MenuBar mb = new MenuBar();

		Menu file = new Menu("File");
			file.add(newCompany);
			newCompany.addActionListener(this);
			file.add(save);
			save.addActionListener(this);
			file.add(load);
			load.addActionListener(this);
			file.add(quit);
			quit.addActionListener(this);
		mb.add(file);
		
		Menu companyInfo = new Menu("Company");
			companyInfo.add(info);
			info.addActionListener(this);
		mb.add(companyInfo);
		
		setMenuBar(mb);
		addWindowListener(new ExitCompanyApplication(this));
		setSize(500,500);
	}

	public CompanyApplication(String fname) {		
		try {
			company = CardCompany.load(fname);
			resetTitle(company.getName());
		}
		catch (IOException e) {
			System.out.println("Unable to load file: " + fname);
		}	
	}
			
	public CompanyApplication() { 
		resetCompany();
	}

	private void resetCompany() {
		resetTitle("<New Company>");
		company = new CardCompany("<New Company>", 312000);
	}
	
	private void resetTitle(String name) {
		setTitle("CardApplication - " + name);
	}
	
	public Card issueCard(String name) {
		isChanged = true;
		return company.issueCard(name);
	}

	public boolean cancelCard(int number) {
		isChanged = true;
		return company.cancelCard(number);
	}

	public Card purchase(int number, String item, double amount) {
		isChanged = true;
		return company.purchase(number, item, amount);
	}

	public Card payment(int number, double amount) {
		isChanged = true;
		return company.payment(number, amount);
	}

	public Card findCard(String name) {
		return company.findCard(name);
	}

	public Card findCard(int number) {
		return company.findCard(number);
	}

	boolean okToChangeCompany() {
		if (isChanged) {
			areYouSure.setVisible(true);
			return areYouSure.isSure();
		}
		else
			return true;
	}
	
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == newCompany)
			resetCompany();
			
		if (evt.getSource() == save) {
			chooseSaveFile.setVisible(true);
			String dir = chooseSaveFile.getDirectory();
			String file = chooseSaveFile.getFile();
			
			if (file != null) {
				try { 
					company.save(dir + file);
					isChanged = false;
				}
				catch(IOException e) { System.out.println("Error saving: " + dir + file); }
			}
		}		
		
		if (evt.getSource() == load) {
			if (!okToChangeCompany())
				return;
				
			chooseLoadFile.setVisible(true);
			String dir = chooseLoadFile.getDirectory();
			String file = chooseLoadFile.getFile();
			
			if (file != null) {
				try { company = CardCompany.load(dir + file); }
				catch(IOException e) { System.out.println("Error loading: " + dir + file); }
			}
		}			

		if (evt.getSource() == quit) {
			if (!okToChangeCompany())
				return;

			System.exit(0);
		}

		if (evt.getSource() == info) {
			infoBox.setCompany(company);
			String oldName = company.getName();
			infoBox.setVisible(true);
			if (!oldName.equals(company.getName()) );
				isChanged = true;
			resetTitle(company.getName());
		}
	}
	
	public static void main(String arg[]) {
		CompanyApplication app;
		
		if (arg.length == 0)
			app = new CompanyApplication();
		else
			app = new CompanyApplication(arg[0]);
			
		app.setVisible(true);
	}
}

class AreYouSure extends Dialog implements ActionListener {
	boolean sure = false;
	Button ok = new Button("OK");
	Button cancel = new Button("Cancel");
	
	AreYouSure(Frame f) {
		super(f, "Company Changed", true);
		Panel options = new Panel(new FlowLayout());
			options.add(ok);
			ok.addActionListener(this);
			options.add(cancel);
			cancel.addActionListener(this);
		this.setLayout(new BorderLayout());
		this.add("North", new Label("Company Changed and you haven't saved.  Are you sure you want to continue?"));
		this.add("South", options);
		pack();
	}
	
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == ok)
			sure = true;
		else 
			sure = false;
			
		setVisible(false);
	}
	
	boolean isSure() { return sure; }
}

class ExitCompanyApplication extends WindowAdapter {
	CompanyApplication app;
	
	ExitCompanyApplication(CompanyApplication app) { this.app = app; }
	
	public void windowClosing(WindowEvent evt) {
		if(app.okToChangeCompany())
			System.exit(0);
	}
}
		
