package siprp.higiene.gestao.equipamentos;

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 javax.swing.JLabel;
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.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import leaf.data.Validator;
import leaf.ui.LeafButton;
import leaf.ui.LeafTextAreaEditor;
import leaf.ui.TreeTools;
import siprp.database.cayenne.objects.HsEquipamento;
import siprp.logic.HigieneSegurancaLogic;

public class GerirEquipamentosPanel extends JPanel
{
	
	private static final long serialVersionUID = 1L;

	private final JPanel panelButtons = new JPanel();

	private final JPanel panelTree = new JPanel();
	
	private final JPanel panelData = new JPanel();
	
	private final LeafTextAreaEditor textMarca = new LeafTextAreaEditor( new Validator<String>()
	{
		@Override
		public boolean isValid( String object )
		{
			return isMarcaValida( object );
		}
	}, true );
	
	private final LeafTextAreaEditor textModelo = new LeafTextAreaEditor( new Validator<String>()
	{
		@Override
		public boolean isValid( String object )
		{
			return isModeloValido( object );
		}
	}, true );

	private final LeafButton buttonCriar = new LeafButton( "Criar" );

	private final LeafButton buttonEditar = new LeafButton( "Editar" );

	private final LeafButton buttonRemover = new LeafButton( "Remover" );

	private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();

	private final DefaultTreeModel model = new DefaultTreeModel( root );

	private final JTree tree = new JTree( model );

	private final JScrollPane scroll = new JScrollPane( tree );

	public GerirEquipamentosPanel()
	{
		startupComponents();
		startupLayout();
		placeComponents();
		setupListeners();
		refresh();
	}

	private void setupListeners()
	{
		buttonCriar.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				criar();
			}
		} );
		buttonEditar.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				editar();
			}
		} );
		buttonRemover.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				remover();
			}
		} );
		tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener()
		{
			@Override
			public void valueChanged( TreeSelectionEvent e )
			{
				setEnabled();
			}
		} );
	}

	private void criar()
	{
	}

	private void editar()
	{
	}

	private void remover()
	{
	}

	private HsEquipamento getSelected()
	{
		Object object = getSelectedObject();
		return object == null ? null : ((object instanceof HsEquipamento) ? (HsEquipamento) object : null);
	}
	
	private Object getSelectedObject()
	{
		DefaultMutableTreeNode node = getSelectedNode();
		return node == null ? null : node.getUserObject();
	}

	private DefaultMutableTreeNode getSelectedNode()
	{
		TreePath path = tree.getSelectionPath();
		return path == null ? null : (DefaultMutableTreeNode) path.getLastPathComponent();
	}

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

	private void setEnabled()
	{
		HsEquipamento equipamento = getSelected();
		buttonEditar.setEnabled( equipamento != null );
		buttonRemover.setEnabled( equipamento != null );
	}

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

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

	private void placeComponents()
	{
		panelButtons.add( buttonCriar, new TableLayoutConstraints( 0, 0 ) );
		panelButtons.add( buttonEditar, new TableLayoutConstraints( 0, 1 ) );
		panelButtons.add( buttonRemover, new TableLayoutConstraints( 0, 2 ) );

		panelTree.add( scroll, new TableLayoutConstraints( 0, 0 ) );

		panelData.add( new JLabel("Marca"), new TableLayoutConstraints( 0, 0 ) );
		panelData.add( textMarca, new TableLayoutConstraints( 1, 0 ) );
		panelData.add( new JLabel("Modelo"), new TableLayoutConstraints( 0, 1 ) );
		panelData.add( textModelo, new TableLayoutConstraints( 1, 1 ) );
		
		add( panelButtons, new TableLayoutConstraints( 0, 0 ) );
		add( panelTree, new TableLayoutConstraints( 1, 0 ) );
		add( panelData, new TableLayoutConstraints( 2, 0 ) );
	}

	private void refresh()
	{
		root.removeAllChildren();
		TreeTools.merge( root, HigieneSegurancaLogic.getEquipamentosTree() );
		TreeTools.refreshTree( tree, root );
		setEnabled();
	}
	
	private boolean isMarcaValida( String marca )
	{
		return true;  
	}
	
	private boolean isModeloValido( String modelo )
	{
		return true;
	}

}
