package siprp.medicina.processo.ui;

import static com.evolute.utils.strings.UnicodeLatin1Map.atilde;
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil;
import static com.evolute.utils.strings.UnicodeLatin1Map.otilde;
import static info.clearthought.layout.TableLayoutConstants.FILL;
import static info.clearthought.layout.TableLayoutConstants.MINIMUM;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.CLOSE;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.CREATE_CONSULTA;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.CREATE_EXAME;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.*;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_CONSULTA_MARCACAO;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_CONSULTA_MARCACAO_EMAIL;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_CONSULTA_MARCACAO_OBSERVACAO;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_EXAME;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_EXAME_MARCACAO;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_EXAME_MARCACAO_EMAIL;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_EXAME_MARCACAO_OBSERVACAO;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_PROCESSO;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.LOAD_TRABALHADOR;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;

import java.awt.CardLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import leaf.LeafButton;
import leaf.LeafWindow;
import leaf.LeafWindow.ActionActivation;

import org.apache.cayenne.conf.Configuration;

import siprp.database.cayenne.objects.Trabalhadores;
import siprp.database.cayenne.objects.TrabalhadoresConsultas;
import siprp.database.cayenne.objects.TrabalhadoresConsultasDatas;
import siprp.database.cayenne.objects.TrabalhadoresConsultasDatasEmails;
import siprp.database.cayenne.objects.TrabalhadoresConsultasDatasObservacoes;
import siprp.database.cayenne.objects.TrabalhadoresEcds;
import siprp.database.cayenne.objects.TrabalhadoresEcdsDatas;
import siprp.database.cayenne.objects.TrabalhadoresEcdsDatasEmails;
import siprp.database.cayenne.objects.TrabalhadoresEcdsDatasObservacoes;
import siprp.database.cayenne.objects.TrabalhadoresProcesso;
import siprp.medicina.MedicinaConstants;
import siprp.medicina.processo.ProcessoConstants;

public class ProcessoAccoesPanel extends JPanel
{
	private static final long serialVersionUID = 1L;

	private final LeafWindow parentWindow;

	private static final String PANEL_START_NAME = "START_PANEL";

	private static final String PANEL_TRABALHADOR_NAME = "TRABALHADOR_PANEL";

	private static final String PANEL_PROCESSO_NAME = "PROCESSO_PANEL";

	private static final String PANEL_CONSULTA_NAME = "PANEL_CONSULTA_NAME";
	private static final String PANEL_CONSULTA_MARCACAO_NAME = "PANEL_CONSULTA_MARCACAO_NAME";

	private static final String PANEL_EXAME_NAME = "EXAME_PANEL";
	private static final String PANEL_EXAME_MARCACAO_NAME = "PANEL_EXAME_MARCACAO_NAME";

	private final JPanel cardPanel = new JPanel();

	// private final JPanel controlPanel = new JPanel();

	private final CardLayout cardLayout = new CardLayout();

	// panels
	private final JPanel panelExame = new JPanel();
	private final JPanel panelExameMarcacao = new JPanel();

	private final JPanel panelConsulta = new JPanel();
	private final JPanel panelConsultaMarcacao = new JPanel();

	private final JPanel panelProcesso = new JPanel();

	private final JPanel panelTrabalhador = new JPanel();

	private final JPanel panelStart = new JPanel();

	// trabalhador
	@ActionActivation(onSelect = CREATE_PROCESSO, onChange = "")
	public final LeafButton buttonNovoTrabalhadorProcesso = new LeafButton( "Novo Processo" );

	// processo
	@ActionActivation(onSelect = FECHAR_PROCESSO, onChange = "")
	public final LeafButton buttonFecharProcesso = new LeafButton( "Fechar Processo" );
	@ActionActivation(onSelect = {
			CREATE_CONSULTA, CREATE_CONSULTA_MARCACAO
	}, onChange = "")
	public final LeafButton buttonNovoProcessoConsulta = new LeafButton( "Nova Consulta" );
	@ActionActivation(onSelect = {
			CREATE_EXAME, CREATE_EXAME_MARCACAO
	}, onChange = "")
	public final LeafButton buttonNovoProcessoExame = new LeafButton( "Novo Exame" );
	
	@ActionActivation(onSelect = APAGAR_PROCESSO, onChange = "")
	public final LeafButton buttonApagarProcesso = new LeafButton( "Remover Processo" );

	// consulta
	@ActionActivation(onSelect = CREATE_CONSULTA_MARCACAO, onChange = "")
	public final LeafButton buttonNovoConsultaMarcacao = new LeafButton( "Nova Marca" + ccedil + atilde + "o" );

