package siprp.higiene.gestao.postos;

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

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
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.LeafTree;
import leaf.ui.LeafUIConstants;
import siprp.database.cayenne.objects.Empresas;
import siprp.database.cayenne.objects.HsArea;
import siprp.database.cayenne.objects.HsPosto;
import siprp.logic.node.AreaNode;
import siprp.logic.node.PostoNode;

import com.evolute.utils.ui.trees.TreeTools;

public class GerirAreasPanel extends JPanel implements LeafUIConstants
{

	private static final long serialVersionUID = 1L;

	private final JPanel panelButtons = new JPanel();

	private final JPanel panelTree = new JPanel();

	public final LeafButton buttonAreaCriar = LeafIconButton.createDefaultNewButton();

	public final LeafButton buttonAreaEditar = LeafIconButton.createDefaultEditButton();

	public final LeafButton buttonAreaRemover = LeafIconButton.createDefaultRemoveButton();

	public final LeafButton buttonPostoCriar = LeafIconButton.createDefaultNewButton();

	public final LeafButton buttonPostoEditar = LeafIconButton.createDefaultEditButton();

	public final LeafButton buttonPostoRemover = LeafIconButton.createDefaultRemoveButton();
	
	private final JCheckBox checkGenerico = new JCheckBox( "Gen"+eacute+"rico" );

	private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();

	private final DefaultTreeModel model = new DefaultTreeModel( root );

	public final LeafTree tree = new LeafTree( model );

	private final JScrollPane scroll = new JScrollPane( tree );

	private Empresas empresa = null;

	public GerirAreasPanel()
	{
		startupComponents();
		startupLayout();
		placeComponents();
		setupListeners();
	}

	private void startupComponents()
	{
		panelButtons.setPreferredSize( new Dimension( 150, 0 ) );
		panelTree.setPreferredSize( new Dimension( 300, 0 ) );
		tree.setRootVisible( false );
		tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
	}

	private void startupLayout()
	{
		TableLayout layout = new TableLayout( new double[] {
			TableLayout.FILL
		}, new double[] {
				TableLayout.MINIMUM, TableLayout.FILL
		} );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		setLayout( layout );

		layout = new TableLayout( new double[] {
				TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL
		}, new double[] {
			TableLayout.MINIMUM
		} );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		panelButtons.setLayout( layout );

		layout = new TableLayout( new double[] {
			TableLayout.FILL
		}, new double[] {
			TableLayout.FILL
		} );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		panelTree.setLayout( layout );
	}

	private void placeComponents()
	{
		JPanel panel = new JPanel();
		panel.setBorder( BorderFactory.createTitledBorder( "Area" ) );
		panel.setLayout( new GridLayout( 1, 3 ) );
		panel.add( buttonAreaCriar );
		panel.add( buttonAreaEditar );
		panel.add( buttonAreaRemover );
		panelButtons.add( panel, new TableLayoutConstraints( 0, 0 ) );

		panel = new JPanel();
		panel.setBorder( BorderFactory.createTitledBorder( "Posto" ) );
		panel.setLayout( new GridLayout( 1, 3 ) );
		panel.add( buttonPostoCriar );
		panel.add( buttonPostoEditar );
		panel.add( buttonPostoRemover );
		panelButtons.add( panel, new TableLayoutConstraints( 1, 0 ) );

		panelButtons.add( checkGenerico, new TableLayoutConstraints(2,0));
		
		panelTree.add( scroll, new TableLayoutConstraints( 0, 0 ) );

		add( panelButtons, new TableLayoutConstraints( 0, 0 ) );
		add( panelTree, new TableLayoutConstraints( 0, 1 ) );
	}

