package siprp.higiene.relatorio;

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.util.Collections;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;

import leaf.ui.SimpleTable;
import siprp.database.cayenne.objects.HsRelatorio;
import siprp.database.cayenne.objects.HsRelatorioLegislacao;

import com.evolute.utils.tables.ColumnizedObjectArray;

public class RelatorioLegislacaoPanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	
	private HsRelatorio relatorio = null;
	
	private final SimpleTable<ColumnizedObjectArray> table = new SimpleTable<ColumnizedObjectArray>( "Categoria", "Legisla" + ccedil + atilde + "o" );

	public RelatorioLegislacaoPanel()
	{
		startupComponents();
		setupLayout();
		placeComponents();
	}
	
	private void startupComponents()
	{
		table.getTable().fixColumnWidth(0,200);
	}
	
	private void setupLayout()
	{
		TableLayout layout = new TableLayout( new double[]{ TableLayout.FILL }, new double[]{ TableLayout.FILL } );
		this.setLayout( layout );
	}
	
	private void placeComponents()
	{
		this.add( table, new TableLayoutConstraints( 0, 0 ) );
	}
	
	public void setRelatorio( HsRelatorio relatorio )
	{
		this.relatorio = relatorio;
		refresh();
	}
	
	private void refresh()
	{
		table.clear();
		if( relatorio != null )
		{
			List<HsRelatorioLegislacao> legislacoes = relatorio.getHsRelatorioLegislacaoArray();
			Collections.sort( legislacoes );
			Vector<ColumnizedObjectArray> rows = new Vector<ColumnizedObjectArray>();
			for( HsRelatorioLegislacao legislacao : legislacoes )
			{
				rows.add( new ColumnizedObjectArray( new Object[]{ legislacao, legislacao.getCategoria(), legislacao.getDescricao() }, true ) );
			}
			table.setRows( rows );
		}
	}

}