	// marcacao consulta
	@ActionActivation(onSelect = CREATE_CONSULTA_MARCACAO_EMAIL, onChange = "")
	public final LeafButton buttonNovoConsultaMarcacaoEmail = new LeafButton( "Enviar e-Mail" );
	@ActionActivation(onSelect = CREATE_CONSULTA_MARCACAO_OBSERVACOES, onChange = "")
	public final LeafButton buttonNovoConsultaMarcacaoObservacoes = new LeafButton( "Adicionar Observa" + ccedil + atilde + "o" );

	@ActionActivation(onSelect = DESMARCAR_SIPRP_CONSULTA_MARCACAO, onChange = "")
	public final LeafButton buttonConsultaMarcacaoDesmarcarSiprp = new LeafButton( "Desmarcar (SIPRP)" );
	
	@ActionActivation(onSelect = DESMARCAR_TRABALHADOR_CONSULTA_MARCACAO, onChange = "")
	public final LeafButton buttonConsultaMarcacaoDesmarcarTrabalhador = new LeafButton( "Desmarcar (Trab)" );

	@ActionActivation(onSelect = REALIZAR_CONSULTA_MARCACAO, onChange = "")
	public final LeafButton buttonConsultaMarcacaoRealizar = new LeafButton( "Realizar" );

	@ActionActivation(onSelect = FALTOU_CONSULTA_MARCACAO, onChange = "")
	public final LeafButton buttonConsultaMarcacaoFaltou = new LeafButton( "Faltou" );

	// email marcacao consulta

	// observacoes marcacao consulta

