package siprp.medicina.processo.ui;

import static siprp.logic.SIPRPLogic.ACTION_STARTUP;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_EMPRESA;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.SELECT_ESTABELECIMENTO;
import static siprp.medicina.processo.logic.MedicinaProcessoLogic.LOAD_TRABALHADOR;

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

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

import siprp.logic.SIPRPLogic.LeafUIActionBinding;
import siprp.ui.SIPRPWindow;
import siprp.ui.SIPRPWindow.ActionActivation;

import com.evolute.utils.tables.BaseTable;
import com.evolute.utils.tables.ColumnizedMappable;
import com.evolute.utils.tables.VectorTableModel;

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

	private final SIPRPWindow parentWindow;

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

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

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

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

		setupComponents();
	}

	private void setupComponents()
	{
		empresasModel = new VectorTableModel( 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 VectorTableModel( 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 VectorTableModel( 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( Vector<ColumnizedMappable> allEmpresas )
	{
		empresasTable.clearSelection();
		empresasModel.clearAll();
		if( allEmpresas != null )
		{
			empresasModel.setValues( allEmpresas );
		}
	}

	@LeafUIActionBinding(action = SELECT_EMPRESA)
	public void fillEstablecimentos( Vector<ColumnizedMappable> allEstabelecimentos )
	{
		estabelecimentosTable.clearSelection();
		estabelecimentosModel.clearAll();
		if( allEstabelecimentos != null )
		{
			estabelecimentosModel.setValues( allEstabelecimentos );
		}
	}
	
	@LeafUIActionBinding(action = SELECT_ESTABELECIMENTO)
	public void fillTrabalhadores( Vector<ColumnizedMappable> allTrabalhadores )
	{
		trabalhadoresTable.clearSelection();
		trabalhadoresModel.clearAll();
		if( allTrabalhadores != null )
		{
			trabalhadoresModel.setValues( allTrabalhadores );
		}
	}

}
