package leaf;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import com.toedter.calendar.JCalendar;

public class LeafCalendarDialog extends JDialog
{
	private static final long serialVersionUID = 1L;
	public static final int ABOVE = 0;
	public static final int BELOW = 1;
	public static final int RIGHT = 0;
	public static final int LEFT = 1;

	private boolean ok = false;
	private boolean clear = false;

	private Calendar cal = null;

	private JComponent parent = null;
	
	private boolean enableClean = true;
	
	private boolean enableCancel = true;

	
	private final JCalendar calendarPanel = new JCalendar( null, null, false, false );

	/** Creates a new instance of JCalendarDialog */
	public LeafCalendarDialog(JFrame parentFrame, JComponent parent)
	{
		super(parentFrame);
		this.parent = parent;
		this.enableClean = true;
		setModal( true );
		setupComponents();
		setUndecorated( true );
		setLocationRelativeTo( parent );
		setVisible( true );
	}
	

	public LeafCalendarDialog(JFrame parentFrame, JComponent parent, boolean enableClean )
	{
		super(parentFrame);
		this.parent = parent;
		this.enableClean = enableClean;
		setModal( true );
		setupComponents();
		setUndecorated( true );
		setLocationRelativeTo( parent );
		setVisible( true );
	}
	
	public LeafCalendarDialog(JFrame parentFrame, JComponent parent, boolean enableClean, boolean enableCancel )
	{
		super(parentFrame);
		this.parent = parent;
		this.enableClean = enableClean;
		this.enableCancel = enableCancel;
		setModal( true );
		setupComponents();
		setUndecorated( true );
		setLocationRelativeTo( parent );
		setVisible( true );
	}

	private void setupComponents()
	{
		TableLayout layout = new TableLayout(new double[]{TableLayout.FILL,TableLayout.FILL,TableLayout.FILL}, new double[]{TableLayout.FILL, TableLayout.MINIMUM});

		getContentPane().setLayout( layout );

		getContentPane().add( calendarPanel, new TableLayoutConstraints(0,0,2,0));

		LeafButton okButton = new LeafButton( "OK" );
		okButton.addActionListener( new ActionListener()
		{
			public void actionPerformed( ActionEvent e )
			{
				ok = true;
				close();
			}
		} );
		LeafButton cancelarButton = new LeafButton( "Cancelar" );
		cancelarButton.setEnabled( enableCancel );
		cancelarButton.addActionListener( new ActionListener()
		{
			public void actionPerformed( ActionEvent e )
			{
				ok = false;
				close();
			}
		} );
		
		LeafButton limparButton = new LeafButton( "Limpar" );
		limparButton.setEnabled(enableClean);
		limparButton.addActionListener( new ActionListener()
		{
			public void actionPerformed( ActionEvent e )
			{
				ok = false;
				clear = true;
				close();
			}
		} );

	
		getContentPane().add( okButton, new TableLayoutConstraints(0,1) );
		getContentPane().add( cancelarButton, new TableLayoutConstraints(1,1)  );
		getContentPane().add( limparButton, new TableLayoutConstraints(2,1)  );

		setSize( 250, 250 );

		((JComponent) getContentPane()).setBorder( BorderFactory.createBevelBorder( BevelBorder.RAISED ) );
	}

	public Date getDate()
	{
		Date result = null;
		if( ok )
		{
			cal = Calendar.getInstance();
			cal.set( Calendar.HOUR_OF_DAY, 12 );
			cal = calendarPanel.getCalendar();
			cal.set( Calendar.YEAR, calendarPanel.getYearChooser().getYear() );
			cal.set( Calendar.DAY_OF_MONTH, calendarPanel.getDayChooser().getDay() );
			result = cal != null ? cal.getTime() : null;
		}
		else if( clear )
		{
			result = new Date( 0 );
		}
		return result;
	}

	public void close()
	{
		setVisible( false );
		dispose();
	}

}
