// Oefening CreateSequentialFile, D&D 5, blz 851,..

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

import be.hogent.h17.H17_BankUI;
import be.hogent.h17.H17_AccountRecord;

public class H17_CreateSequetialFile extends JFrame {
	
	private ObjectOutputStream output;
	private H17_BankUI userInterface;
	private JButton enterButton, openButton;
	
	public H17_CreateSequetialFile() {
		super ("Aanmaken van een sequentieel bestand van Objects");
		
		userInterface = new H17_BankUI(4);
		getContentPane().add(userInterface, BorderLayout.CENTER);
		
		openButton = userInterface.getDoTask1Button();
		openButton.setText ("Opslaan in bestand...");

	
		openButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					openFile();
				}
			}
		);
		
		enterButton = userInterface.getDoTask2Button();
		enterButton.setText("Record Opslaan");
		enterButton.setEnabled(false);
		
		enterButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					addRecord();
				}
			}
		);
		
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent event) {
					if (output != null)
						addRecord();
					
					closeFile();
				}
			}
		);
		
		setSize(300,200);
		setVisible(true);
		
	}
	
	public void openFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
		
		int result = fileChooser.showSaveDialog(this);
		
		if (result == JFileChooser.CANCEL_OPTION)
			return;
		
		File fileName = fileChooser.getSelectedFile();
		
		if (fileName == null || fileName.getName().equals(""))
			JOptionPane.showMessageDialog (this, "Ongeldige bestandsnaam", "Ongeldige bestandsnaam",JOptionPane.ERROR_MESSAGE);
		else {
			
			try {
				output = new ObjectOutputStream(new FileOutputStream(fileName));
				
				openButton.setEnabled(false);
				enterButton.setEnabled(true);
			}
			catch (IOException ioException) {
				JOptionPane.showMessageDialog (this, "Fout bij het openen", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
	
	private void closeFile() {
		try {
			output.close();
			System.exit(0);
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog(this, "Fout bij het sluiten", "Error", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
	
	public void addRecord() {
		int  accountNumber = 0;
		H17_AccountRecord record;
		String fieldValues[] = userInterface.getFieldValues();
		
		if (!fieldValues[H17_BankUI.ACCOUNT].equals("")) {
			
			try {
				accountNumber = Integer.parseInt(fieldValues[H17_BankUI.ACCOUNT]);
				
				if (accountNumber > 0) {
					record = new H17_AccountRecord(
						accountNumber,
						fieldValues[H17_BankUI.FIRSTNAME],
						fieldValues[H17_BankUI.LASTNAME],
						Double.parseDouble(fieldValues[H17_BankUI.BALANCE])
					);
					
					output.writeObject(record);
					output.flush();
				} else {
					JOptionPane.showMessageDialog(this, "Accountnumber moet groter dan 0 zijn","Fout accountnumber",JOptionPane.ERROR_MESSAGE);
				}
				
				userInterface.clearFields();
			}
			catch (NumberFormatException formatException) {
				JOptionPane.showMessageDialog(this, "Fout accountnummer of balance","Error",JOptionPane.ERROR_MESSAGE);
			}
			
			catch (IOException ioException) {
				JOptionPane.showMessageDialog(this, "Fout tijdens schrijven naar bestand","IO Exception", JOptionPane.ERROR_MESSAGE);
				closeFile();
			}
		}
	}
	
	
	public static void main( String args[]) {
		new H17_CreateSequetialFile();
	}
	
}
