// Oefening CreditInquiry, D&D 5, blz 861,..

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

import be.hogent.h17.H17_AccountRecord;


public class H17_CreditInquiry extends JFrame {
	
	private JTextArea recordDisplayArea;
	private JButton openButton, creditButton, debitButton, zeroButton;
	private JPanel buttonPanel;
	
	private ObjectInputStream input;
	private FileInputStream fileInput;
	private File fileName;
	private String accountType;
	
	static private DecimalFormat twoDigits = new DecimalFormat("0.00");
	
	public H17_CreditInquiry() {
		
		super ("Credit Inquiry programma");
		
		Container container = getContentPane();
		
		buttonPanel = new JPanel();
		
		openButton = new JButton ("Bestand Openen");
		buttonPanel.add(openButton);
		
		openButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					openFile();
				}
			}
		);
		
		creditButton = new JButton("Credit balansen");
		buttonPanel.add(creditButton);
		creditButton.addActionListener(new ButtonHandler());
		
		debitButton = new JButton("Debit balansen");
		buttonPanel.add(debitButton);
		debitButton.addActionListener(new ButtonHandler());
		
		zeroButton = new JButton("Nul balansen");
		buttonPanel.add(zeroButton);
		zeroButton.addActionListener(new ButtonHandler());
		
		recordDisplayArea = new JTextArea();
		JScrollPane scroller = new JScrollPane(recordDisplayArea);
		
		container.add(scroller, BorderLayout.CENTER);
		container.add(buttonPanel, BorderLayout.SOUTH);
		
		creditButton.setEnabled(false);
		debitButton.setEnabled(false);
		zeroButton.setEnabled(false);
		
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent event) {
					closeFile();
					System.exit(0);
				}
			}
		);
		
		pack();
		setSize(600,250);
		setVisible(true);

	}
	
	public void openFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
		
		int result = fileChooser.showOpenDialog(this);
		
		if (result == JFileChooser.CANCEL_OPTION)
			return;
		
		fileName = fileChooser.getSelectedFile();
		
		if (fileName == null || fileName.getName().equals(""))
			JOptionPane.showMessageDialog (this, "Ongeldige bestandsnaam", "Ongeldige bestandsnaam",JOptionPane.ERROR_MESSAGE);
			
		try {
			if (input != null)
				input.close();
			
			fileInput = new FileInputStream (fileName);
			input = new ObjectInputStream(fileInput);
			openButton.setEnabled(false);
			creditButton.setEnabled(true);
			debitButton.setEnabled(true);
			zeroButton.setEnabled(true);
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog (this, "Fout bij het openen", "Error",JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void closeFile() {
		try {
			if (input != null)
				input.close();
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog(this, "Fout bij het sluiten", "Error", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
	
	
	public void readRecords() {
		
		H17_AccountRecord record;
		
		try {
			
			if (input != null)
				input.close();
			
			fileInput = new FileInputStream (fileName);
			input = new ObjectInputStream (fileInput);
			
			recordDisplayArea.setText("De accounts zijn:\n");
			
			while (true) {
				record = (H17_AccountRecord) input.readObject();
				
				if (shouldDisplay(record.getBalance()))
					recordDisplayArea.append(
						record.getAccount() + "\t" +
						record.getFirstName() + "\t" +
						record.getLastName() + "\t" +
						twoDigits.format(record.getBalance()) + "\n");
					
			}
			
		}
		catch (EOFException endOfFileException) {
			closeFile();
		}
		catch (ClassNotFoundException classNotFoundException) {
			JOptionPane.showMessageDialog(this, "Fout bij het maken van object","Class niet gevonden",JOptionPane.ERROR_MESSAGE);
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog(this, "Fout bij het lezen van bestand","Leesfout",JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private boolean shouldDisplay(double balance) {
		if (accountType.equals("Credit balansen") && balance < 0)
			return true;
		
		else if (accountType.equals("Debit balansen") && balance > 0)
			return true;
		
		else if (accountType.equals("Nul balansen") && balance == 0)
			return true;
		
		return false;
	}


	private class ButtonHandler implements ActionListener {
		public void actionPerformed (ActionEvent event) {
			accountType = event.getActionCommand();
			readRecords();
		}
	}
	
	public static void main (String args[]) {
		new H17_CreditInquiry();
	}
	
}
