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.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;
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.LeafButton;
import leaf.ui.LeafDialog;
import leaf.ui.LeafIconButton;
import leaf.ui.TreeTools;
import siprp.database.cayenne.objects.HsRelatorioMedida;
import siprp.database.cayenne.objects.HsRelatorioPosto;
import siprp.database.cayenne.objects.HsRelatorioPostoMedida;
import siprp.database.cayenne.objects.HsRelatorioPostoRisco;
import siprp.database.cayenne.objects.HsRelatorioRisco;
import siprp.logic.HigieneSegurancaLogic;
import siprp.logic.node.MedidaRelatorioNode;
import siprp.logic.node.RiscoRelatorioNode;

public class GerirMedidasRelatorioPanel extends JPanel
{

	private static final long serialVersionUID = 1L;

	public static final String SELECTION_CHANGED = "RISCOS_SELECTION_CHANGED";

	public static final String VALUE_CHANGED = "VALUE_CHANGED";

	private static final String ICON_NAME_SAVE = "siprp/higiene/gestao/add.png";

	private static final String ICON_NAME_REVERT = "siprp/higiene/gestao/delete.png";
	
	private final LeafButton buttonAdicionar = LeafIconButton.createButton( ICON_NAME_SAVE );

	private final LeafButton buttonRemover = LeafIconButton.createButton( ICON_NAME_REVERT );

	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 HsRelatorioPosto posto = null;
	
	private HsRelatorioPostoMedida selectedMedida = null;

	public GerirMedidasRelatorioPanel()
	{
		startupComponents();
		setupLayout();
		placeComponents();
		startupListeners();
	}

