package siprp.higiene.relatorio;

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

import java.awt.CardLayout;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import siprp.database.cayenne.objects.HsRelatorio;
import siprp.database.cayenne.objects.HsRelatorioArea;
import siprp.database.cayenne.objects.HsRelatorioPosto;
import siprp.database.cayenne.objects.HsRelatorioPostoMedida;
import siprp.database.cayenne.objects.HsRelatorioPostoRisco;
import siprp.logic.HigieneSegurancaLogic;

public class PlanoActuacaoPanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	
	private static final String RISCO_PANEL = "RISCO_PANEL";
	
	private static final String MEDIDA_PANEL = "MEDIDA_PANEL";
	
	private static final String EMPTY_PANEL = "EMPTY_PANEL";
	
	private final VerAreasRelatorioPanel areas = new VerAreasRelatorioPanel();
	
	private final GerirMedidasRelatorioPanel riscos = new GerirMedidasRelatorioPanel();
	
	private final GerirValoresRiscoPanel valoresRisco = new GerirValoresRiscoPanel();
	
	private final GerirMedidaRelatorioPanel valoresMedida = new GerirMedidaRelatorioPanel();
	
	private final JPanel emptyPanel = new JPanel();
	
	private final JPanel valoresPanel = new JPanel();
	
	private final CardLayout cards = new CardLayout();
	
	

	private HsRelatorio relatorio = null;

	public PlanoActuacaoPanel()
	{
		startupListeners();
		startupComponents();
		startupLayout();
		placeComponents();
		cards.show( valoresPanel, EMPTY_PANEL );
	}
	
	private void startupListeners()
	{
		areas.addPropertyChangeListener( VerAreasRelatorioPanel.SELECTION_CHANGED, new PropertyChangeListener()
		{
			@Override
			public void propertyChange( PropertyChangeEvent evt )
			{
				if( evt.getNewValue() instanceof HsRelatorioPosto )
				{
					riscos.setPosto( (HsRelatorioPosto) evt.getNewValue() );
				}
				else
				{
					riscos.setPosto( null );
				}
			}
		} );
		riscos.addPropertyChangeListener( GerirMedidasRelatorioPanel.SELECTION_CHANGED, new PropertyChangeListener()
		{
			@Override
			public void propertyChange( PropertyChangeEvent evt )
			{
				if( evt.getNewValue() instanceof HsRelatorioPostoRisco )
				{
					cards.show( valoresPanel, RISCO_PANEL );
					valoresRisco.setRelatorioPostoRisco( (HsRelatorioPostoRisco) evt.getNewValue() );
				}
				else if( evt.getNewValue() instanceof HsRelatorioPostoMedida )
				{
					cards.show( valoresPanel, MEDIDA_PANEL );
					valoresMedida.setMedida( (HsRelatorioPostoMedida) evt.getNewValue() );
				}
				else
				{
					cards.show( valoresPanel, EMPTY_PANEL );
					valoresRisco.setRelatorioPostoRisco( null );
					valoresMedida.setMedida( null );
				}
			}
		} );
		valoresRisco.addPropertyChangeListener( GerirValoresRiscoPanel.VALUE_CHANGED, new PropertyChangeListener()
		{
			@Override
			public void propertyChange( PropertyChangeEvent evt )
			{
				riscos.repaint();
				areas.repaint();
			}
		} );
		valoresMedida.addPropertyChangeListener( GerirMedidaRelatorioPanel.MEDIDA_CHANGED, new PropertyChangeListener()
		{
			@Override
			public void propertyChange( PropertyChangeEvent evt )
			{
				riscos.refresh();
			}
		} );
	}
	
	private void startupComponents()
	{
		riscos.setBorder( BorderFactory.createTitledBorder( "Riscos" ) );
		areas.setBorder( BorderFactory.createTitledBorder( "Postos de trabalho" ) );
		areas.setPreferredSize( new Dimension(250,0) );
	}
	
	private void startupLayout()
	{
		TableLayout layout = new TableLayout(
				new double[]{ TableLayout.MINIMUM, TableLayout.PREFERRED, TableLayout.FILL, TableLayout.FILL },
				new double[]{ TableLayout.MINIMUM, TableLayout.FILL }
		);
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		setLayout( layout );
		valoresPanel.setLayout(cards);
	}
	
	private void placeComponents()
	{
		valoresPanel.add( valoresRisco, RISCO_PANEL );
		valoresPanel.add( valoresMedida, MEDIDA_PANEL );
		valoresPanel.add( emptyPanel, EMPTY_PANEL );
		add( areas, new TableLayoutConstraints( 0, 1, 1, 1 ) );
		add( riscos, new TableLayoutConstraints( 2, 1 ) );
		add( valoresPanel, new TableLayoutConstraints( 3, 1 ) );
	}

	private void setEnabled()
	{
		
	}
	
	public void setRelatorio( HsRelatorio relatorio )
	{
		this.relatorio  = relatorio;
		areas.setRelatorio( relatorio );
		riscos.setPosto( null );
		valoresRisco.setRelatorioPostoRisco( null );
		valoresRisco.setEnabled( relatorio != null && relatorio.getIsSubmetido() == null );
		valoresMedida.setMedida( null );
		valoresMedida.setEnabled( relatorio != null && relatorio.getIsSubmetido() == null );
		setEnabled( );
	}

	public boolean isValidPlano()
	{
		boolean result = true;
		if( relatorio != null )
		{
			for( HsRelatorioArea area : relatorio.getHsRelatorioArea() )
			{
				for( HsRelatorioPosto posto : area.getHsRelatorioPostoArray() )
				{
					for( HsRelatorioPostoRisco relRisco : posto.getHsRelatorioPostoRiscoArray() )
					{
						boolean preenchido = relRisco.getToHsRelatorioRiscoValorQualitativo() != null;
						if( !preenchido )
						{
							preenchido = HigieneSegurancaLogic.isProbabilidadeValida( relRisco.getProbabilidade() ) && HigieneSegurancaLogic.isSeveridadeValida( relRisco.getSeveridade() );	
						}
						if( !preenchido )
						{
							result = false;
							break;
						}
					}
					if( !result )
					{
						break;
					}
				}
				if( !result )
				{
					break;
				}
			}
		}
		return result;
	}
	
}

