package siprp.higiene.gestao.riscos;

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

import java.awt.Insets;
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.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import leaf.ui.LeafButton;
import leaf.ui.LeafError;
import siprp.database.cayenne.objects.HsMedida;

import com.evolute.utils.images.ImageException;
import com.evolute.utils.images.ImageIconLoader;
import com.evolute.utils.ui.text.CopyPasteHandler;

public class GerirMedidaPanel extends JPanel
{

	public static final String MEDIDA_CHANGED = "MEDIDA_CHANGED";

	private static final long serialVersionUID = 1L;

	private static final String ICON_NAME_SAVE = "leaf/ui/icons/save.png";

	private static final String ICON_NAME_REVERT = "leaf/ui/icons/revert.png";

	private LeafButton buttonSaveRequesito;

	private LeafButton buttonRevertRequesito;

	private final JTextArea fieldTextMedida = new JTextArea();

	private final JScrollPane scrollMedida = new JScrollPane( fieldTextMedida );

	private final JTextArea fieldTextRequisitosLegais = new JTextArea();

	private final JScrollPane scrollRequesitos = new JScrollPane( fieldTextRequisitosLegais );

	private HsMedida medida = null;

	public GerirMedidaPanel()
	{
		try
		{
			buttonSaveRequesito = new LeafButton( ImageIconLoader.loadImageIcon( ICON_NAME_SAVE ) );
			buttonRevertRequesito = new LeafButton( ImageIconLoader.loadImageIcon( ICON_NAME_REVERT ) );
		} catch( ImageException e )
		{
			e.printStackTrace();
		}
		startupComponents();
		startupLayout();
		placeComponents();
		setupListeners();
	}

	private void startupComponents()
	{
		fieldTextMedida.setWrapStyleWord( true );
		fieldTextMedida.setLineWrap( true );
		new CopyPasteHandler(fieldTextMedida);
		fieldTextRequisitosLegais.setWrapStyleWord( true );
		fieldTextRequisitosLegais.setLineWrap( true );
		new CopyPasteHandler(fieldTextRequisitosLegais);
		scrollMedida.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		scrollMedida.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		scrollRequesitos.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		scrollRequesitos.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		buttonSaveRequesito.setMargin( new Insets( 0, 0, 0, 0 ) );
		buttonRevertRequesito.setMargin( new Insets( 0, 0, 0, 0 ) );
	}

	private void setupListeners()
	{
		fieldTextRequisitosLegais.addCaretListener( new CaretListener()
		{
			@Override
			public void caretUpdate( CaretEvent e )
			{
				setEnabled();
			}
		} );
		fieldTextMedida.addCaretListener( new CaretListener()
		{
			@Override
			public void caretUpdate( CaretEvent e )
			{
				setEnabled();
			}
		} );
		buttonSaveRequesito.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				saveRequesito();
			}
		} );
		buttonRevertRequesito.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				revertRequesito();
			}
		} );
	}

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

	private void placeComponents()
	{
		add( new JLabel( "Requisitos legais" ), new TableLayoutConstraints( 0, 0 ) );
		add( buttonSaveRequesito, new TableLayoutConstraints( 1, 0 ) );
		add( buttonRevertRequesito, new TableLayoutConstraints( 2, 0 ) );
		add( scrollRequesitos, new TableLayoutConstraints( 0, 1, 2, 1 ) );
		add( new JLabel( "Medida" ), new TableLayoutConstraints( 0, 2 ) );
		add( scrollMedida, new TableLayoutConstraints( 0, 3, 2, 3 ) );
	}

	private void setEnabled()
	{
		boolean medidaChanged = false;
		boolean requesitoChanged = false;
		if( medida != null )
		{
			medidaChanged = !fieldTextMedida.getText().equals( medida.getDescription() );
			requesitoChanged = !fieldTextRequisitosLegais.getText().equals( medida.getRequesitosLegais() );
		}
		buttonSaveRequesito.setEnabled( requesitoChanged || medidaChanged );
		buttonRevertRequesito.setEnabled( requesitoChanged || medidaChanged );
	}

	private void saveRequesito()
	{
		try
		{
			if( medida != null )
			{
				medida.setRequesitosLegais( fieldTextRequisitosLegais.getText() );
				medida.setDescription( fieldTextMedida.getText() );
				medida.save();
				refresh();
			}
			setEnabled();
		} catch( Exception e )
		{
			LeafError.error( e );
		}
	}

	private void revertRequesito()
	{
		if( medida != null )
		{
			fieldTextRequisitosLegais.setText( medida.getRequesitosLegais() );
			fieldTextMedida.setText( medida.getDescription() );
		}
		setEnabled();
	}

	private void refresh()
	{
		firePropertyChange( MEDIDA_CHANGED, null, medida );
	}

	public void setMedida( HsMedida medida )
	{
		this.medida = medida;
		fieldTextMedida.setText( medida == null ? null : medida.getDescription() );
		fieldTextRequisitosLegais.setText( medida == null ? null : medida.getRequesitosLegais() );
		setEnabled();
	}

	public HsMedida getMedida()
	{
		return this.medida;
	}

}
