package siprp.higiene.relatorio;

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;

import leaf.ui.SimpleTable;
import siprp.database.cayenne.objects.Estabelecimentos;
import siprp.database.cayenne.objects.HsRelatorio;
import siprp.database.cayenne.objects.HsRelatorioNormalizacao;
import siprp.database.cayenne.objects.MarcacoesEstabelecimento;
import siprp.logic.HigieneSegurancaLogic;

import com.evolute.utils.error.ErrorLogger;
import com.evolute.utils.tables.ColumnizedObjectArray;

public class RelatorioNormalizacaoPanel extends JPanel
{

	private static final long serialVersionUID = 1L;

	private HsRelatorio relatorio = null;
	
	private final JButton revert = new JButton("Reverter");
	
	private final SimpleTable<ColumnizedObjectArray> table = new SimpleTable<ColumnizedObjectArray>( "C" + oacute + "digo", "Normaliza" + ccedil + atilde + "o", "" );
	
	public RelatorioNormalizacaoPanel()
	{
		startupComponents();
		setupLayout();
		placeComponents();
	}
	
	private void startupComponents()
	{
		table.getTable().fixColumnWidth(0,50);
		table.getTable().fixColumnWidth(2,100);
		revert.addActionListener( new ActionListener()
		{
		
			@Override
			public void actionPerformed( ActionEvent e )
			{
				if( relatorio != null )
				{
					MarcacoesEstabelecimento marcacao = relatorio.getToHsMarcacoesEstabelecimento();
					if( marcacao != null )
					{
						Estabelecimentos estabelecimento = marcacao.getToEstabelecimentos();
						if( estabelecimento != null )
						{
							try
							{
								HigieneSegurancaLogic.reverterNormalizacao( relatorio, estabelecimento );
							} catch( Exception e1 )
							{
								ErrorLogger.logException( e1 );
							}
						}
					}
					refresh();
				}
			}
		} );
	}
	
	private void setupLayout()
	{
		TableLayout layout = new TableLayout( new double[]{ TableLayout.MINIMUM, TableLayout.FILL }, new double[]{ TableLayout.MINIMUM, TableLayout.FILL } );
		this.setLayout( layout );
	}
	
	private void placeComponents()
	{
		this.add( table, new TableLayoutConstraints( 0, 1,1,1 ) );
		this.add( revert, new TableLayoutConstraints( 0, 0 ) );
	}
	
	public void setRelatorio( HsRelatorio relatorio )
	{
		this.relatorio = relatorio;
		refresh();
	}
	
	private void refresh()
	{
		table.clear();
		if( relatorio != null )
		{
			List<HsRelatorioNormalizacao> normalizacoes = relatorio.getHsRelatorioNormalizacaoArray();
			Collections.sort( normalizacoes );
			Vector<ColumnizedObjectArray> rows = new Vector<ColumnizedObjectArray>();
			for( HsRelatorioNormalizacao normalizacao : normalizacoes )
			{
				rows.add( new ColumnizedObjectArray( new Object[]{ normalizacao, normalizacao.getCodigo(), normalizacao.getDescricao(), normalizacao.getPortuguesa() ? "Portuguesa" : "Internacional" }, true ) );
			}
			table.setRows( rows );
		}
	}
	
}