	private void setupListeners()
	{
		tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener()
		{
			@Override
			public void valueChanged( TreeSelectionEvent e )
			{
				setSelected();
			}
		} );
		buttonAreaCriar.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				criarArea();
			}
		} );
		buttonAreaEditar.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				editarArea();
			}
		} );
		buttonAreaRemover.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				removerArea();
			}
		} );
		buttonPostoCriar.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				criarPosto();
			}
		} );
		buttonPostoEditar.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				editarPosto();
			}
		} );
		buttonPostoRemover.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				removerPosto();
			}
		} );
		checkGenerico.addItemListener( new ItemListener()
		{
			@Override
			public void itemStateChanged( ItemEvent e )
			{
				HsPosto posto = getSelectedPosto();
				if( posto != null )
				{
					posto.setIsPrincipal( checkGenerico.isSelected() );
					try
					{
						posto.save();
					} catch( Exception e1 )
					{
						e1.printStackTrace();
					}
				}
			}
		} );
	}

	private void criarArea()
	{
		try
		{
			if( empresa != null )
			{
				String areaString = JOptionPane.showInputDialog( this, "Descri" + ccedil + atilde + "o", "Criar nova " + aacute + "rea", JOptionPane.QUESTION_MESSAGE );
				if( areaString != null )
				{
					HsArea area = new HsArea();
					area.setToEmpresas( empresa );
					area.setDescription( areaString );
					area.save();
					refresh();
				}
			}
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private void editarArea()
	{
		try
		{
			HsArea area = getSelectedArea();
			if( area != null )
			{
				String areaString = JOptionPane.showInputDialog( this, "Descri" + ccedil + atilde + "o",  area.getDescription() );
				if( areaString != null )
				{
					area.setDescription( areaString );
					area.save();
					refresh();
				}
			}
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private void removerArea()
	{
		try
		{
			HsArea area = getSelectedArea();
			if( area != null && LeafDialog.confirmDelete( false, false, aacute + "rea") )
			{
				area.delete();
				refresh();
			}
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private void criarPosto()
	{
		try
		{
			HsArea area = getSelectedArea();
			if( area != null )
			{
				String postoString = JOptionPane.showInputDialog( this, "Descri" + ccedil + atilde + "o", "Criar novo posto de trabalho", JOptionPane.QUESTION_MESSAGE );
				if( postoString != null )
				{
					HsPosto posto = new HsPosto();
					posto.setToHsArea( area );
					posto.setDescription( postoString );
					posto.save();
					refresh();
				}
				refresh();
			}
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}
	
	private void editarPosto()
	{
		try
		{
			HsPosto posto = getSelectedPosto();
			if( posto != null )
			{
				String postoString = JOptionPane.showInputDialog( this, "Descri" + ccedil + atilde + "o", posto.getDescription() );
				if( postoString != null )
				{
					posto.setDescription( postoString );
					posto.save();
					refresh();
				}
			}
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private void removerPosto()
	{
		try
		{
			HsPosto posto = getSelectedPosto();
			if( posto != null && LeafDialog.confirmDelete( true, false, "posto" ) )
			{
				posto.setDeletedDate( new Date() );
				posto.save();
				refresh();
			}
		} catch( Exception e )
		{
			LeafDialog.error( e );
		}
	}

	private HsArea getSelectedArea()
	{
		HsArea result = null;
		TreePath path = tree.getSelectionPath();
		if( path != null )
		{
			Object leaf = path.getLastPathComponent();
			if( leaf != null )
			{
				if( leaf instanceof AreaNode )
				{
					result = (HsArea) ((AreaNode) leaf).getUserObject();
				}
				else if( leaf instanceof PostoNode )
				{
					result = (HsArea) ((HsPosto) ((PostoNode) leaf).getUserObject()).getToHsArea();
				}
			}
		}
		return result;
	}

	private HsPosto getSelectedPosto()
	{
		HsPosto result = null;
		TreePath path = tree.getSelectionPath();
		if( path != null )
		{
			Object leaf = path.getLastPathComponent();
			if( leaf != null && (leaf instanceof PostoNode) )
			{
				result = (HsPosto) ((PostoNode) leaf).getUserObject();
			}
		}
		return result;
	}

	private void setSelected()
	{
		TreePath path = tree.getSelectionPath();
		TreeNode node = path == null ? null : (TreeNode) path.getLastPathComponent();
		boolean areaSelected = node != null && (node instanceof AreaNode);
		boolean postoSelected = node != null && (node instanceof PostoNode);
		buttonPostoCriar.setEnabled( areaSelected || postoSelected );
		buttonPostoEditar.setEnabled( postoSelected );
		buttonPostoRemover.setEnabled( postoSelected );
		buttonAreaRemover.setEnabled( areaSelected );
		buttonAreaCriar.setEnabled( empresa != null );
		buttonAreaEditar.setEnabled( areaSelected );
		checkGenerico.setEnabled( postoSelected );
		boolean generico = false;
		if( postoSelected &&  node != null)
		{
			HsPosto posto = (HsPosto) ((PostoNode) node).getUserObject();
			generico = posto != null && (posto.getIsPrincipal() ==null ? false : posto.getIsPrincipal() );
		}
		checkGenerico.setSelected(generico);
	}

	public void refresh()
	{
		root.removeAllChildren();
		if( empresa != null )
		{
			List<HsArea> areas = empresa.getHsAreaArray();
			Collections.sort( areas );
			for( HsArea area : areas )
			{
				addArea( area );
			}
		}
		setSelected();
		TreeTools.refreshTree( tree, root, false );
	}

	private void addArea( HsArea area )
	{
		if( area != null )
		{
			AreaNode areaNode = new AreaNode( area );
			for( HsPosto posto : area.getHsPostoArray() )
			{
				addPosto( posto, areaNode );
			}
			root.add( areaNode );
		}
	}

	private void addPosto( HsPosto posto, AreaNode areaNode )
	{
		if( posto != null && posto.getDeletedDate() == null )
		{
			PostoNode postoNode = new PostoNode( posto );
			areaNode.add( postoNode );
		}
	}

	public void setEmpresa( Empresas empresa )
	{
		this.empresa = empresa;
		refresh();
	}

}