	private void startupComponents()
	{
		buttonAdicionar.setEnabled( false );
		buttonRemover.setEnabled( false );
		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 RiscoRelatorioNode )
				{
					Object userObject = ((RiscoRelatorioNode) pValue).getUserObject();
					if( userObject instanceof HsRelatorioPostoRisco )
					{
						highlight = HigieneSegurancaLogic.isRelatorioRiscoPreenchido( (HsRelatorioPostoRisco) userObject );
						highColor = RelatorioHigieneSegurancaWindow.COLOR_RISCO_OK_SEL;
						lowColor = RelatorioHigieneSegurancaWindow.COLOR_RISCO_OK;
					}
				}
				else if( pValue instanceof MedidaRelatorioNode )
				{
					Object userObject = ((MedidaRelatorioNode) pValue).getUserObject();
					if( userObject instanceof HsRelatorioPostoMedida )
					{
						HsRelatorioPostoMedida medida = (HsRelatorioPostoMedida) userObject;
						if( medida.getIsPlanoActuacao() != null )
						{
							highlight = medida.getIsPlanoActuacao();
							highColor = RelatorioHigieneSegurancaWindow.COLOR_MEDIDA_OK_SEL;
							lowColor = RelatorioHigieneSegurancaWindow.COLOR_MEDIDA_OK;
						}
					}
				}
				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.MINIMUM, TableLayout.FILL
		}, new double[] {
			TableLayout.MINIMUM
		} );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		panel.setLayout( layout );
		panel.add( buttonAdicionar, new TableLayoutConstraints( 0, 0 ) );
		panel.add( buttonRemover, new TableLayoutConstraints( 1, 0 ) );

		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 )
			{
				Object object = null;
				TreePath path = tree.getSelectionPath();
				if( path != null )
				{
					Object selection = path.getLastPathComponent();
					if( selection instanceof RiscoRelatorioNode )
					{
						object = selection == null ? null : ((RiscoRelatorioNode) selection).getUserObject();

					}
					else if( selection instanceof MedidaRelatorioNode )
					{
						object = selection == null ? null : ((MedidaRelatorioNode) selection).getUserObject();
						selectedMedida = (HsRelatorioPostoMedida) object;
					}
				}
				setEnabled();
				firePropertyChange( SELECTION_CHANGED, null, object );
			}
		} );
		buttonAdicionar.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent e )
			{
				add();

			}

		} );
		buttonRemover.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent e )
			{
				rem();
			}

		} );
	}

	private HsRelatorioPostoRisco getSelectedRisco()
	{
		HsRelatorioPostoRisco result = null;
		TreePath path = tree.getSelectionPath();
		if( path != null )
		{
			Object selection = path.getLastPathComponent();
			if( selection instanceof RiscoRelatorioNode )
			{
				result = selection == null ? null : (HsRelatorioPostoRisco) ((RiscoRelatorioNode) selection).getUserObject();
			}
		}
		return result;
	}

	private HsRelatorioPostoMedida getSelectedMedida()
	{
		HsRelatorioPostoMedida result = null;
		TreePath path = tree.getSelectionPath();
		if( path != null )
		{
			Object selection = path.getLastPathComponent();
			if( selection instanceof MedidaRelatorioNode )
			{
				result = selection == null ? null : (HsRelatorioPostoMedida) ((MedidaRelatorioNode) selection).getUserObject();
			}
		}
		return result;
	}

	private void add()
	{
		try
		{
			HsRelatorioPostoRisco postoRisco = getSelectedRisco();
			if( posto != null )
			{
				HsRelatorioMedida medida = new HsRelatorioMedida();
				medida.setDescription( "" );
				medida.setRequesitosLegais( "" );
				medida.setToHsRelatorioRisco( postoRisco.getToHsRelatorioRisco() );
				HsRelatorioPostoMedida rel = new HsRelatorioPostoMedida();
				rel.setToHsRelatorioMedida( medida );
				rel.setToHsRelatorioPosto( posto );
				rel.save();
				selectedMedida = rel;
			}
			refresh();
			setEnabled();
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private boolean confirm( String message )
	{
		return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog( this, message, "Confirma"+ccedil+atilde+"o", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null );
	}
	
	private void rem()
	{
		try
		{
			TreePath path = tree.getSelectionPath();
			if( path != null )
			{
				Object selection = path.getLastPathComponent();
				if( selection instanceof MedidaRelatorioNode )
				{
					if( confirm("Tem a certeza que deseja remover o requisito?") )
					{
						HsRelatorioPostoMedida rel = selection == null ? null : (HsRelatorioPostoMedida) ((MedidaRelatorioNode) selection).getUserObject();
						rel.getToHsRelatorioMedida().setDeletedDate( new Date() );
						rel.delete();
					}
				}
				else if( selection instanceof RiscoRelatorioNode )
				{
					if( confirm("Tem a certeza que deseja remover o risco?")  )
					{
						HsRelatorioPostoRisco rel = (HsRelatorioPostoRisco) ((RiscoRelatorioNode)selection).getUserObject();
						for( HsRelatorioMedida medida : rel.getToHsRelatorioRisco().getHsRelatorioMedidaArray() )
						{
							while( true )
							{
								List<HsRelatorioPostoMedida> list = medida.getHsRelatorioPostoMedidaArray();
								HsRelatorioPostoMedida relMedida = list.isEmpty() ? null : list.get(0);
								if( relMedida != null )
								{
									relMedida.delete();
								}
								else
								{
									break;
								}
							}
							medida.setDeletedDate( new Date() );
						}
						rel.getToHsRelatorioRisco().setDeletedDate( new Date() );
					}
				}
			}
			refresh();
			setEnabled();
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private void setEnabled()
	{
		boolean add = false;
		boolean rem = false;
		if( posto != null )
		{
			if( posto.getToHsRelatorioArea() != null && posto.getToHsRelatorioArea().getToHsRelatorio() != null && posto.getToHsRelatorioArea().getToHsRelatorio().getIsSubmetido() == null )
			{
				add = true;
				rem = true;
			}
		}
		buttonAdicionar.setEnabled( add && getSelectedRisco() != null );
		buttonRemover.setEnabled( rem && ( getSelectedMedida() != null || getSelectedRisco() != null ) );
	}

	public void refresh()
	{
		root.removeAllChildren();
		if( posto != null )
		{
			for( HsRelatorioPostoRisco rel : posto.getHsRelatorioPostoRiscoArray() )
			{
				RiscoRelatorioNode node = new RiscoRelatorioNode( rel );
				HsRelatorioRisco risco = rel.getToHsRelatorioRisco();
				if( risco.getDeletedDate() == null )
				{
					for( HsRelatorioMedida medida : risco.getHsRelatorioMedidaArray() )
					{
						for( HsRelatorioPostoMedida medidaRel : medida.getHsRelatorioPostoMedidaArray() )
						{
							node.add( new MedidaRelatorioNode( medidaRel ) );
						}
					}
					root.add( node );
				}
			}
		}
		setEnabled();
		TreeTools.sort( root );
		TreeTools.refreshTree( tree, root, false );
		if( selectedMedida != null )
		{
			selectMedida();
		}
	}
	
	private void selectMedida( )
	{
		DefaultMutableTreeNode node = TreeTools.findNodeWithUserObject( selectedMedida, root );
		if( node != null )
		{
			TreePath path = TreeTools.getPathFor( node );
			if( path != null )
			{
				tree.setSelectionPath( path );
			}
		}
		else
		{
			selectedMedida = null;
		}
	}

	public void setPosto( HsRelatorioPosto posto )
	{
		this.posto = posto;
		this.selectedMedida = null;
		refresh();
		setEnabled();
	}

}
