package siprp.higiene.gestao;

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

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import leaf.ui.LeafDialog;

import siprp.database.cayenne.objects.Empresas;
import siprp.database.cayenne.providers.MedicinaDAO;

public class EmpresaPanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	
	private final JComboBox comboEmpresa = new JComboBox();
	
	private final JCheckBox checkImpressao = new JCheckBox("Imprimir tabela alargada");
	
	private final GerirEmpresaPanel panelEmpresa = new GerirEmpresaPanel();
	
	private final GerirEstabelecimentosPanel panelEstabelecimentos = new GerirEstabelecimentosPanel();
	
	private final JTabbedPane tabs = new JTabbedPane();
	
	private Empresas empresa = null;

	public EmpresaPanel()
	{
		startupListeners();
		startupComponents();
		setupLayout();
		placeComponents();
		
	}
	
	private void startupComponents()
	{
		List<Empresas> empresas = new MedicinaDAO().getAllEmpresas();
		if( empresas != null )
		{
			for( Empresas empresa : empresas )
			{
				comboEmpresa.addItem( empresa );
			}
		}
		tabs.addTab( "Empresa", panelEmpresa );
		tabs.addTab( "Estabelecimentos", panelEstabelecimentos );
	}
	
	private void setupLayout()
	{
		double[] cols = new double[]{
		                             TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM
		};
		double[] rows = new double[]{
		                             TableLayout.MINIMUM, TableLayout.FILL,
		};
		TableLayout layout = new TableLayout(cols,rows);
		layout.setHGap( 10 );
		layout.setVGap( 10 );
		this.setLayout( layout );
	}
	
	private void placeComponents()
	{
		this.add( new JLabel( "Empresa" ), new TableLayoutConstraints( 0, 0 ) );
		this.add( comboEmpresa, new TableLayoutConstraints( 1, 0 ) );
		this.add( checkImpressao, new TableLayoutConstraints( 2, 0 ) );
		this.add( tabs, new TableLayoutConstraints( 0, 1, 2, 1 ) );
	}
	
	private void setEmpresa( Empresas empresa )
	{
		this.empresa = empresa;
		checkImpressao.setSelected( false );
		if( empresa != null )
		{
			checkImpressao.setSelected( empresa.getImprimirTabelaAlargada() == null ? false : empresa.getImprimirTabelaAlargada() );
		}
	}
	
	private void startupListeners()
	{
		checkImpressao.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				try
				{
					empresa.setImprimirTabelaAlargada( checkImpressao.isSelected() );
					empresa.save();
				}
				catch( Exception ex )
				{
					LeafDialog.error( ex );
				}
			}
		} );
		comboEmpresa.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				Object item = e.getItem();
				if( item != null && ItemEvent.SELECTED == e.getStateChange() )
				{
					if( item instanceof Empresas )
					{
						panelEmpresa.setEmpresa( (Empresas) item );
						panelEstabelecimentos.setEmpresa( (Empresas) item );
						EmpresaPanel.this.setEmpresa( (Empresas) item );
					}
				}
			}
		});
	}

}
