package siprp.medicina.processo.ui;

import static leaf.ui.LeafLogic.ACTION_STARTUP;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.LOAD_TRABALHADOR;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_EMPRESA;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_ESTABELECIMENTO;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Collection;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import leaf.ui.LeafTableModel;
import leaf.ui.LeafWindow;
import leaf.ui.LeafLogic.LeafUIActionBinding;
import leaf.ui.LeafWindow.ActionActivation;
import siprp.database.cayenne.objects.Empresas;
import siprp.database.cayenne.objects.Estabelecimentos;

import com.evolute.utils.tables.BaseTable;

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

	private final LeafWindow parentWindow;

	@ActionActivation(onSelect = SELECT_EMPRESA, onChange = "")
	public BaseTable empresasTable;
	public LeafTableModel empresasModel;

	@ActionActivation(onSelect = SELECT_ESTABELECIMENTO, onChange = "")
	public BaseTable estabelecimentosTable;
	public LeafTableModel estabelecimentosModel;

	@ActionActivation(onSelect = LOAD_TRABALHADOR, onChange = "")
	public BaseTable trabalhadoresTable;
	public LeafTableModel trabalhadoresModel;

	public TrabalhadoresChooserPanel(LeafWindow parentWindow)
	{
		super();
		this.parentWindow = parentWindow;

		setupComponents();
	}

	private void setupComponents()
	{
		empresasModel = new LeafTableModel( new String[] {
			"Empresas"
		} );
		empresasTable = new BaseTable( empresasModel );
		empresasTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		empresasTable.setNonResizableNorReordable();
		JScrollPane empresasScroll = new JScrollPane( empresasTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		estabelecimentosModel = new LeafTableModel( new String[] {
			"Estabelecimentos"
		} );
		estabelecimentosTable = new BaseTable( estabelecimentosModel );
		estabelecimentosTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		estabelecimentosTable.setNonResizableNorReordable();
		JScrollPane estabelecimentosScroll = new JScrollPane( estabelecimentosTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		trabalhadoresModel = new LeafTableModel( new String[] {
			"Trabalhadores"
		} );
		trabalhadoresTable = new BaseTable( trabalhadoresModel );
		trabalhadoresTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		trabalhadoresTable.setNonResizableNorReordable();
		JScrollPane trabalhadoresScroll = new JScrollPane( trabalhadoresTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );

		GridBagLayout gridbag = new GridBagLayout();
		setLayout( gridbag );
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets( 1, 1, 1, 1 );
		constraints.fill = GridBagConstraints.BOTH;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		constraints.weightx = 0.3;
		constraints.weighty = 1;

		gridbag.setConstraints( empresasScroll, constraints );

		gridbag.setConstraints( estabelecimentosScroll, constraints );

		constraints.weightx = 0.4;
		constraints.gridheight = GridBagConstraints.REMAINDER;
		gridbag.setConstraints( trabalhadoresScroll, constraints );

		add( empresasScroll );
		add( estabelecimentosScroll );
		add( trabalhadoresScroll );

	}
	
	@LeafUIActionBinding(action= ACTION_STARTUP)
	public void fillEmpresas( Collection<Empresas> allEmpresas )
	{
		empresasTable.clearSelection();
		empresasModel.clearAll();
		if( allEmpresas != null )
		{
			empresasModel.setValues( allEmpresas );
		}
	}

	@LeafUIActionBinding(action = SELECT_EMPRESA)
	public void fillEstablecimentos( Empresas empresa )
	{
		estabelecimentosTable.clearSelection();
		estabelecimentosModel.clearAll();
		if( empresa != null )
		{
			estabelecimentosModel.setValues( empresa.getEstabelecimentosArray() );
		}
	}
	
	@LeafUIActionBinding(action = SELECT_ESTABELECIMENTO)
	public void fillTrabalhadores(Estabelecimentos estabelecimento)
	{
		trabalhadoresTable.clearSelection();
		trabalhadoresModel.clearAll();
		if( estabelecimento != null )
		{
			trabalhadoresModel.setValues( estabelecimento.getTrabalhadoresArray() );
		}
	}

}
