/**
 * 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 javax.swing.JTextField;

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

/**
 * 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;
	private JCalendar mCalendar;
	private JButton mOkButton;
	private JTextField mHours;
	private JTextField mSeconds;
	private JTextField 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 JTextField();
		mHours.setColumns(10);
		p.add(mHours);
		p.add(new JLabel("Minutes:"));
		mMinutes = new JTextField();
		mMinutes.setColumns(10);
		p.add(mMinutes);
		p.add(new JLabel("Seconds:"));
		mSeconds = new JTextField();
		mSeconds.setColumns(10);
		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);
		
		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() +
					" "+ Integer.parseInt(mHours.getText())+":"+ Integer.parseInt(mMinutes.getText())+":"+ Integer.parseInt(mSeconds.getText())));
			this.dispose();
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
			mFailLabel.setVisible(true);
			pack();
		} catch (InvalidDateFormatException e1) {
			e1.printStackTrace();
			mFailLabel.setVisible(true);
			pack();
		}
	}

}