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.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
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.HsRelatorioPostoMedida;

import com.evolute.utils.images.ImageException;
import com.evolute.utils.images.ImageIconLoader;

public class GerirMedidaRelatorioPanel extends JPanel
{

	public static final String MEDIDA_CHANGED = "MEDIDA_CHANGED";

	private static final long serialVersionUID = 1L;

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

	private static final String ICON_NAME_REVERT = "siprp/higiene/gestao/riscos/revert.png";

	private LeafButton buttonSaveMedida;

	private LeafButton buttonRevertMedida;

	private LeafButton buttonSaveRequesito;

	private LeafButton buttonRevertRequesito;

	private final JCheckBox checkPlanoActuacao = new JCheckBox( "Plano de actua" + ccedil + atilde + "o" );

	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 HsRelatorioPostoMedida relacao = null;

	public GerirMedidaRelatorioPanel()
	{
		try
		{
			buttonSaveMedida = new LeafButton( ImageIconLoader.loadImageIcon( ICON_NAME_SAVE ) );
			buttonRevertMedida = new LeafButton( ImageIconLoader.loadImageIcon( ICON_NAME_REVERT ) );
			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 );
		fieldTextRequisitosLegais.setWrapStyleWord( true );
		fieldTextRequisitosLegais.setLineWrap( true );
		scrollMedida.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		scrollMedida.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		scrollRequesitos.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		scrollRequesitos.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
		buttonSaveMedida.setMargin( new Insets( 0, 0, 0, 0 ) );
		buttonSaveRequesito.setMargin( new Insets( 0, 0, 0, 0 ) );
		buttonRevertMedida.setMargin( new Insets( 0, 0, 0, 0 ) );
		buttonRevertRequesito.setMargin( new Insets( 0, 0, 0, 0 ) );
	}

	private void setupListeners()
	{
		checkPlanoActuacao.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				savePlano();
			}
		} );
		fieldTextRequisitosLegais.addCaretListener( new CaretListener()
		{
			@Override
			public void caretUpdate( CaretEvent e )
			{
				setEnabled();
			}
		} );
		fieldTextMedida.addCaretListener( new CaretListener()
		{
			@Override
			public void caretUpdate( CaretEvent e )
			{
				setEnabled();
			}
		} );
		buttonSaveMedida.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				saveMedida();
			}
		} );
		buttonSaveRequesito.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				saveRequesito();
			}
		} );
		buttonRevertMedida.addActionListener( new ActionListener()
		{
			@Override
			public void actionPerformed( ActionEvent e )
			{
				revertMedida();
			}
		} );
		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.MINIMUM, TableLayout.FILL, 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
		}, new double[] {
			TableLayout.MINIMUM
		} );
		layout.setHGap( 5 );
		layout.setVGap( 5 );
		panel.setLayout( layout );
		panel.add( checkPlanoActuacao, new TableLayoutConstraints( 0, 0 ) );
		add( panel, new TableLayoutConstraints( 0, 0, 2, 0 ) );
		add( new JLabel( "Requisitos legais" ), new TableLayoutConstraints( 0, 1 ) );
		add( buttonSaveRequesito, new TableLayoutConstraints( 1, 1 ) );
		add( buttonRevertRequesito, new TableLayoutConstraints( 2, 1 ) );
		add( scrollRequesitos, new TableLayoutConstraints( 0, 2, 2, 2 ) );
		add( new JLabel( "Medida" ), new TableLayoutConstraints( 0, 3 ) );
		add( buttonSaveMedida, new TableLayoutConstraints( 1, 3 ) );
		add( buttonRevertMedida, new TableLayoutConstraints( 2, 3 ) );
		add( scrollMedida, new TableLayoutConstraints( 0, 4, 2, 4 ) );
	}

	private void setEnabled()
	{
		if( isEnabled() )
		{
			boolean medidaChanged = false;
			boolean requesitoChanged = false;
			if( relacao != null )
			{
				checkPlanoActuacao.setEnabled( true );
				medidaChanged = !fieldTextMedida.getText().equals( relacao.getToHsRelatorioMedida().getDescription() );
				requesitoChanged = !fieldTextRequisitosLegais.getText().equals( relacao.getToHsRelatorioMedida().getRequesitosLegais() );
			}
			fieldTextMedida.setEnabled( relacao != null );
			fieldTextRequisitosLegais.setEnabled( relacao != null );
			buttonSaveMedida.setEnabled( medidaChanged );
			buttonSaveRequesito.setEnabled( requesitoChanged );
			buttonRevertMedida.setEnabled( medidaChanged );
			buttonRevertRequesito.setEnabled( requesitoChanged );
		}
		else
		{
			buttonSaveMedida.setEnabled( false );
			buttonSaveRequesito.setEnabled( false );
			buttonRevertMedida.setEnabled( false );
			buttonRevertRequesito.setEnabled( false );
			fieldTextRequisitosLegais.setEnabled( false );
			fieldTextMedida.setEnabled( false );
			checkPlanoActuacao.setEnabled( false );
		}

	}

	private void savePlano()
	{
		try
		{
			if( relacao != null )
			{
				relacao.setIsPlanoActuacao( checkPlanoActuacao.isSelected() );
				relacao.save();
				firePropertyChange( MEDIDA_CHANGED, null, relacao );
			}
		} catch( Exception e )
		{
			LeafError.error( e );
		}
	}

	private void saveMedida()
	{
		try
		{
			if( relacao != null )
			{
				relacao.getToHsRelatorioMedida().setDescription( fieldTextMedida.getText() );
				relacao.save();
				refresh();
			}
			setEnabled();
		} catch( Exception e )
		{
			LeafError.error( e );
		}
	}

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

	private void revertMedida()
	{
		if( relacao != null )
		{
			fieldTextMedida.setText( relacao.getToHsRelatorioMedida().getDescription() );
		}
		setEnabled();
	}

	private void revertRequesito()
	{
		if( relacao != null )
		{
			fieldTextRequisitosLegais.setText( relacao.getToHsRelatorioMedida().getRequesitosLegais() );
		}
		setEnabled();
	}

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

	public void setMedida( HsRelatorioPostoMedida medida )
	{
		this.relacao = medida;
		fieldTextMedida.setText( medida == null ? null : medida.getToHsRelatorioMedida().getDescription() );
		fieldTextRequisitosLegais.setText( medida == null ? null : medida.getToHsRelatorioMedida().getRequesitosLegais() );
		checkPlanoActuacao.setSelected( medida == null ? false : (medida.getIsPlanoActuacao() == null ? false : medida.getIsPlanoActuacao()) );
		setEnabled();
	}

}
