package siprp.higiene.marcacoes;

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

import java.util.Hashtable;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import siprp.data.outer.MarcacoesEstabelecimentoData;

import com.evolute.utils.Singleton;
import com.evolute.utils.data.IDObject;
import com.evolute.utils.data.MappableObject;
import com.evolute.utils.data.PropertyObject;
import com.evolute.utils.dataui.ComponentController;
import com.evolute.utils.dataui.ComponentsHashtable;
import com.evolute.utils.dataui.ControllableComponent;
import com.evolute.utils.dataui.DataClassAware;
import com.evolute.utils.db.DBManager;
import com.evolute.utils.db.SQLExecuter;
import com.evolute.utils.ui.calendar.JCalendarPanel;
import com.evolute.utils.ui.panel.RadioButtonFixedPanel;
import com.evolute.utils.ui.text.StateTextArea;

public class MarcacaoVisitaHSTPanel
	extends JPanel
	implements ControllableComponent, DataClassAware
{

	private static final long serialVersionUID = 1L;
	private MarcacoesEstabelecimentoData marcacaoEstabelecimentosData;
	private final ComponentsHashtable components = new ComponentsHashtable();

	protected final SQLExecuter EXECUTER;

	private final JLabel dataLabel = new JLabel( "Data da visita", JLabel.RIGHT );
	private final JCalendarPanel dataPanel = new JCalendarPanel( null );
	private final JButton emailButton = new JButton( "Enviar Email" );
	private final JLabel dataFichaLabel = new JLabel( "Data do Relat\u00f3rio(externo)", JLabel.RIGHT );
	private final JCalendarPanel dataFichaPanel = new JCalendarPanel( null );
	private final JLabel tecnicoLabel = new JLabel( "T\u00e9cnico", JLabel.RIGHT );
	private final JLabel tecnicoSuperiorLabel = new JLabel( "T\u00e9cnico Superior", JLabel.RIGHT );
	private final JComboBox tecnicoList = new JComboBox();
	private final JComboBox tecnicoSuperiorList = new JComboBox();
	private RadioButtonFixedPanel estadoList;
	private final JScrollPane observacoesTextScroll = new JScrollPane();
	private final StateTextArea observacoesText = new StateTextArea();

	public MarcacaoVisitaHSTPanel()
		throws Exception
	{
		DBManager dbm = ( DBManager )Singleton.getInstance( Singleton.DEFAULT_DBMANAGER );
		if( dbm != null )
		{
			EXECUTER = ( SQLExecuter )dbm.getSharedExecuter( this );
		}
		else
		{
			EXECUTER = null;
		}
		setupComponents();
		setupComponentsHashtable();
		place();
	}

	public void setupComponents()
		throws Exception
	{
//		emailButton.setMinimumSize( new Dimension( 300, 20 ) );
//		emailButton.setPreferredSize( new Dimension( 200, 20 ) );

		tecnicoList.addItem( new MappableObject( new Integer( -1 ), "" ) );
		tecnicoList.setSelectedIndex( 0 );
		
		tecnicoSuperiorList.addItem( new MappableObject( new Integer( -1 ), "" ) );
		tecnicoSuperiorList.setSelectedIndex( 0 );

		IDObject estadoListOptions[] = new IDObject[]{
			new MappableObject( new Integer( 0 ), "Por realizar" ),
			new MappableObject( new Integer( 2 ), "Realizada" ),
			new MappableObject( new Integer( 3 ), "Desmarcada pela empresa" ),
			new MappableObject( new Integer( 4 ), "Desmarcada pela SIPRP" ),
		};
		estadoList = new RadioButtonFixedPanel( estadoListOptions, 6, 1, RadioButtonFixedPanel.ORIENTATION_VERTICAL, false, null );
		estadoList.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Estado" ) );

		observacoesText.setLineWrap( true );
		observacoesText.setWrapStyleWord( true );
		observacoesText.setRows( 5 );
		
		observacoesTextScroll.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Observa\u00e7\u00f5es" ) );		
		observacoesTextScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		observacoesTextScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );
		observacoesTextScroll.setViewportView( observacoesText );

	}

	public void place()
	{
		TableLayout layout = new TableLayout(
				new double[]{ TableLayout.FILL, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL },
				new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM,
						TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM,
						TableLayout.PREFERRED}
				);
		layout.setHGap(5);
		layout.setVGap(5);
		this.setLayout( layout );
		this.add( dataLabel, new TableLayoutConstraints(1,0) );
		this.add( dataPanel, new TableLayoutConstraints(2,0) );
		this.add( emailButton, new TableLayoutConstraints(2,1) );
		this.add( dataFichaLabel, new TableLayoutConstraints(1,2) );
		this.add( dataFichaPanel, new TableLayoutConstraints(2,2) );
		this.add( tecnicoLabel, new TableLayoutConstraints(1,3) );
		this.add( tecnicoList, new TableLayoutConstraints(2,3) );
		this.add( tecnicoSuperiorLabel, new TableLayoutConstraints(1,4) );
		this.add( tecnicoSuperiorList, new TableLayoutConstraints(2,4) );
		this.add( estadoList, new TableLayoutConstraints(1,5,2,5) );
		this.add( observacoesTextScroll, new TableLayoutConstraints(1,6,2,6) );
