package siprp.medicina;

import com.evolute.utils.*;
import com.evolute.utils.arrays.*;
import com.evolute.utils.data.*;
import com.evolute.utils.dataui.*;
import com.evolute.utils.db.*;
import com.evolute.utils.documents.*;
import com.evolute.utils.sql.*;
import com.evolute.utils.ui.*;
import com.evolute.utils.ui.calendar.*;
import com.evolute.utils.ui.panel.*;
import com.evolute.utils.ui.text.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import siprp.data.*;

public class MarcacaoConsultaPanel
	extends JPanel
	implements ControllableComponent, DataClassAware
{
	private siprp.data.MarcacaoTrabalhadorData marcacaoTrabalhadorData;
	private final ComponentsHashtable components = new ComponentsHashtable();

	protected final SQLExecuter EXECUTER;

	private final JLabel dataLabel = new JLabel( "Data da consulta", JLabel.RIGHT );
	private final JCalendarPanel dataPanel = new JCalendarPanel( null );
	private final JButton emailButton = new JButton( "Enviar Email" );
	private final JLabel dataFichaLabel = new JLabel( "Data da Ficha de Aptid\u00e3o", JLabel.RIGHT );
	private final JCalendarPanel dataFichaPanel = new JCalendarPanel( null );
	private RadioButtonFixedPanel tipoList;
	private RadioButtonFixedPanel estadoList;
	private final JScrollPane observacoesTextScroll = new JScrollPane();
	private final StateTextArea observacoesText = new StateTextArea();

	public MarcacaoConsultaPanel()
		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
	{
		JComponent MarcacaoConsultaPanel = this;
		dataPanel.setMinimumSize( new Dimension( 300, 20 ) );
		dataPanel.setPreferredSize( new Dimension( 100, 20 ) );

		emailButton.setMinimumSize( new Dimension( 300, 20 ) );
		emailButton.setPreferredSize( new Dimension( 200, 20 ) );

		dataFichaPanel.setMinimumSize( new Dimension( 300, 20 ) );
		dataFichaPanel.setPreferredSize( new Dimension( 100, 20 ) );

		IDObject tipoListOptions[] = new IDObject[]{
			new MappableObject( new Integer( 1 ), "Admiss\u00e3o" ),
			new MappableObject( new Integer( 2 ), "Peri\u00f3dico" ),
			new MappableObject( new Integer( 5 ), "Peri\u00f3dico inicial" ),
			new MappableObject( new Integer( 3 ), "Ocasional" ),
		};
		tipoList = new RadioButtonFixedPanel( tipoListOptions, 4, 1, RadioButtonFixedPanel.ORIENTATION_VERTICAL, false, null );
		tipoList.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Tipo" ) );

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

		observacoesTextScroll.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Observa\u00e7\u00f5es" ) );
		observacoesText.setLineWrap( true );
		observacoesText.setWrapStyleWord( true );
		observacoesText.setRows( 5 );

	}

	public void place()
	{
		JPanel MarcacaoConsultaPanel = this;
		GridBagConstraints MarcacaoConsultaPanelGBC = new GridBagConstraints();
		MarcacaoConsultaPanelGBC.insets = new Insets( 1, 1, 1, 1 );
		GridBagLayout MarcacaoConsultaPanelLayout = new GridBagLayout();
		MarcacaoConsultaPanel.setLayout( MarcacaoConsultaPanelLayout );
		MarcacaoConsultaPanelGBC.gridx = 0;
		MarcacaoConsultaPanelGBC.gridy = 0;
		MarcacaoConsultaPanelGBC.gridwidth = 1;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.0;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.HORIZONTAL;
		MarcacaoConsultaPanelLayout.setConstraints( dataLabel, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( dataLabel );

		MarcacaoConsultaPanelGBC.gridx = 1;
		MarcacaoConsultaPanelGBC.gridy = 0;
		MarcacaoConsultaPanelGBC.gridwidth = 1;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.5;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.HORIZONTAL;
		MarcacaoConsultaPanelLayout.setConstraints( dataPanel, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( dataPanel );

		MarcacaoConsultaPanelGBC.gridx = 2;
		MarcacaoConsultaPanelGBC.gridy = 0;
		MarcacaoConsultaPanelGBC.gridwidth = 1;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.5;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.NONE;
		MarcacaoConsultaPanelLayout.setConstraints( emailButton, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( emailButton );

		MarcacaoConsultaPanelGBC.gridx = 0;
		MarcacaoConsultaPanelGBC.gridy = 1;
		MarcacaoConsultaPanelGBC.gridwidth = 1;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.0;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.HORIZONTAL;
		MarcacaoConsultaPanelLayout.setConstraints( dataFichaLabel, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( dataFichaLabel );

		MarcacaoConsultaPanelGBC.gridx = 1;
		MarcacaoConsultaPanelGBC.gridy = 1;
		MarcacaoConsultaPanelGBC.gridwidth = 1;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.5;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.HORIZONTAL;
		MarcacaoConsultaPanelLayout.setConstraints( dataFichaPanel, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( dataFichaPanel );

		MarcacaoConsultaPanelGBC.gridx = 0;
		MarcacaoConsultaPanelGBC.gridy = 2;
		MarcacaoConsultaPanelGBC.gridwidth = 2;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.5;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.BOTH;
		MarcacaoConsultaPanelLayout.setConstraints( tipoList, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( tipoList );

		MarcacaoConsultaPanelGBC.gridx = 2;
		MarcacaoConsultaPanelGBC.gridy = 2;
		MarcacaoConsultaPanelGBC.gridwidth = 1;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 0.5;
		MarcacaoConsultaPanelGBC.weighty = 0.0;
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.BOTH;
		MarcacaoConsultaPanelLayout.setConstraints( estadoList, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( estadoList );

		MarcacaoConsultaPanelGBC.gridx = 0;
		MarcacaoConsultaPanelGBC.gridy = 3;
		MarcacaoConsultaPanelGBC.gridwidth = 3;
		MarcacaoConsultaPanelGBC.gridheight = 1;
		MarcacaoConsultaPanelGBC.weightx = 1.0;
		MarcacaoConsultaPanelGBC.weighty = 1.0;
		observacoesTextScroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		observacoesTextScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );
		observacoesTextScroll.setViewportView( observacoesText );
		MarcacaoConsultaPanelGBC.fill = GridBagConstraints.BOTH;
		MarcacaoConsultaPanelLayout.setConstraints( observacoesTextScroll, MarcacaoConsultaPanelGBC );
		MarcacaoConsultaPanel.add( observacoesTextScroll );

	}

	private void setupComponentsHashtable()
	{
		components.putComponent( siprp.data.MarcacaoTrabalhadorData.DATA, dataPanel );
		components.putComponent( siprp.data.MarcacaoTrabalhadorData.DATA_RELATORIO, dataFichaPanel );
		components.putComponent( siprp.data.MarcacaoTrabalhadorData.MOTIVO, tipoList );
		components.putComponent( siprp.data.MarcacaoTrabalhadorData.ESTADO, estadoList );
		components.putComponent( siprp.data.MarcacaoTrabalhadorData.OBSERVACOES, observacoesText );
	}

	public void fill( Object toFill )
	{
		clear();
		if( toFill == null || !( toFill instanceof PropertyObject ) )
		{
			return;
		}
		Hashtable data = null;
		if( toFill instanceof PropertyObject )
		{
			data = ( (PropertyObject) toFill ).getHashData();
			if( toFill instanceof siprp.data.MarcacaoTrabalhadorData )
			{
				marcacaoTrabalhadorData = (siprp.data.MarcacaoTrabalhadorData) toFill;
			}
		}
		else if( toFill instanceof Hashtable )
		{
			data = (Hashtable) toFill;
		}
		else
		{
			return;
		}
		if( marcacaoTrabalhadorData == null )
		{
			marcacaoTrabalhadorData = new siprp.data.MarcacaoTrabalhadorData();
			marcacaoTrabalhadorData.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( marcacaoTrabalhadorData == null )
		{
			marcacaoTrabalhadorData = new siprp.data.MarcacaoTrabalhadorData();
		}
		Hashtable data = marcacaoTrabalhadorData.getHashData();
		String properties[] = (String[]) components.keySet().toArray( new String[ 0 ] );
		ComponentController.save( properties, data, components );
		if( !( marcacaoTrabalhadorData instanceof PropertyObject ) )
		{
			return data;
		}
		marcacaoTrabalhadorData.setHashData( data );
		return marcacaoTrabalhadorData;
	}

	public void clear()
	{
		marcacaoTrabalhadorData = 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 getDataClass()
	{
		return siprp.data.MarcacaoTrabalhadorData.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 RadioButtonFixedPanel getTipoList()
	{
		return tipoList;
	}
}
