// Oefening WriteRandomFile, D&D 5, blz 875,..

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

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

public class H17_WriteRandomFile extends JFrame {
	
	private RandomAccessFile output;
	private H17_BankUI userInterface;
	private JButton enterButton, openButton;
	
	private static final int NUMBER_RECORDS=100;
	
	public H17_WriteRandomFile() {
		super ("Schrijven naar een Random Access bestand");
		
		userInterface = new H17_BankUI(4);
		getContentPane().add(userInterface, BorderLayout.CENTER);
		
		openButton = userInterface.getDoTask1Button();
		openButton.setText("Open...");
		
		openButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					openFile();
				}
			}
		);
		
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent event) {
					if (output != null)
						addRecord();
					
					closeFile();
				}
			}
		);
		
		enterButton = userInterface.getDoTask2Button();
		enterButton.setText("Schrijven");
		enterButton.setEnabled(false);
		
		enterButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					addRecord();
				}
			}
		);
		
		setSize(300,150);
		setVisible(true);
	
	}
	
	private void openFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		
		int result = fileChooser.showOpenDialog(this);
		
		if (result == JFileChooser.CANCEL_OPTION)
			return;
		
		File fileName = fileChooser.getSelectedFile();
		
		if (fileName == null || fileName.getName().equals(""))
			JOptionPane.showMessageDialog(this, "Ongeldige bestandsnaam","Error",JOptionPane.ERROR_MESSAGE);
		else {
			try {
				output = new RandomAccessFile(fileName, "rw");
				enterButton.setEnabled(true);
				openButton.setEnabled(false);
			}
			catch (IOException ioException) {
				JOptionPane.showMessageDialog(this, "Het gekozen bestand bestaat niet","Error",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private void closeFile() {
		try {
			if (output != null)
				output.close();
			
			System.exit(0);
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog(this, "Fout bij het sluiten","Error",JOptionPane.ERROR_MESSAGE);
			
			System.exit(1);
		}
	}
	
	private void addRecord() {
		String  fields[] = userInterface.getFieldValues();
		
		if (! fields[H17_BankUI.ACCOUNT].equals("")) {
			
			try {
				int accountNumber = Integer.parseInt(fields[H17_BankUI.ACCOUNT]);
				
				if (accountNumber > 0 && accountNumber <= NUMBER_RECORDS) {
					H17_RandomAccessAccountRecord record = new H17_RandomAccessAccountRecord();
					
					record.setAccount(accountNumber);
					record.setFirstName(fields[H17_BankUI.FIRSTNAME]);
					record.setLastName(fields[H17_BankUI.LASTNAME]);
					record.setBalance(Double.parseDouble(fields[H17_BankUI.BALANCE]));
					
					output.seek((accountNumber -1) * H17_RandomAccessAccountRecord.SIZE);
					record.write(output);
				}
				else {
					JOptionPane.showMessageDialog(this, "Account moet tussen 1 en 100 zijn","Error",JOptionPane.ERROR_MESSAGE);
				}
				
				userInterface.clearFields();
			}
			catch(NumberFormatException formatException) {
				JOptionPane.showMessageDialog(this, "Fout accountnr of balans","Error",JOptionPane.ERROR_MESSAGE);
			}
			catch(IOException ioException) {
				JOptionPane.showMessageDialog(this, "Fout bij het schrijven naar bestand","Error",JOptionPane.ERROR_MESSAGE);
				
				closeFile();
			}
		}
	}
	
	public static void main (String args[]) {
		new H17_WriteRandomFile();
	}
}