	// exame
	@ActionActivation(onSelect = CREATE_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonNovoExameMarcacao = new LeafButton( "Nova Marca" + ccedil + atilde + "o" );

	// marcacao exame
	@ActionActivation(onSelect = CREATE_EXAME_MARCACAO_EMAIL, onChange = "")
	public final LeafButton buttonNovoExameMarcacaoEmail = new LeafButton( "Enviar e-Mail" );
	@ActionActivation(onSelect = CREATE_EXAME_MARCACAO_OBSERVACOES, onChange = "")
	public final LeafButton buttonNovoExameMarcacaoObservacoes = new LeafButton( "Adicionar Observa" + ccedil + atilde + "o" );
	
	@ActionActivation(onSelect = DESMARCAR_SIPRP_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonExameMarcacaoDesmarcarSiprp = new LeafButton( "Desmarcar (SIPRP)" );
	
	@ActionActivation(onSelect = DESMARCAR_TRABALHADOR_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonExameMarcacaoDesmarcarTrabalhador = new LeafButton( "Desmarcar (Trab)" );

	@ActionActivation(onSelect = REALIZAR_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonExameMarcacaoRealizar = new LeafButton( "Realizar" );
	
	@ActionActivation(onSelect = REALIZAR_PARCIAL_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonExameMarcacaoRealizarParcial = new LeafButton( "Realizar Parcial." );

	@ActionActivation(onSelect = FALTOU_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonExameMarcacaoFaltou = new LeafButton( "Faltou" );
	
	@ActionActivation(onSelect = ANULAR_EXAME_MARCACAO, onChange = "")
	public final LeafButton buttonExameMarcacaoAnular = new LeafButton( "Anular" );

	// email marcacao exame

	// observacoes marcacao exame

	// control

	public ProcessoAccoesPanel(LeafWindow parentWindow)
	{
		this.parentWindow = parentWindow;
		setupLayout();
		setupComponents();
		placeComponents();
	}

	private void setupLayout()
	{
		TableLayout layout = new TableLayout( new double[] {
			TableLayout.FILL
		}, new double[] {
			TableLayout.FILL
		} );
		this.setLayout( layout );
		cardPanel.setLayout( cardLayout );
	}

	private void setupComponents()
	{
		this.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), "Ac" + ccedil + otilde + "es" ) );
		setupStartPanel();
		setupTrabalhadorPanel();
		setupProcessoPanel();
		setupConsultaPanel();
		setupConsultaMarcacaoPanel();
		setupExamePanel();
		setupExameMarcacaoPanel();
	}

	private void placeComponents()
	{
		cardPanel.add( panelStart, PANEL_START_NAME );
		cardPanel.add( panelTrabalhador, PANEL_TRABALHADOR_NAME );
		cardPanel.add( panelProcesso, PANEL_PROCESSO_NAME );
		cardPanel.add( panelConsulta, PANEL_CONSULTA_NAME );
		cardPanel.add( panelConsultaMarcacao, PANEL_CONSULTA_MARCACAO_NAME );
		cardPanel.add( panelExame, PANEL_EXAME_NAME );
		cardPanel.add( panelExameMarcacao, PANEL_EXAME_MARCACAO_NAME );
		this.add( cardPanel, new TableLayoutConstraints( 0, 0 ) );
	}

	@LeafUIActionBinding(action = {
			LOAD_TRABALHADOR, SELECT_TRABALHADOR
	})
	public void setForTrabalhador( Trabalhadores trabalhador )
	{
		if( trabalhador != null )
		{
			boolean processoAberto = false;
			for( TrabalhadoresProcesso processo : (List<TrabalhadoresProcesso>) trabalhador.getTrabalhadoresProcessoArray() )
			{
				if( ProcessoConstants.PROCESSO_ABERTO_CODE.equals( processo.getEstado() ) )
				{
					processoAberto = true;
					break;
				}
			}
			buttonNovoTrabalhadorProcesso.setEnabled( !processoAberto );
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_START_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_PROCESSO)
	public void setForProcesso( TrabalhadoresProcesso processo )
	{
		if( processo != null )
		{
			buttonFecharProcesso.setEnabled( !ProcessoConstants.PROCESSO_FECHADO_CODE.equals( processo.getEstado() ) );
			buttonApagarProcesso.setEnabled( processo.isEmpty() );
			boolean consultaAberta = false;
			for( TrabalhadoresConsultas consulta : (List<TrabalhadoresConsultas>) processo.getTrabalhadoresConsultasArray() )
			{
				if( new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ).equals( consulta.getEstado() ) )
				{
					consultaAberta = true;
					break;
				}
			}
			buttonNovoProcessoConsulta.setEnabled( !consultaAberta );

			cardLayout.show( cardPanel, PANEL_PROCESSO_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_CONSULTA)
	public void setForConsulta( TrabalhadoresConsultas consulta )
	{
		if( consulta != null )
		{
			boolean marcacaoAberta = false;
			for( TrabalhadoresConsultasDatas marcacao : (List<TrabalhadoresConsultasDatas>) consulta.getTrabalhadoresConsultasDatasArray() )
			{
				if( new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ).equals( marcacao.getEstado() ) )
				{
					marcacaoAberta = true;
					break;
				}
			}
			buttonNovoConsultaMarcacao.setEnabled( !marcacaoAberta && !new Integer( MedicinaConstants.ESTADO_REALIZADO ).equals( consulta.getEstado() ) );
			cardLayout.show( cardPanel, PANEL_CONSULTA_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_CONSULTA_MARCACAO)
	public void setForConsultaMarcacao( TrabalhadoresConsultasDatas marcacao )
	{
		if( marcacao != null )
		{
			boolean porRealizar = new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ).equals( marcacao.getEstado() );
			List list = marcacao.getTrabalhadoresConsultasDatasObservacoesArray();
			boolean noObservacoes = (list == null || list.size() == 0) ? true : false;
			buttonConsultaMarcacaoDesmarcarSiprp.setEnabled( porRealizar );
			buttonConsultaMarcacaoDesmarcarTrabalhador.setEnabled( porRealizar );
			buttonConsultaMarcacaoFaltou.setEnabled( porRealizar );
			buttonConsultaMarcacaoRealizar.setEnabled( porRealizar );
			buttonNovoConsultaMarcacaoObservacoes.setEnabled( noObservacoes );
			cardLayout.show( cardPanel, PANEL_CONSULTA_MARCACAO_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_CONSULTA_MARCACAO_EMAIL)
	public void setForConsultaMarcacaoEmail( TrabalhadoresConsultasDatasEmails email )
	{
		if( email != null )
		{
			cardLayout.show( cardPanel, PANEL_START_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_CONSULTA_MARCACAO_OBSERVACAO)
	public void setForConsultaMarcacaoObservacao( TrabalhadoresConsultasDatasObservacoes obs )
	{
		if( obs != null )
		{
			cardLayout.show( cardPanel, PANEL_START_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_EXAME)
	public void setForExame( TrabalhadoresEcds exame )
	{
		if( exame != null )
		{
			boolean porMarcar = new Integer( MedicinaConstants.ESTADO_POR_MARCAR ).equals( exame.getEstado() );
			buttonNovoExameMarcacao.setEnabled( porMarcar );
			cardLayout.show( cardPanel, PANEL_EXAME_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_EXAME_MARCACAO)
	public void setForExameMarcacao( TrabalhadoresEcdsDatas marcacao )
	{
		if( marcacao != null )
		{
			boolean porRealizar = new Integer( MedicinaConstants.ESTADO_POR_REALIZAR ).equals( marcacao.getEstado() );
			List list = marcacao.getTrabalhadoresEcdsDatasObservacoesArray();
			boolean noObservacoes = (list == null || list.size() == 0) ? true : false;
			buttonExameMarcacaoDesmarcarSiprp.setEnabled( porRealizar );
			buttonExameMarcacaoDesmarcarTrabalhador.setEnabled( porRealizar );
			buttonExameMarcacaoFaltou.setEnabled( porRealizar );
			buttonExameMarcacaoRealizar.setEnabled( porRealizar );
			buttonExameMarcacaoRealizarParcial.setEnabled( porRealizar );
			buttonExameMarcacaoAnular.setEnabled( porRealizar );
			buttonNovoExameMarcacaoObservacoes.setEnabled( noObservacoes );
			cardLayout.show( cardPanel, PANEL_EXAME_MARCACAO_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_EXAME_MARCACAO_EMAIL)
	public void setForExameMarcacaoEmail( TrabalhadoresEcdsDatasEmails email )
	{
		if( email != null )
		{
			cardLayout.show( cardPanel, PANEL_START_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	@LeafUIActionBinding(action = SELECT_EXAME_MARCACAO_OBSERVACAO)
	public void setForExameMarcacaoObservacao( TrabalhadoresEcdsDatasObservacoes obs )
	{
		if( obs != null )
		{
			cardLayout.show( cardPanel, PANEL_START_NAME );
		}
		else
		{
			cardLayout.show( cardPanel, PANEL_TRABALHADOR_NAME );
		}
	}

	private void setupStartPanel()
	{
	}

	private void setupTrabalhadorPanel()
	{
		setupSimpleActionsPanel( panelTrabalhador, buttonNovoTrabalhadorProcesso, new JPanel() );
	}

	private void setupProcessoPanel()
	{
		JPanel panelTopProcesso = new JPanel();
		JPanel panelBottomProcesso = new JPanel();
		setupSimpleActionsPanel( panelTopProcesso, buttonNovoProcessoConsulta, buttonNovoProcessoExame );
		setupSimpleActionsPanel( panelBottomProcesso, buttonFecharProcesso, buttonApagarProcesso );
		setupTopBottomSimpleActionsPanel( panelProcesso, panelTopProcesso, panelBottomProcesso );
	}

	private void setupConsultaPanel()
	{
		setupSimpleActionsPanel( panelConsulta, buttonNovoConsultaMarcacao, new JPanel() );
	}

	private void setupConsultaMarcacaoPanel()
	{
		JPanel consultaMarcacaoUpperPanel = new JPanel();
		JPanel consultaMarcacaoLowerPanel = new JPanel();
		setupSimpleActionsPanel( consultaMarcacaoUpperPanel, buttonNovoConsultaMarcacaoEmail, buttonNovoConsultaMarcacaoObservacoes );
		setupSimpleActionsPanel( consultaMarcacaoLowerPanel, buttonConsultaMarcacaoRealizar, buttonConsultaMarcacaoDesmarcarSiprp, buttonConsultaMarcacaoDesmarcarTrabalhador, buttonConsultaMarcacaoFaltou);
		setupTopBottomSimpleActionsPanel(panelConsultaMarcacao, consultaMarcacaoUpperPanel, consultaMarcacaoLowerPanel);
	}
	
	private void setupExameMarcacaoPanel()
	{
		JPanel exameMarcacaoUpperPanel = new JPanel();
		JPanel exameMarcacaoLowerPanel = new JPanel();
		setupSimpleActionsPanel( exameMarcacaoUpperPanel, buttonNovoExameMarcacaoEmail, buttonNovoExameMarcacaoObservacoes, new JPanel() );
		setupSimpleActionsPanel( exameMarcacaoLowerPanel, buttonExameMarcacaoRealizar, buttonExameMarcacaoRealizarParcial, buttonExameMarcacaoFaltou, buttonExameMarcacaoAnular, buttonExameMarcacaoDesmarcarTrabalhador, buttonExameMarcacaoDesmarcarSiprp );
		setupTopBottomSimpleActionsPanel(panelExameMarcacao, exameMarcacaoUpperPanel, exameMarcacaoLowerPanel);
	}

	private void setupExamePanel()
	{
		setupSimpleActionsPanel( panelExame, buttonNovoExameMarcacao, new JPanel() );
	}
	
	private void setupTopBottomSimpleActionsPanel(JPanel panel, JPanel top, JPanel bottom)
	{
		TableLayout layout = new TableLayout(new double[]{TableLayout.FILL}, new double[]{TableLayout.MINIMUM, TableLayout.FILL,TableLayout.MINIMUM});
		panel.setLayout( layout );
		panel.add( top, new TableLayoutConstraints(0,0) );
		panel.add( new JPanel(), new TableLayoutConstraints(0,1) );
		panel.add( bottom, new TableLayoutConstraints(0,2) );
	}

	private void setupSimpleActionsPanel( JPanel panel, JComponent... field )
	{
		double[] cols = new double[] {
			FILL
		};
		double[] rows = new double[field.length];
		for( int i = 0; i < field.length; rows[i++] = MINIMUM )
			;
		rows[rows.length - 1] = FILL;
		TableLayout layout = new TableLayout( cols, rows );
		panel.setLayout( layout );

		for( int i = 0; i < field.length; ++i )
		{
			panel.add( field[i], new TableLayoutConstraints( 0, i ) );
		}
	}
}
