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


/**
 * Dialog for adding appointments.
 *
 * @author Dave Knauer, SE 376.
 */ 
public class InsertDialog extends JPanel implements ActionListener {
	
	protected JComboBox day, month, year;
	protected JComboBox hour1, hour2, min1, min2, ampm1, ampm2;
	protected JLabel dayL, monthL, yearL, nameL, c1, c2, s1, s2, errorL;
	protected JTextField name;
	protected GregorianCalendar cal = new GregorianCalendar();
	
	/**
	 * Sets the GUI elements.
	 */
	public InsertDialog() {
		this.setPreferredSize(new Dimension(160,220));
		this.setLayout(null);
		//Name
		this.nameL = new JLabel("Name");
		this.nameL.setBounds(5,0,80,20);
		this.add(this.nameL);
		this.name = new JTextField();
		this.name.setBounds(5,20,80, 20);
		this.add(this.name);
		//Year
		this.yearL = new JLabel("Year");
		this.yearL.setBounds(5, 45, 60, 20);
		this.add(this.yearL);
		int curYear = cal.get(Calendar.YEAR);
		Integer years[] = new Integer[12];
		for (int i = 0; i < 12; i++) {
			years[i] = new Integer(curYear + i);
		}
		this.year = new JComboBox(years);
		this.year.setBounds(5, 65, 60, 20);
		this.year.addActionListener(this);
		this.add(this.year);
		//Month
		this.monthL = new JLabel("Month");
		this.monthL.setBounds(80, 45, 60, 20);
		this.add(this.monthL);
		int curMonth = cal.get(Calendar.MONTH);
		Integer months[] = new Integer[12];
		for (int i = 0; i < 12; i++) {
			months[i] = new Integer(1 + i);
		}		
		this.month = new JComboBox(months);
		this.month.setBounds(80, 65, 60, 20);
		this.month.setSelectedIndex(curMonth);
		this.month.addActionListener(this);
		this.add(this.month);
		//Day
		this.dayL = new JLabel("Day");
		this.dayL.setBounds(155, 45, 60, 20);
		this.add(this.dayL);
		Integer selMonth = (Integer) this.month.getSelectedItem();
		Integer selYear = (Integer) this.year.getSelectedItem();
		int maxDays = MaxDays(selMonth.intValue(),selYear.intValue());
		Integer days[] = new Integer[maxDays];
		for (int i = 0; i < maxDays; i++) {
			days[i] = new Integer(1 + i);
		}
		this.day = new JComboBox(days);
		this.day.setBounds(155, 65, 60, 20);
		if (cal.get(Calendar.DAY_OF_MONTH) <= maxDays)
			this.day.setSelectedIndex(cal.get(Calendar.DAY_OF_MONTH) - 1);
		else
			this.day.setSelectedIndex(0);
		this.add(this.day);
		//Arrays for time
		Integer hourA[] = new Integer[12];
		for (int i = 0; i < 12; i++) {
			hourA[i] = new Integer(i + 1);
		}
		String minA[] = new String[60];
		for (int i = 0; i < 60; i++) {
			if (i < 10)
				minA[i] = "0" + i;
			else 
				minA[i] = "" + i;
		}
		String ampmA[] = new String[2];
		ampmA[0] = "AM"; ampmA[1] = "PM";
		//Start Time
		this.s1 = new JLabel("Start Time");
		this.s1.setBounds(5, 100, 60, 20);
		this.add(this.s1);
		this.c1 = new JLabel(":");
		this.c1.setBounds(70, 120, 10, 20);
		this.add(this.c1);
		this.hour1 = new JComboBox(hourA);
		this.hour1.setBounds(5, 120, 60, 20);
		this.add(this.hour1);
		this.min1 = new JComboBox(minA);
		this.min1.setBounds(80, 120, 60, 20);
		this.add(this.min1);
		this.ampm1 = new JComboBox(ampmA);
		this.ampm1.setBounds(155, 120, 60, 20);
		this.add(this.ampm1);
		//Stop Time
		this.s2 = new JLabel("Stop Time");
		this.s2.setBounds(5, 155, 60, 20);
		this.add(this.s2);
		this.c2 = new JLabel(":");
		this.c2.setBounds(70, 175, 10, 20);
		this.add(this.c2);
		this.hour2 = new JComboBox(hourA);
		this.hour2.setBounds(5, 175, 60, 20);
		this.add(this.hour2);
		this.min2 = new JComboBox(minA);
		this.min2.setBounds(80, 175, 60, 20);
		this.add(this.min2);
		this.ampm2 = new JComboBox(ampmA);
		this.ampm2.setBounds(155, 175, 60, 20);
		this.add(this.ampm2);
		//Error Label
		this.errorL = new JLabel("");
		this.errorL.setBounds(5, 200, 180, 20);
		this.add(this.errorL);
	}
	
	/**
	 * Responds to GUI selections
	 *
	 * @param event An ActionEvent.
	 */ 
	public void actionPerformed(ActionEvent e) {
		if ((e.getSource() == this.year)	
				|| (e.getSource() == this.month)) {
			adjustDays();
		}
	}
	
	/**
	 * Sets the day ComboBox to the correct number of days, if the
	 * month or year changes.
	 */
	public void adjustDays() {
		this.day.removeAllItems();
		Integer selMonth = (Integer) this.month.getSelectedItem();
		Integer selYear = (Integer) this.year.getSelectedItem();
		int maxDays = MaxDays(selMonth.intValue(),selYear.intValue());
		Integer days[] = new Integer[maxDays];
		for (int i = 0; i < maxDays; i++) {
			days[i] = new Integer(1 + i);
			this.day.addItem(days[i]);
		}
		if (cal.get(Calendar.DAY_OF_MONTH) <= maxDays)
			this.day.setSelectedIndex(cal.get(Calendar.DAY_OF_MONTH) - 1);
		else
			this.day.setSelectedIndex(0);
		this.day.repaint();
	}
	
	/**
	 * Returns the max number of days for the given month/year combination.
	 *
	 * @param month The current selected month.
	 * @param year The current selected year.
	 * @return The max days for the month/year.
	 */
	private int MaxDays(int month, int year) {
		int maxDays = 0;
		switch(month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				maxDays = 31;
				break;
			case 2: 
				if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
					maxDays = 29;
				else
					maxDays = 28;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				maxDays = 30;
				break;
			default:
				maxDays = 31;
				break;
		}
		return maxDays;
	}
						
}
