package siprp.higiene.gestao;

import static com.evolute.utils.strings.UnicodeLatin1Map.atilde;
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

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

import siprp.database.cayenne.objects.Empresas;
import siprp.database.cayenne.objects.Estabelecimentos;
import siprp.database.cayenne.objects.HsPosto;
import siprp.higiene.gestao.legislacao.AdicionarLegislacaoPanel;
import siprp.higiene.gestao.normalizacao.AdicionarNormalizacaoPanel;
import siprp.higiene.gestao.postos.AdicionarAreasPanel;
import siprp.higiene.gestao.riscos.AdicionarRiscosPanel;

public class GerirEstabelecimentosPanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	
	private final JComboBox comboEstabelecimentos = new JComboBox();
	private final JPanel panelAreasRiscos = new JPanel();
	private final AdicionarAreasPanel panelAreas = new AdicionarAreasPanel();
	private final AdicionarRiscosPanel panelRiscos = new AdicionarRiscosPanel();
	private final AdicionarEmailsPanel panelEmails = new AdicionarEmailsPanel();
	private final AdicionarLegislacaoPanel panelLegislacao = new AdicionarLegislacaoPanel();
	private final AdicionarNormalizacaoPanel panelNormalizacao = new AdicionarNormalizacaoPanel();

	public GerirEstabelecimentosPanel()
	{
		startupComponents();
		setupLayout();
		placeComponents();
		startupListeners();
	}
	
	private void startupComponents()
	{
		panelAreasRiscos.setBorder( BorderFactory.createTitledBorder( "Riscos por posto de trabalho" ) );
		panelRiscos.setBorder( BorderFactory.createTitledBorder( "Riscos" ) );
		panelEmails.setBorder( BorderFactory.createTitledBorder( "Emails" ) );
		panelLegislacao.setBorder( BorderFactory.createTitledBorder( "Legisla" + ccedil + atilde + "o" ) );
		panelEmails.setPreferredSize( new Dimension(0,150) );
	}
	
	private void setupLayout()
	{
		double [] cols = new double[]{ TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, };
		double [] rows = new double[]{ TableLayout.MINIMUM, TableLayout.FILL,TableLayout.FILL, TableLayout.PREFERRED };
		TableLayout layout = new TableLayout( cols, rows );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		this.setLayout( layout );
		
		panelAreasRiscos.setLayout( new GridLayout( 1, 2 ) );
	}
	
	private void placeComponents()
	{
		panelAreasRiscos.add( panelAreas );
		panelAreasRiscos.add( panelRiscos );
		
		this.add( comboEstabelecimentos, new TableLayoutConstraints( 0, 0, 2, 0 ) );
		this.add( panelAreasRiscos, new TableLayoutConstraints( 0, 1, 1, 3 ) );
		this.add( panelLegislacao, new TableLayoutConstraints( 2, 1 ) );
		this.add( panelNormalizacao, new TableLayoutConstraints( 2, 2 ) );
		this.add( panelEmails, new TableLayoutConstraints( 2, 3 ) );
	}
	
	private void startupListeners()
	{
		comboEstabelecimentos.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				if( e.getItem() != null && ItemEvent.SELECTED == e.getStateChange() )
				{
					if( e.getItem() instanceof Estabelecimentos )
					{
						Estabelecimentos estabelecimento = (Estabelecimentos) e.getItem();
						panelAreas.setEstabelecimento( estabelecimento );
						panelEmails.setEstabelecimento( estabelecimento );
						panelLegislacao.setEstabelecimento( estabelecimento );
						panelNormalizacao.setEstabelecimento( estabelecimento );
					}
				}
			}
		} );
		panelAreas.addPropertyChangeListener( AdicionarAreasPanel.SELECTION_CHANGED, new PropertyChangeListener()
		{
			@Override
			public void propertyChange( PropertyChangeEvent evt )
			{
				Object obj = evt.getNewValue();
				if( obj != null && (obj instanceof HsPosto) )
				{
					panelRiscos.setPosto( (HsPosto) obj );
				}
				else
				{
					panelRiscos.setPosto( null );
				}
			}
		} );
	}

	public void setEmpresa( Empresas empresa )
	{
		comboEstabelecimentos.removeAllItems();
		if( empresa != null )
		{
			for( Estabelecimentos estabelecimento : empresa.getEstabelecimentosArray() )
			{
				comboEstabelecimentos.addItem( estabelecimento );
			}
			if( comboEstabelecimentos.getItemCount() > 0 )
			{
				comboEstabelecimentos.setSelectedIndex( 0 );
			}
		}
	}
	
}
