package siprp.medicina.processo.ui;

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

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;

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

	private Map<Object, Object> map = null;

	private Object selected = null;

	/** Creates a new instance of JCalendarDialog */
	public LeafOptionDialog(JFrame parentFrame, JComponent parent, Map<Object, Object> map)
	{
		super( parentFrame );
		this.map = map == null ? new HashMap<Object, Object>() : map;
		setModal( true );
		setupComponents();
		setUndecorated( true );
		setLocationRelativeTo( parent );
		setVisible( true );
	}

	private void setupComponents()
	{
		Set<Object> keySet = map.keySet();
		double[] cols = new double[] {
			TableLayout.MINIMUM
		};
		double[] rows = new double[keySet.size() + 1];
		for( int i = 0; i < keySet.size() + 1; ++i )
		{
			rows[i] = TableLayout.MINIMUM;
		}
		TableLayout layout = new TableLayout( cols, rows );
		layout.setVGap( 3 );
		setContentPane( new LeafGradientPanel() );
		getContentPane().setLayout( layout );
		Iterator<Object> iterator = keySet.iterator();
		Object current = iterator.next();
		for( int i = 0; i < keySet.size() && iterator.hasNext(); ++i, current = iterator.next() )
		{
			LeafInputField<Object> component = new LeafInputField<Object>();
			component.setObject( map.get( current ) );
			getContentPane().add( component, new TableLayoutConstraints( 0, i ) );
			addListenerToComponent( component );
		}
		LeafInputField<String> cancelButton = new LeafInputField<String>();
		cancelButton.setObject( "Cancelar" );
		getContentPane().add( cancelButton, new TableLayoutConstraints( 0, keySet.size() ) );
		addListenerToComponent( cancelButton );
		((JComponent) getContentPane()).setBorder( BorderFactory.createRaisedBevelBorder() );
		setSize( layout.preferredLayoutSize( this.getContentPane() ) );
	}



	private Object getKeyForValue( Object value )
	{
		if( value != null && map.containsValue( value ) )
		{
			for( Object key : map.keySet() )
			{
				if( map.get( key ).equals( value ) )
				{
					return key;
				}
			}
		}
		return null;
	}

	private void addListenerToComponent( JComponent component )
	{
		component.addMouseListener( new MouseListener()
		{

			@Override
			public void mouseReleased( MouseEvent e )
			{
			}

			@Override
			public void mousePressed( MouseEvent e )
			{
			}

			@Override
			public void mouseExited( MouseEvent e )
			{
			}

			@Override
			public void mouseEntered( MouseEvent e )
			{
			}

			@Override
			public void mouseClicked( MouseEvent e )
			{
				if( e.getSource() instanceof LeafInputField )
				{
					Object value = ((LeafInputField<Object>) e.getSource()).getObject();
					if( value != null )
					{
						selected = getKeyForValue( value );
					}
				}
				close();
			}

		} );
	}

	public Object getOption()
	{
		return selected;
	}

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

}