//		
//		JPanel MarcacaoVisitaHSTPanel = this;
//		GridBagConstraints MarcacaoVisitaHSTPanelGBC = new GridBagConstraints();
//		MarcacaoVisitaHSTPanelGBC.insets = new Insets( 1, 1, 1, 1 );
//		GridBagLayout MarcacaoVisitaHSTPanelLayout = new GridBagLayout();
//		MarcacaoVisitaHSTPanel.setLayout( MarcacaoVisitaHSTPanelLayout );
//		MarcacaoVisitaHSTPanelGBC.gridx = 0;
//		MarcacaoVisitaHSTPanelGBC.gridy = 0;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.0;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.HORIZONTAL;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( dataLabel, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( dataLabel );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 1;
//		MarcacaoVisitaHSTPanelGBC.gridy = 0;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.5;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.HORIZONTAL;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( dataPanel, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( dataPanel );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 2;
//		MarcacaoVisitaHSTPanelGBC.gridy = 0;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.5;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.NONE;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( emailButton, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( emailButton );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 0;
//		MarcacaoVisitaHSTPanelGBC.gridy = 1;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.0;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.HORIZONTAL;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( dataFichaLabel, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( dataFichaLabel );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 1;
//		MarcacaoVisitaHSTPanelGBC.gridy = 1;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.5;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.HORIZONTAL;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( dataFichaPanel, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( dataFichaPanel );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 0;
//		MarcacaoVisitaHSTPanelGBC.gridy = 2;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.0;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.HORIZONTAL;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( tecnicoLabel, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( tecnicoLabel );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 1;
//		MarcacaoVisitaHSTPanelGBC.gridy = 2;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 1;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 0.5;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.0;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.HORIZONTAL;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( tecnicoList, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( tecnicoList );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 0;
//		MarcacaoVisitaHSTPanelGBC.gridy = 3;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 3;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 1.0;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.5;
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.BOTH;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( estadoList, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( estadoList );
//
//		MarcacaoVisitaHSTPanelGBC.gridx = 0;
//		MarcacaoVisitaHSTPanelGBC.gridy = 4;
//		MarcacaoVisitaHSTPanelGBC.gridwidth = 3;
//		MarcacaoVisitaHSTPanelGBC.gridheight = 1;
//		MarcacaoVisitaHSTPanelGBC.weightx = 1.0;
//		MarcacaoVisitaHSTPanelGBC.weighty = 0.5;
//		
//		MarcacaoVisitaHSTPanelGBC.fill = GridBagConstraints.BOTH;
//		MarcacaoVisitaHSTPanelLayout.setConstraints( observacoesTextScroll, MarcacaoVisitaHSTPanelGBC );
//		MarcacaoVisitaHSTPanel.add( observacoesTextScroll );
	}

	private void setupComponentsHashtable()
	{
		components.putComponent( MarcacoesEstabelecimentoData.DATA, dataPanel );
		components.putComponent( MarcacoesEstabelecimentoData.DATA_RELATORIO, dataFichaPanel );
		components.putComponent( MarcacoesEstabelecimentoData.TECNICO_HST, tecnicoList );
		components.putComponent( MarcacoesEstabelecimentoData.TECNICO_SUPERIOR_HST, tecnicoSuperiorList );
		components.putComponent( MarcacoesEstabelecimentoData.ESTADO, estadoList );
		components.putComponent( MarcacoesEstabelecimentoData.OBSERVACOES, observacoesText );
	}

	public void fill( Object toFill )
	{
		clear();
		if( toFill == null || !( toFill instanceof PropertyObject ) )
		{
			return;
		}
		Map<String,Object> data = null;
		if( toFill instanceof PropertyObject )
		{
			data = ( (PropertyObject) toFill ).getHashData();
			if( toFill instanceof MarcacoesEstabelecimentoData )
			{
				marcacaoEstabelecimentosData = (MarcacoesEstabelecimentoData) toFill;
			}
		}
		else if( toFill instanceof Hashtable )
		{
			data = (Hashtable) toFill;
		}
		else
		{
			return;
		}
		if( marcacaoEstabelecimentosData == null )
		{
			marcacaoEstabelecimentosData = new MarcacoesEstabelecimentoData();
			marcacaoEstabelecimentosData.setHashData( data );
		}
		PropertyObject po = (PropertyObject) toFill;
		String properties[] = (String[]) components.keySet().toArray( new String[ 0 ] );
		ComponentController.fill( properties, po.getHashData(), components );
	}

	public Object save()
	{
		if( marcacaoEstabelecimentosData == null )
		{
			marcacaoEstabelecimentosData = new MarcacoesEstabelecimentoData();
		}
		Map<String,Object> data = marcacaoEstabelecimentosData.getHashData();
		String properties[] = (String[]) components.keySet().toArray( new String[ 0 ] );
		ComponentController.save( properties, data, components );
		if( !( marcacaoEstabelecimentosData instanceof PropertyObject ) )
		{
			return data;
		}
		marcacaoEstabelecimentosData.setHashData( data );
		return marcacaoEstabelecimentosData;
	}

	public void clear()
	{
		marcacaoEstabelecimentosData = null;
		String properties[] = (String[]) components.keySet().toArray( new String[ 0 ] );
		ComponentController.clear( properties, components );
	}

	public void setEnabled( boolean enable )
	{
		String properties[] = (String[]) components.keySet().toArray( new String[ 0 ] );
		ComponentController.setEnabled( properties, enable, components );
	}

	public Class<MarcacoesEstabelecimentoData> getDataClass()
	{
		return MarcacoesEstabelecimentoData.class;
	}

	public JLabel getDataFichaLabel()
	{
		return dataFichaLabel;
	}

	public JCalendarPanel getDataFichaPanel()
	{
		return dataFichaPanel;
	}

	public JLabel getDataLabel()
	{
		return dataLabel;
	}

	public JCalendarPanel getDataPanel()
	{
		return dataPanel;
	}

	public JButton getEmailButton()
	{
		return emailButton;
	}

	public RadioButtonFixedPanel getEstadoList()
	{
		return estadoList;
	}

	public StateTextArea getObservacoesText()
	{
		return observacoesText;
	}

	public JLabel getTecnicoLabel()
	{
		return tecnicoLabel;
	}

	public JComboBox getTecnicoList()
	{
		return tecnicoList;
	}
	
	public JComboBox getTecnicoSuperiorList()
	{
		return tecnicoSuperiorList;
	}

}
