/**
 * Test.java
 * @date Jan 10, 2011
 * @author ricky barrette
 * @author Twenty Codes, LLC
 */
package com.TwentyCodes.java.OrderProcessor.UI;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.TwentyCodes.java.OrderProcessor.Date;
import com.TwentyCodes.java.OrderProcessor.DatePickerListener;
import com.TwentyCodes.java.OrderProcessor.InvalidDateFormatException;
import com.toedter.calendar.JCalendar;
import com.toedter.components.JSpinField;

/**
 * a simple date picker window that allows the user to easily pick a date
 * @author ricky barrette
 */
public class DatePicker extends JFrame implements ActionListener {

	private static final long serialVersionUID = -8460535573336456875L;
	public static final int END_DATE_RESULT = 1;
	public static final int START_DATE_RESULT = 0;
	private JCalendar mCalendar;
	private JButton mOkButton;
	private JSpinField mHours;
	private JSpinField mSeconds;
	private JSpinField mMinutes;
	private DatePickerListener mListener;
	private JLabel mFailLabel;
	private int mResultCode;

	/**
	 * Creates a new Test
	 * @throws HeadlessException
	 * @author ricky barrette
	 */
	public DatePicker(int resultCode, DatePickerListener listener) {
		this.setTitle("Date Picker");
		setIconImage(new ImageIcon(getClass().getResource("/calendar_date.png")).getImage());
		
		mResultCode = resultCode;
		mListener = listener;
		
		//Time fields
		JPanel p = new JPanel();
		p.add(new JLabel("Hours:"));
		mHours = new JSpinField();
		mHours.setMinimum(0);
		mHours.setMaximum(23);
		mHours.adjustWidthToMaximumValue();
		p.add(mHours);
		p.add(new JLabel("Minutes:"));
		mMinutes = new JSpinField();
		mMinutes.setMinimum(0);
		mMinutes.setMaximum(59);
		mMinutes.adjustWidthToMaximumValue();
		p.add(mMinutes);
		p.add(new JLabel("Seconds:"));
		mSeconds = new JSpinField();
		mSeconds.setMinimum(0);
		mSeconds.setMaximum(59);
		mSeconds.adjustWidthToMaximumValue();
		p.add(mSeconds);
		this.getContentPane().add(p, BorderLayout.NORTH);
		
		//Calender
		mCalendar = new JCalendar();
		this.getContentPane().add(mCalendar, BorderLayout.CENTER);
		
		//ok button
		mOkButton = new JButton("Ok", new ImageIcon(getClass().getResource("/accept.png")));
		mOkButton.addActionListener(this);
		this.getContentPane().add(mOkButton, BorderLayout.SOUTH);
		
		//fail label
		mFailLabel = new JLabel("Invalid entry");
		mFailLabel.setVisible(false);
		p.add(mFailLabel);
		
		//if this is an end date, then set the time to the latest possible
		if(resultCode == END_DATE_RESULT){
			mHours.setValue(23);
			mMinutes.setValue(59);
			mSeconds.setValue(59);
		}
		
		pack();
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			mListener.onDatePicked(mResultCode, new Date(
					mCalendar.getYearChooser().getYear()+"-"+ (mCalendar.getMonthChooser().getMonth()+1) +"-"+ mCalendar.getDayChooser().getDay() +
					" "+ mHours.getValue()+":"+ mMinutes.getValue()+":"+ mSeconds.getValue()));
			this.dispose();
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
			mFailLabel.setVisible(true);
			pack();
		} catch (InvalidDateFormatException e1) {
			e1.printStackTrace();
			mFailLabel.setVisible(true);
			pack();
		}
	}

}