package siprp.medicina.processo.detalhes;

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import siprp.medicina.MedicinaConstants;
import siprp.medicina.prestadores.PrestadoresDataProvider;
import siprp.medicina.prestadores.data.PrestadoresData;
import siprp.medicina.processo.ProcessoConstants;
import siprp.medicina.processo.ProcessoDataProvider;
import siprp.medicina.processo.data.TrabalhadoresConsultasData;

import com.evolute.utils.dataui.ControllableComponent;
import com.evolute.utils.images.ImageIconLoader;
import com.evolute.utils.tables.BaseTable;
import com.evolute.utils.tables.ColumnizedObjectArray;
import com.evolute.utils.tables.VectorTableModel;
import com.evolute.utils.ui.DialogException;

public class DetalhesConsultaPanel
	extends JPanel
	implements ControllableComponent, ProcessoConstants,
				ActionListener
{
	protected JFrame owner;
	protected JButton marcarButton;
	protected JButton cancelarButton;
	protected JTextField estadoText;
	protected BaseTable datasTable;
	protected VectorTableModel datasModel;
	
	protected ProcessoDataProvider processoProvider;
	
	public DetalhesConsultaPanel( JFrame owner )
		throws Exception
	{
		this.owner = owner;
		processoProvider = ProcessoDataProvider.getProvider();
		setupComponents();
	}

	private void setupComponents()
	{
		Icon marcarIcon = getIcon( DATA_ICON_PATH );
		marcarButton = new JButton( "Fazer Marca\u00e7\u00e3o" );
		marcarButton.setIcon( marcarIcon );
		marcarButton.addActionListener( this );
		Icon cancelarIcon = getIcon( DATA_ICON_PATH );
		cancelarButton = new JButton( "Cancelar Consulta" );
		cancelarButton.setIcon( cancelarIcon );
		cancelarButton.addActionListener( this );
		
		estadoText = new JTextField();
		estadoText.setEditable( false );
		datasModel = new VectorTableModel( new String[]{ "data", "prestador", "estado" } );
		datasTable = new BaseTable( datasModel );
		JScrollPane datasScroll = new JScrollPane( datasTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		
		double cols[] = new double[]{ TableLayout.FILL, TableLayoutConstraints.FILL };
		double rows[] = new double[]{ TableLayout.PREFERRED, TableLayoutConstraints.MINIMUM, TableLayoutConstraints.FILL };
		TableLayout layout = new TableLayout( cols, rows );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		setLayout( layout );
		add( marcarButton, new TableLayoutConstraints( 0, 0 ) );
		add( cancelarButton, new TableLayoutConstraints( 1, 0 ) );
		add( estadoText, new TableLayoutConstraints( 0, 1, 1, 1 ) );
		add( datasScroll, new TableLayoutConstraints( 0, 2, 1, 2 ) );
	}
	
	public Object save()
	{
		return null;
	}
	
	public void fill( Object value )
	{
		clear();
		Integer id = ( Integer ) value;
		if( id != null )
		{
			try
			{
				TrabalhadoresConsultasData consulta = processoProvider.getConsultaByID( id );
				Integer estado = ( Integer ) consulta.get( TrabalhadoresConsultasData.ESTADO );
				String estadoStr = MedicinaConstants.ESTADOS_CONSULTA_STR[ estado != null ? estado.intValue() : 0 ];
				estadoText.setText( estadoStr );
				Vector values = datasModel.getValues();
				Object datas[][] = processoProvider.getDatasConsulta( id );
				for( int n = 0; n < datas.length; n++ )
				{
					PrestadoresData prestador = PrestadoresDataProvider.getProvider().getPrestadorByID( ( Integer ) datas[ n ][ 2 ] );
					datas[ n ][ 2 ] = prestador.get( PrestadoresData.NOME );
					Integer estadoData = ( Integer ) datas[ n ][ 3 ];
					datas[ n ][ 3 ] = MedicinaConstants.ESTADOS_CONSULTA_STR[ estadoData != null ? estadoData.intValue() : 0 ];
					values.add( new ColumnizedObjectArray( datas[ n ], true ) );
				}
				datasModel.setValues( values );
			}
			catch( Exception ex )
			{
				DialogException.showExceptionMessage( ex, "Erro a carregar dados da consulta", true );
			}
		}
	}
	
	public void clear()
	{
		estadoText.setText( "" );
		datasModel.clearAll();
	}
	
	public void setEnabled( boolean enable )
	{
	}
	
	public void actionPerformed( ActionEvent e )
	{
	}
	
	public Icon getIcon( String path )
	{
		try
		{
			return ImageIconLoader.loadImageIcon( getClass(), path );
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
			return null;
		}
	}
}
