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 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;
import siprp.ui.SIPRPWindow;
import siprp.ui.SIPRPWindow.ActionActivation;

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

	private final SIPRPWindow 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, onChange = "")
	public final LeafButton buttonNovoProcessoExame = new LeafButton( "Novo Exame" );

	// 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_CONSULTA_MARCACAO, onChange = "")
	public final LeafButton buttonConsultaMarcacaoDesmarcar = new LeafButton( "Desmarcar" );

	@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
	public final LeafButton buttonNovoExameMarcacao = new LeafButton( "Nova Marca" + ccedil + atilde + "o" );

	// marcacao exame
	public final LeafButton buttonNovoExameMarcacaoEmail = new LeafButton( "Enviar e-Mail" );
	public final LeafButton buttonNovoExameMarcacaoObservacoes = new LeafButton( "Adicionar Observa" + ccedil + atilde + "o" );

	// email marcacao exame

	// observacoes marcacao exame

	// control

	public final LeafButton buttonControlClose = new LeafButton( "Fechar" );

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

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

		cardPanel.setLayout( cardLayout );

		layout = new TableLayout( new double[] {
				TableLayout.FILL, TableLayout.FILL
		}, new double[] {
				TableLayout.MINIMUM, TableLayout.MINIMUM
		} );
		layout.setVGap( 5 );
		layout.setHGap( 5 );
		controlPanel.setLayout( layout );
	}

	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 );

		// controlPanel.add( buttonControlSave, new TableLayoutConstraints(0,0)
		// );
		// controlPanel.add( buttonControlRevert, new
		// TableLayoutConstraints(1,0) );
		controlPanel.add( buttonControlClose, new TableLayoutConstraints( 0, 1, 1, 1 ) );

		this.add( cardPanel, new TableLayoutConstraints( 0, 0 ) );
		this.add( new JPanel(), new TableLayoutConstraints( 0, 1 ) );
		this.add( controlPanel, new TableLayoutConstraints( 0, 2 ) );
	}

	@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() ) );
			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() );
			buttonConsultaMarcacaoDesmarcar.setEnabled( porRealizar );
			buttonConsultaMarcacaoFaltou.setEnabled( porRealizar );
			buttonConsultaMarcacaoRealizar.setEnabled( porRealizar );
//			buttonNovoConsultaMarcacaoObservacoes.setEnabled( );
			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 )
		{
			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 )
		{
			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()
	{
		// panelStart.setBorder( BorderFactory.createTitledBorder(
		// BorderFactory.createEtchedBorder(), "none" ) );
	}

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

	private void setupProcessoPanel()
	{
		setupSimpleActionsPanel( panelProcesso, buttonFecharProcesso, buttonNovoProcessoConsulta, buttonNovoProcessoExame, new JPanel() );
	}

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

	private void setupConsultaMarcacaoPanel()
	{
		setupSimpleActionsPanel( panelConsultaMarcacao, buttonNovoConsultaMarcacaoEmail, buttonNovoConsultaMarcacaoObservacoes, buttonConsultaMarcacaoRealizar, buttonConsultaMarcacaoDesmarcar, buttonConsultaMarcacaoFaltou, new JPanel() );
	}

	private void setupExamePanel()
	{
		setupSimpleActionsPanel( panelExame, buttonNovoExameMarcacao, new JPanel() );
	}

	private void setupExameMarcacaoPanel()
	{
		setupSimpleActionsPanel( panelExameMarcacao, buttonNovoExameMarcacaoEmail, buttonNovoExameMarcacaoObservacoes, new JPanel() );
	}

	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 ) );
		}
	}
}
