/*
 * PrestadoresWindow.java
 *
 * Created on February 2, 2007, 9:44 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.medicina.prestadores;

import com.evolute.utils.Singleton;
import com.evolute.utils.data.IDObject;
import com.evolute.utils.dataui.ComponentController;
import com.evolute.utils.dataui.ComponentsHashtable;
import com.evolute.utils.dataui.JDOControllable;
import com.evolute.utils.dataui.JTextComponentPlainFillerSaver;
import com.evolute.utils.jdo.JDOProvider;
import com.evolute.utils.ui.DialogException;
import com.evolute.utils.ui.panel.CheckBoxPanel;
import com.evolute.utils.ui.search.SearchDialog;
import com.evolute.utils.ui.window.EditorWindow;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;
import java.awt.Dimension;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import siprp.clientes.ClientesDataProvider;
import siprp.clientes.ContactoPanel;
import siprp.data.ContactoData;
import siprp.medicina.prestadores.data.PrestadoresData;

/**
 *
 * @author fpalma
 */
public class PrestadoresWindow extends EditorWindow
{
	public static final String TITLE = "Prestadores";
	
	protected JTextField nomeText;
	protected JCheckBox activoCheck;
	protected JCheckBox consultasCheck;
	protected JCheckBox ecdsCheck;
	protected CheckBoxPanel ecdsPanel;
	protected JTextArea moradaText;
	protected JTextField codigoPostalText;
	protected JTextField localidadeCodigoPostalText;
	protected JTextField localidadeText;
	protected ContactoPanel contactoPanel;
	protected JTextField contribuinteText;
	
	
	protected PrestadoresDataProvider provider;
	protected ClientesDataProvider clientesProvider;
	
	protected ComponentsHashtable components;
	
	protected PrestadoresData prestador;
	protected Integer gruposProtocolo[];
	
	/** Creates a new instance of PrestadoresWindow */
	public PrestadoresWindow()
		throws Exception
	{
		super( new int[][]{{ NEW_INDEX, EDIT_INDEX, CANCEL_INDEX, SAVE_INDEX, DELETE_INDEX,
								SELECT_BYNAME_INDEX }} );
		provider = PrestadoresDataProvider.getProvider();
		clientesProvider = ( ClientesDataProvider ) ClientesDataProvider.getProvider();
		setupComponents();
		setupComponentsHashtable();
	}
	
	private void setupComponents()
		throws Exception
	{
		setTitle( TITLE );
		JLabel nomeLabel = new JLabel( "Designa\u00e7\u00e3o" );
		nomeText = new JTextField();
		nomeText.setPreferredSize( new Dimension( 500, 20 ) );
		activoCheck = new JCheckBox( "Activo (actualmente fornece servi\u00e7os \u00e0 SIPRP)" );
		consultasCheck = new JCheckBox( "Faz consultas" );
		ecdsCheck = new JCheckBox( "Faz ecds" );
		List<IDObject> gruposProtocolo = new Vector<IDObject>();
		IDObject gruposProtocoloReais[] = clientesProvider.getAllGruposProtocoloReais();
		if( gruposProtocoloReais != null && gruposProtocoloReais.length > 0 )
		{
			gruposProtocolo.addAll( Arrays.asList( gruposProtocoloReais ) );
		}
		IDObject gruposProtocoloFalsos[] = clientesProvider.getAllGruposProtocoloFalsos();
		if( gruposProtocoloFalsos != null && gruposProtocoloFalsos.length > 0 )
		{
			gruposProtocolo.addAll( Arrays.asList( gruposProtocoloFalsos ) );
		}
		ecdsPanel = new CheckBoxPanel( gruposProtocolo.toArray( new IDObject[ gruposProtocolo.size() ] ) );
		JLabel contactoLabel = new JLabel( "Contacto" );
		contactoPanel = new ContactoPanel();
		contactoPanel.setPreferredSize( new Dimension( 350, 120 ) );
		contactoPanel.setBorder( BorderFactory.createEtchedBorder() );
		JLabel moradaLabel = new JLabel( "Morada" );
		moradaText = new JTextArea();
		moradaText.setLineWrap( true );
		moradaText.setWrapStyleWord( true );
		JScrollPane moradaScroll = new JScrollPane( moradaText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
														JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		moradaScroll.setPreferredSize( new Dimension( 500, 50 ) );
		JLabel codigoPostalLabel = new JLabel( "C\u00f3digo Postal" );
		codigoPostalText = new JTextField();
		codigoPostalText.setPreferredSize( new Dimension( 100, 50 ) );
		localidadeCodigoPostalText = new JTextField();
		localidadeCodigoPostalText.setPreferredSize( new Dimension( 150, 50 ) );
		JLabel localidadeLabel = new JLabel( "Localidade" );
		localidadeText = new JTextField();
		JLabel contribuinteLabel = new JLabel( "Contribuinte" );
		contribuinteText = new JTextField();
		contribuinteText.setPreferredSize( new Dimension( 100, 50 ) );
		
		double cols[] = 
				new double[]{ TableLayout.MINIMUM, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.FILL };
		double rows[] = 
				new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM,
								TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED,
								TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM };
		TableLayout tableLayout = new TableLayout( cols,rows );
		getContentPane().setLayout( tableLayout );
		
		getContentPane().add( nomeLabel, new TableLayoutConstraints( 0, 0 ) );
		getContentPane().add( nomeText, new TableLayoutConstraints( 1, 0, 3, 0 ) );
		getContentPane().add( activoCheck, new TableLayoutConstraints( 0, 1, 2, 1 ) );
		getContentPane().add( consultasCheck, new TableLayoutConstraints( 0, 2 ) );
		getContentPane().add( ecdsCheck, new TableLayoutConstraints( 0, 3 ) );
		getContentPane().add( ecdsPanel, new TableLayoutConstraints( 1, 3, 2, 3 ) );
		getContentPane().add( contactoLabel, new TableLayoutConstraints( 0, 4 ) );
		getContentPane().add( contactoPanel, new TableLayoutConstraints( 1, 4, 2, 4 ) );
		getContentPane().add( moradaLabel, new TableLayoutConstraints( 0, 5 ) );
		getContentPane().add( moradaScroll, new TableLayoutConstraints( 1, 5, 3, 5 ) );
		getContentPane().add( codigoPostalLabel, new TableLayoutConstraints( 0, 6 ) );
		getContentPane().add( codigoPostalText, new TableLayoutConstraints( 1, 6 ) );
		getContentPane().add( localidadeCodigoPostalText, new TableLayoutConstraints( 2, 6, 3, 6 ) );
		getContentPane().add( localidadeLabel, new TableLayoutConstraints( 0, 7 ) );
		getContentPane().add( localidadeText, new TableLayoutConstraints( 1, 7, 3, 7 ) );
		getContentPane().add( contribuinteLabel, new TableLayoutConstraints( 0, 8 ) );
		getContentPane().add( contribuinteText, new TableLayoutConstraints( 1, 8 ) );
		pack();
	}
	
