package siprp.higiene.relatorio;

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.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import leaf.ui.LeafError;
import siprp.database.cayenne.objects.Empresas;
import siprp.database.cayenne.objects.Estabelecimentos;
import siprp.database.cayenne.objects.HsRelatorio;
import siprp.database.cayenne.objects.MarcacoesEstabelecimento;
import siprp.logic.HigieneSegurancaLogic;

import com.evolute.utils.tracker.TrackableWindow;

public class RelatorioHigieneSegurancaWindow extends JFrame implements TrackableWindow
{
	private static final long serialVersionUID = 1L;
	
	public static final Color COLOR_RISCO_OK = new Color(200,255,200);
	
	public static final Color COLOR_RISCO_OK_SEL = new Color(140,240,140);
	
	public static final Color COLOR_MEDIDA_OK = new Color(200,230,255);
	
	public static final Color COLOR_MEDIDA_OK_SEL = new Color(160,190,255);
	
	public static final String TITLE = "Relat" + oacute + "rios de Higiene e Seguran" + ccedil + "a no Trabalho";
	
	public static final Dimension SIZE = new Dimension( 1024, 700 );
	
	private final JComboBox comboEmpresas = new JComboBox();
	
	private final JComboBox comboEstabelecimentos = new JComboBox();
	
	private final JComboBox comboVisitas = new JComboBox();
	
	private final PanelRelatorio panelRelatorio = new PanelRelatorio();
	
	public RelatorioHigieneSegurancaWindow()
	{
		setupListeners();
		startupComponents();
		startupLayout();
		placeComponents();
		setLocationRelativeTo( null );
		setTitle( TITLE );
		setSize( SIZE );
	}

	private void startupComponents()
	{
		panelRelatorio.setBorder( BorderFactory.createTitledBorder( "Relat" + oacute + "rio" ) );
		for( Empresas empresa : HigieneSegurancaLogic.getAllEmpresas())
		{
			comboEmpresas.addItem( empresa );
		}
	}
	
	private void startupLayout()
	{
		TableLayout layout = new TableLayout(
				new double[]{ TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM, TableLayout.FILL },
				new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL }
				);
		layout.setVGap( 5 );
		layout.setHGap( 5 );
		getContentPane().setLayout( layout );
	}
	
	private void placeComponents()
	{
		Container pane = getContentPane();
		pane.add( new JLabel("Empresa"), new TableLayoutConstraints( 0, 0 ) );
		pane.add( comboEmpresas, new TableLayoutConstraints( 1, 0, 3, 0 ) );
		pane.add( new JLabel("Estabelecimento"), new TableLayoutConstraints( 0, 1 ) );
		pane.add( comboEstabelecimentos, new TableLayoutConstraints( 1, 1 ) );
		pane.add( new JLabel("Visita"), new TableLayoutConstraints( 2, 1 ) );
		pane.add( comboVisitas, new TableLayoutConstraints( 3, 1 ) );
		pane.add( panelRelatorio, new TableLayoutConstraints(0,2,3,2));
	}
	
	private void setupListeners()
	{
		comboEmpresas.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				if( e.getStateChange() == ItemEvent.SELECTED )
				{
					fillEstabelecimentos( (Empresas) e.getItem() );
				}
			}
		} );
		comboEstabelecimentos.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				if( e.getStateChange() == ItemEvent.SELECTED )
				{
					fillVisitas( (Estabelecimentos) e.getItem() );
				}
			}
		} );
		comboVisitas.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				if( e.getStateChange() == ItemEvent.SELECTED )
				{
					fillRelatorio( (MarcacoesEstabelecimento) e.getItem() );
				}
			}
		} );
	}
	
	private void fillEstabelecimentos( Empresas empresa )
	{
		comboEstabelecimentos.removeAllItems();
		if( empresa != null )
		{
			for( Estabelecimentos estabelecimento : empresa.getEstabelecimentosArray() )
			{
				comboEstabelecimentos.addItem( estabelecimento );
			}
		}
	}
	
	private void fillVisitas( Estabelecimentos estabelecimento )
	{
		comboVisitas.removeAllItems();
		if( estabelecimento != null )
		{
			for( MarcacoesEstabelecimento visita : estabelecimento.getMarcacoesEstabelecimentoArray() )
			{
				if( "y".equals( visita.getRealizada() ) )
				{
					comboVisitas.addItem( visita );
				}
			}
		}
	}
	
	private void fillRelatorio( MarcacoesEstabelecimento visita )
	{
		try
		{
			boolean carregarDadosEstabelecimento = false;
			panelRelatorio.setRelatorio( null, carregarDadosEstabelecimento );
			if( visita != null )
			{
				HsRelatorio relatorio = null;
				List<HsRelatorio> relatorios = visita.getHsRelatorioArray();
				Date data = null;
				for( HsRelatorio current : relatorios )
				{
					boolean newest = data == null ? true : (data.before( current.getData() ) );
					if( newest )
					{
						data = current.getData();
						relatorio = current;
					}
				}
				if( relatorio == null )
				{
					relatorio = new HsRelatorio();
					relatorio.setData( new Date() );
					relatorio.setToHsMarcacoesEstabelecimento( visita );
					relatorio.save();
					carregarDadosEstabelecimento = true;
				}
				panelRelatorio.setRelatorio( relatorio, carregarDadosEstabelecimento );;
			}
		} catch( Exception e )
		{
			LeafError.error(e);
		}
	}
	
	@Override
	public void open()
	{
		setVisible( true );
	}

	public void close()
	{
		SwingUtilities.invokeLater( new Runnable()
		{
			public void run()
			{
				setVisible( false );
				dispose();
			}
		} );
	}

	@Override
	public boolean closeIfPossible()
	{
		close();
		return true;
	}

	@Override
	public void refresh()
	{
		
	}

	public static void main( String[] args )
	{
		RelatorioHigieneSegurancaWindow window = new RelatorioHigieneSegurancaWindow();
		window.setVisible( true );
		window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}
}
