package siprp.medicina.processo.detalhes;

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

import java.util.Vector;

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.ProcessoDataProvider;
import siprp.medicina.processo.data.TrabalhadoresConsultasData;

import com.evolute.utils.dataui.ControllableComponent;
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 ConsultaInfoPanel
	extends JPanel
	implements ControllableComponent
{
	protected JFrame owner;
	protected JTextField estadoText;
	protected BaseTable datasTable;
	protected VectorTableModel datasModel;
	
	protected ProcessoDataProvider processoProvider;
	
	public ConsultaInfoPanel( JFrame owner )
		throws Exception
	{
		this.owner = owner;
		processoProvider = ProcessoDataProvider.getProvider();
		setupComponents();
	}

	private void setupComponents()
	{
		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[]{ TableLayoutConstraints.FILL };
		double rows[] = new double[]{ TableLayoutConstraints.MINIMUM, TableLayoutConstraints.FILL };
		TableLayout layout = new TableLayout( cols, rows );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		setLayout( layout );
		add( estadoText, new TableLayoutConstraints( 0, 0 ) );
		add( datasScroll, new TableLayoutConstraints( 0, 1 ) );
	}
	
	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 )
	{
	}
}