	private void setupComponentsHashtable()
	{
		components = new ComponentsHashtable();
		components.putComponent( PrestadoresData.NOME, nomeText );
		components.putComponent( PrestadoresData.NOME_PLAIN, new JTextComponentPlainFillerSaver( nomeText ) );
		components.putComponent( PrestadoresData.MORADA, moradaText );
		components.putComponent( PrestadoresData.CODIGO_POSTAL, codigoPostalText );
		components.putComponent( PrestadoresData.LOCALIDADE_CP, localidadeCodigoPostalText );
		components.putComponent( PrestadoresData.LOCALIDADE, localidadeText );
		components.putComponent( PrestadoresData.CONTRIBUINTE, contribuinteText );
		components.putComponent( PrestadoresData.ACTIVO, activoCheck );
		components.putComponent( PrestadoresData.FAZ_CONSULTAS, consultasCheck );
		components.putComponent( PrestadoresData.FAZ_ECDS, ecdsCheck );
		components.putComponent( PrestadoresData.CONTACTO_ID, 
				new JDOControllable( ( JDOProvider )Singleton.getInstance( Singleton.DEFAULT_JDO_PROVIDER ),
										ContactoData.class, new String[]{ ContactoData.ID },
										contactoPanel ) );
	}
	

	public boolean searchByName()
	{
		SearchDialog search = new SearchDialog( provider );
		Integer id = ( Integer )search.getSelected();
		if( id == null )
		{
			return false;
		}
		clear( 0 );
		try
		{
			prestador = provider.getPrestadorByID( id );
			gruposProtocolo = provider.getGruposProtocoloIDByPrestadorID( id );
			reload( 0 );
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Ocorreu um erro a carregar o prestador.", true );
			return false;
		}

		return true;
	}
	
	public void enableComponents( int index, boolean enable )
	{
		String names[] = (String[])components.keySet().toArray( new String[0] );
		ComponentController.setEnabled( names, enable, components );
		ecdsPanel.setEnabled( enable );
	}
	
	public void reload( int index )
	{
		try
		{
			String names[] = (String[])components.keySet().toArray( new String[0] );
			ComponentController.fill( names, prestador.getHashData(), components );
			ecdsPanel.setSelected( gruposProtocolo );
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro a carregar dados", true );
			
		}
	}
	
	public void clear( int index )
	{
		String names[] = (String[])components.keySet().toArray( new String[0] );
		ComponentController.clear( names, components );
		ecdsPanel.clear();
		prestador = null;
		gruposProtocolo = null;
	}
	
	public boolean newItem( int index )
	{
		return true;
	}
	
	public boolean save( int index )
	{
		if( prestador == null )
		{
			prestador = new PrestadoresData();
		}
		String names[] = (String[])components.keySet().toArray( new String[0] );
		Hashtable data = prestador.getHashData();
		ComponentController.save( names, data, components );
		prestador.setHashData( data );
		gruposProtocolo = ecdsPanel.getSelected();
		try
		{
			provider.savePrestador( prestador );
			provider.setGruposProtocoloForPrestador( ( Integer ) prestador.get( PrestadoresData.ID ),
														gruposProtocolo );
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro a guardar dados", true );
			return false;
		}
		return true;
	}
}
