package siprp.higiene.relatorio;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import leaf.ui.TreeTools;
import siprp.database.cayenne.objects.HsRelatorio;
import siprp.database.cayenne.objects.HsRelatorioPosto;
import siprp.database.cayenne.objects.HsRelatorioPostoRisco;
import siprp.logic.HigieneSegurancaLogic;
import siprp.logic.node.PostoRelatorioNode;

public class VerAreasRelatorioPanel extends JPanel
{

	private static final long serialVersionUID = 1L;

	public static final String SELECTION_CHANGED = "SELECTION_CHANGED";
	
	private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
	
	private final DefaultTreeModel model = new DefaultTreeModel( root );
	
	public final JTree tree = new JTree( model );
	
	private final JScrollPane scroll = new JScrollPane( tree );

	private HsRelatorio relatorio = null;

	public VerAreasRelatorioPanel()
	{
		startupComponents();
		setupLayout();
		placeComponents();
		startupListeners();
	}
	
	private void startupComponents()
	{
		tree.setRootVisible( false );
		tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
		tree.setCellRenderer(new DefaultTreeCellRenderer()
        {

			private static final long serialVersionUID = 1L;

			public Component getTreeCellRendererComponent(JTree pTree, Object pValue, boolean pIsSelected, boolean pIsExpanded, boolean pIsLeaf, int pRow, boolean pHasFocus)
             {
				super.getTreeCellRendererComponent(pTree, pValue, pIsSelected, pIsExpanded, pIsLeaf, pRow, pHasFocus);
				boolean highlight= false;
				Color highColor = null;
				Color lowColor = null;
				if( pValue instanceof PostoRelatorioNode )
				{
					Object userObject = ((PostoRelatorioNode)pValue).getUserObject();
					if( userObject instanceof HsRelatorioPosto )
					{
						HsRelatorioPosto posto = (HsRelatorioPosto) userObject;
						boolean allOk = true;
						for( HsRelatorioPostoRisco rel : posto.getHsRelatorioPostoRiscoArray() )
						{
							allOk &= HigieneSegurancaLogic.isRelatorioRiscoPreenchido( rel );
						}
						highColor = RelatorioHigieneSegurancaWindow.COLOR_RISCO_OK_SEL;
						lowColor = RelatorioHigieneSegurancaWindow.COLOR_RISCO_OK;
						highlight = allOk;
					}
				}
				if( highlight )
				{
					setOpaque( true );
					setBackground( pIsSelected ? highColor : lowColor );
				}
				else
				{
					setOpaque( false );
				}
			    return(this);
             }
			} );
	}
	
	private void setupLayout()
	{
		TableLayout layout = new TableLayout(
				new double[]{ TableLayout.FILL },
				new double[]{ TableLayout.MINIMUM, TableLayout.FILL }
				);
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		setLayout( layout );
	}
	
	private void placeComponents()
	{
		JPanel panel = new JPanel();
		TableLayout layout = new TableLayout(
				new double[]{ TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM },
				new double[]{ TableLayout.MINIMUM }
				);
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		panel.setLayout( layout );
			
		add( panel, new TableLayoutConstraints( 0, 0 ) );
		add( scroll, new TableLayoutConstraints( 0, 1 ) );
	}
	
	private void startupListeners()
	{
		tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener(){
			@Override
			public void valueChanged( TreeSelectionEvent e )
			{
				TreePath path = tree.getSelectionPath();
				Object object = path == null ? null : path.getLastPathComponent();
				HsRelatorioPosto posto = object == null ? null : ( (object instanceof PostoRelatorioNode) ? (HsRelatorioPosto) ((PostoRelatorioNode) object).getUserObject() : null); 
				firePropertyChange( SELECTION_CHANGED, null,  posto );
			}
		} );
	}
	
	public void refresh()
	{
		root.removeAllChildren();
		if( relatorio != null )
		{
			TreeTools.merge( root, HigieneSegurancaLogic.getAreasRelatorioTree( relatorio ) );
		}
		TreeTools.sort( root );
		TreeTools.refreshTree( tree, root );
	}


	public void setRelatorio( HsRelatorio relatorio )
	{
		this.relatorio  = relatorio;
		refresh();
	}
	
}
