/**
 * RoseyCalendar.java 1.0 01/16/2004
 *
 */

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

/**
 * The frame which serves as the primary object for this project:
 * RoseyCalendar.
 *
 * @author Dave Knauer, SE 376.
 */ 
public class RoseyCalendar extends JFrame implements ActionListener {
	
	private JPanel mainPanel;
	private JButton insertButton, deleteButton, changeButton; 
	private JButton viewButton, limitButton;
	private JLabel nameL, dateL, startL, stopL;
	private Appointments apps;
	protected static Vector appointments;
	
	
	
		
	protected JList displayList;
	private JScrollPane scrollPane;
	
	/**
	 * Create a JFrame for the project:
	 * RoseyCalendar.
	 */ 
	public RoseyCalendar() {
		Container pane = this.getContentPane();
		pane.setLayout(null);
		
		this.mainPanel = new JPanel();
		this.mainPanel.setLayout(null);
		this.mainPanel.setBounds(0, 0, 600, 200);
		pane.add(this.mainPanel);	
		
		this.insertButton = new JButton("Insert Appointment");
		this.insertButton.setBounds(5, 5, 160, 30);
		this.insertButton.addActionListener(this);
		this.mainPanel.add(this.insertButton);
		
		this.deleteButton = new JButton("Delete Appointment");
		this.deleteButton.setBounds(5, 40, 160, 30);
		this.deleteButton.addActionListener(this);
		this.mainPanel.add(this.deleteButton);
		
		this.changeButton = new JButton("Delete Appointment");
		this.changeButton.setBounds(5, 75, 160, 30);
		this.changeButton.addActionListener(this);
		this.mainPanel.add(this.changeButton);
		
		this.limitButton = new JButton("Limit View");
		this.limitButton.setBounds(5, 110, 160, 30);
		this.limitButton.addActionListener(this);
		this.mainPanel.add(this.limitButton);
		
		this.viewButton = new JButton("View All");
		this.viewButton.setBounds(5, 145, 160, 30);
		this.viewButton.addActionListener(this);
		this.mainPanel.add(this.viewButton);
		
		this.nameL = new JLabel("Name");
		this.nameL.setBounds(170, 5, 80, 20);
		this.mainPanel.add(this.nameL);
		
		this.dateL = new JLabel("Date");
		this.dateL.setBounds(350, 5, 80, 20);
		this.mainPanel.add(this.dateL);
		
		this.startL = new JLabel("Start Time");
		this.startL.setBounds(430, 5, 80, 20);
		this.mainPanel.add(this.startL);
		
		this.stopL = new JLabel("Stop Time");
		this.stopL.setBounds(510, 5, 80, 20);
		this.mainPanel.add(this.startL);
		
		appointments = new Vector();
		this.displayList = new JList();
		this.apps = new Appointments(this.mainPanel, 
									 appointments, 
									 this.displayList);
		load();
		this.displayList.setFont(new Font("Courier",Font.PLAIN,12));	
		this.scrollPane = new JScrollPane(this.displayList);
		this.scrollPane.setWheelScrollingEnabled(true);	
		this.scrollPane.setBounds(170,25,400,190);
		this.mainPanel.add(this.scrollPane);
		this.displayList.setListData(appointments);
		this.mainPanel.updateUI();
		
	}
	
	/**
	 * Captures button presses
	 *
	 * @param e The button press event.
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.insertButton) {
			this.apps.insert();
			this.displayList.setListData(appointments);
			this.mainPanel.updateUI();
		}
		if (e.getSource() == this.deleteButton) {
			int curSel = this.displayList.getSelectedIndex();
			this.appointments.remove(curSel);
			this.mainPanel.updateUI();
		}
		if (e.getSource() == this.changeButton) {
			if (appointments.size() > 0)
				this.apps.change();
			this.mainPanel.updateUI();
		}
		if (e.getSource() == this.limitButton) {
			this.apps.limitView();
		}
		if (e.getSource() == this.viewButton) {
			this.displayList.setListData(appointments);
		}
	}
	
	/**
	 * Loads the appointments from file
	 */
	public void load() {
		try {
			File appointmentFile = new File("appointments.txt");
			appointmentFile.createNewFile();
			BufferedReader readFile = new BufferedReader(
								  new FileReader(appointmentFile));
			String currentLine = readFile.readLine();
			while (currentLine != null) {
				ScheduleEvent tempEvent;
				StringTokenizer st = new StringTokenizer(currentLine,"*");
				String date = st.nextToken();
				String name = st.nextToken();
				String start = st.nextToken();
				String stop = st.nextToken();
				Integer year = new Integer(st.nextToken());
				Integer month = new Integer(st.nextToken());
				Integer day = new Integer(st.nextToken());
				tempEvent = new ScheduleEvent(name, date, start, stop,
											  year, month, day);
				appointments.add(tempEvent);
				currentLine = readFile.readLine();
			}
		} catch (Exception e) {
			System.out.println("Error loading appointments: " + e.getMessage());
		}
	}
	
	/**
	 * Saves the appointments to file on closing of the app.
	 */
	public static void save() {
		try {
			File appointmentFile = new File("appointments.txt");
			BufferedWriter fileOut = new BufferedWriter(
									 new FileWriter(appointmentFile));
			int numItems = appointments.size();
			for (int i = 0; i < numItems; i++) {
				ScheduleEvent currentEvent = (ScheduleEvent) appointments.get(i);
				fileOut.write(currentEvent.getDate() + "*");
				fileOut.write(currentEvent.getName() + "*");
				fileOut.write(currentEvent.getStopTime() + "*");
				fileOut.write(currentEvent.getStopTime() + "*");
				fileOut.write(currentEvent.year + "*");
				fileOut.write(currentEvent.month + "*");
				fileOut.write(currentEvent.day + "");
				fileOut.newLine();
			}
			fileOut.close();
		} catch (Exception e) {
			System.out.println("Error saving appointments: " + e.getMessage());
		}
	}
	
	/**
	 * The application starts here in MAIN;
	 * it creates a new JFrame for the project and makes it visible.
	 *
	 * @param args  Array of command-line arguments
	 */ 
	public static void main(String args[]) {
		
		System.out.println("Starting RoseyCalendar...");
		
		RoseyCalendar mainFrame = new RoseyCalendar();
		mainFrame.setResizable(false);
		mainFrame.addWindowListener(new WindowListener() {
			public void windowActivated(WindowEvent e) {}
 			public void windowClosed(WindowEvent e) {}
 			public void windowClosing(WindowEvent e) {
 				save();
 			} 
 			public void windowDeactivated(WindowEvent e) {}
 			public void windowDeiconified(WindowEvent e) {}
 			public void windowIconified(WindowEvent e) {} 
 			public void windowOpened(WindowEvent e) {}
		});
		mainFrame.setSize(600, 240);
		mainFrame.setTitle("RoseyCalendar");
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
