/*
 * DetalhesProcessoPanel.java
 *
 * Created on 25 de Abril de 2007, 17:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.medicina.processo.detalhes;

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

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

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import siprp.medicina.processo.ProcessoConstants;
import siprp.medicina.processo.ProcessoEvent;
import siprp.medicina.processo.ProcessoListener;

import com.evolute.utils.images.ImageIconLoader;

/**
 *
 * @author Frederico
 */
public class DetalhesProcessoPanel extends JPanel
	implements ProcessoConstants, ActionListener
{
	private final Vector<ProcessoListener> PROCESSO_LISTENERS = new Vector<ProcessoListener>();
	
	private static final String CARD_BLANK = "";
	private static final String CARD_CONSULTA = "CONSULTA";
	private static final String CARD_ECDS = "ECDS";
	
	protected JFrame owner;
	
	private JButton novoGrupoECDsButton;
	private JButton novaConsultaButton;
	private JLabel consultaLabel;
	private JLabel ecdsLabel;
	private JLabel fichaAptidaoLabel;
//	protected JPanel blankPanel;
//	protected DataConsultaPanel consultaPanel;
//	protected ECDsPanel ecdsPanel;
	
	protected CardLayout cardLayout;
	
	protected Integer processoId; 
	
	/**
	 * Creates a new instance of DetalhesProcessoPanel
	 */
	public DetalhesProcessoPanel( JFrame owner )
		throws Exception
	{
		this.owner = owner;
		setupComponents();
	}
	
	private void setupComponents()
		throws Exception
	{
		Icon ecdsIcon = getIcon( ECDS_ICON_PATH );
		novoGrupoECDsButton = new JButton( "Marcar ECDs" );
		novoGrupoECDsButton.setIcon( ecdsIcon );
		novoGrupoECDsButton.addActionListener( this );
		Icon consultaIcon = getIcon( CONSULTA_ICON_PATH );
		novaConsultaButton = new JButton( "Marcar Consulta" );
		novaConsultaButton.setIcon( consultaIcon );
		novaConsultaButton.addActionListener( this );
		ecdsLabel = new JLabel( ecdsIcon, JLabel.LEFT );
		consultaLabel = new JLabel( consultaIcon, JLabel.LEFT );
		Icon fichaAptidaoIcon = getIcon( FICHA_APTIDAO_ICON_PATH );
		fichaAptidaoLabel = new JLabel( fichaAptidaoIcon, JLabel.LEFT );
		
		double cols[] =	new double[]{ TableLayout.FILL, TableLayout.FILL };
		double rows[] =	
			new double[]{ TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, 
							TableLayout.PREFERRED, TableLayout.FILL };
	
		TableLayout tableLayout = new TableLayout( cols, rows );
		setLayout( tableLayout );
	
		add( novoGrupoECDsButton, new TableLayoutConstraints( 0, 0 ) );
		add( novaConsultaButton, new TableLayoutConstraints( 1, 0 ) );
		add( ecdsLabel, new TableLayoutConstraints( 0, 1, 1, 1 ) );
		add( consultaLabel, new TableLayoutConstraints( 0, 2, 1, 2 ) );
		add( fichaAptidaoLabel, new TableLayoutConstraints( 0, 3, 1, 3 ) );
		
//		blankPanel = new JPanel();
//		consultaPanel = new DataConsultaPanel( owner );
//		ecdsPanel = new ECDsPanel( owner );
//		
//		cardLayout = new CardLayout();
//		setLayout( cardLayout );
//		add( blankPanel, CARD_BLANK );
//		add( consultaPanel, CARD_CONSULTA );
//		add( ecdsPanel, CARD_ECDS );
	}
	
	public void addProcessoListener( ProcessoListener listener )
	{
		PROCESSO_LISTENERS.add( listener );
//		consultaPanel.addProcessoListener( listener );
//		ecdsPanel.addProcessoListener( listener );
	}
	
	public void removeProcessoListener( ProcessoListener listener )
	{
		PROCESSO_LISTENERS.remove( listener );
//		consultaPanel.removeProcessoListener( listener );
//		ecdsPanel.removeProcessoListener( listener );
	}
	
	public void actionPerformed( ActionEvent e )
	{
		Object source = e.getSource();
		if( source.equals( novoGrupoECDsButton ) )
		{
			novaConsulta();
		}
		else if( source.equals( novaConsultaButton ) )
		{
			novoGrupoECDs();
		}
	}
	
	public void clear()
	{
//		cardLayout.first( this );
	}
	
	public void setTrabalhadorID( Integer trabalhadorID )
	{
//		consultaPanel.setTrabalhadorID( trabalhadorID );
	}
	
	public void setProcessoID( Integer processoID )
	{
		this.processoId = processoID;
//		consultaPanel.setProcessoID( processoID );
	}
	
	public void setConsulta( Integer consultaID )
	{
//		cardLayout.show( this, CARD_CONSULTA );
//		consultaPanel.fill( consultaID );
	}
	
	public void setECDs( Integer ecdsID )
	{
		cardLayout.show( this, CARD_ECDS );
	}
	
	public Icon getIcon( String path )
	{
		try
		{
			return ImageIconLoader.loadImageIcon( getClass(), path );
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
			return null;
		}
	}
	
	protected void notifyListeners( int accao )
	{
		HashMap<Integer,Integer> ids = new HashMap<Integer,Integer>();
		ids.put( ProcessoEvent.TIPO_PROCESSO, processoId );
		ProcessoEvent event = new ProcessoEvent( this, accao, ids );
		for( int n = 0; n < PROCESSO_LISTENERS.size(); n++ )
		{
			PROCESSO_LISTENERS.get( n ).processoStateChanged( event );
		}
	}
	
	protected void novoGrupoECDs()
	{
	}
	
	protected void novaConsulta()
	{
		notifyListeners( ProcessoEvent.ACCAO_MARCAR_CONSULTA );
	}
}
