package leaf;

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

import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class LeafTextDialog extends JDialog
{
	private static final long serialVersionUID = 1L;

	private static final Dimension buttonSize = new Dimension( 30, 20 );

	private static final Dimension textSize = new Dimension( 200, 20 );

	private static final Dimension expandedTextSize = new Dimension( 300, 200 );

	private String text = null;

	private String defaultText = null;

	private boolean expanded = false;

	private final JTextArea textArea = new JTextArea();
	private final LeafButton expandButton = new LeafButton( "+" );
	private final LeafButton okButton = new LeafButton( "Ok" );
	private final LeafButton cancelButton = new LeafButton( "X" );

	private TableLayout layout = null;
	private TableLayout expandedLayout = null;

	/** Creates a new instance of JCalendarDialog */
	public LeafTextDialog(JFrame parentFrame, JComponent parent, String defaultText, boolean expanded)
	{
		super( parentFrame );
		this.defaultText = defaultText == null ? "" : defaultText;
		this.text = this.defaultText;
		setModal( true );
		setContentPane( new LeafGradientPanel() );
		textArea.setText( text );
		expandButton.setPreferredSize( buttonSize );
		cancelButton.setPreferredSize( buttonSize );
		okButton.setPreferredSize( buttonSize );
		setupLayout();
		setUndecorated( true );
		setLocationRelativeTo( parent );
		this.expanded = expanded;
		expand( expanded );
		addListeners();
		setVisible( true );
	}

	private void setupLayout()
	{
		double[] cols = new double[] {
				TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED
		};
		double[] rows = new double[] {
			TableLayout.PREFERRED
		};
		layout = new TableLayout( cols, rows );
		
		cols = new double[] {
				TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED
		};
		rows = new double[] {
			TableLayout.PREFERRED, TableLayout.FILL
		};
		expandedLayout = new TableLayout( cols, rows );
	}
	
	private void placeComponents(boolean expand)
	{
		if(expand)
		{
			getContentPane().add( expandButton, new TableLayoutConstraints( 0, 0 ) );
			getContentPane().add( new JPanel(), new TableLayoutConstraints( 0, 1 ) );
			getContentPane().add( textArea, new TableLayoutConstraints( 1, 0,1,1 ) );
			getContentPane().add( okButton, new TableLayoutConstraints( 2, 0 ) );
			getContentPane().add( cancelButton, new TableLayoutConstraints( 3, 0 ) );
			getContentPane().add( new JPanel(), new TableLayoutConstraints( 2, 1,3,1 ) );
		}
		else
		{
			getContentPane().add( expandButton, new TableLayoutConstraints( 0, 0 ) );
			getContentPane().add( textArea, new TableLayoutConstraints( 1, 0 ) );
			getContentPane().add( okButton, new TableLayoutConstraints( 2, 0 ) );
			getContentPane().add( cancelButton, new TableLayoutConstraints( 3, 0 ) );
		}
		
		((JComponent) getContentPane()).setBorder( BorderFactory.createRaisedBevelBorder() );
		
		setSize( expand ? expandedLayout.preferredLayoutSize( this.getContentPane() ) : layout.preferredLayoutSize( this.getContentPane() ) );
	}

	private void setupComponents(boolean expand)
	{
		getContentPane().setLayout( expand ? expandedLayout : layout);
		textArea.setPreferredSize( expand ? expandedTextSize : textSize );
		expandButton.setText( expand ? "-" : "+" );
		placeComponents(expand);
	}

	private void expand( boolean expand )
	{
		setupComponents(expand);
	}

	private void addListeners()
	{
		expandButton.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				expanded = !expanded;
				expand( expanded );
			}
		} );

		okButton.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				text = textArea.getText();
				close();
			}
		} );

		cancelButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent e )
			{
				text = defaultText;
				close();
			}

		} );
	}

	public String getText()
	{
		return text;
	}

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

}
