package view.definition;

import controller.definition.Definition_Frame_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.database.model.DBModel;
import org.sig.fw.db.corrida.dao.State_DBAO;
import org.sig.fw.db.corrida.model.State;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.component.SSpinner;
import org.sig.fw.layout.component.STextField;
import org.sig.fw.layout.pane.SOptionPane;
import org.sig.fw.utility.color.SColor;
import renderer.table.Search_State_Renderer;
import table.search.Search_State_Table;
import view.Definition_Frame_View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class State_View extends Definition_Frame_View {
	private static final long serialVersionUID = -4640562421963912343L;
	
	private STextField jtf_short_value;
	private STextField jtf_long_value;
	private SSpinner js_critique_index;
	private SLabel jl_example;
	private STextField jtf_background_color;
	private STextField jtf_foregound_color;
	

	public State_View(Definition_Frame_Controller ctrl_definition) {
		super(ctrl_definition, "Liste des états");
		setBounds(100, 100, 560, 389);
		
		jp_form.setLayout(new MigLayout("", "[][grow][]", "[][][][][][][][][][grow][]"));
		
		jl_title = new SLabel("Ajouter un <ACTION>", SLabel.TYPE_TITLE_BOLD);
		jp_form.add(jl_title, "cell 0 0 3 1,alignx center");
		
		SLabel lblNewLabel = new SLabel("Nom Court", SLabel.TYPE_COMMUN);
		jp_form.add(lblNewLabel, "cell 0 2,alignx left");
		
		jtf_short_value = new STextField("");
		jp_form.add(jtf_short_value, "cell 1 2 2 1,growx");
		
		SLabel lblNewLabel_1 = new SLabel("Nom Long", SLabel.TYPE_COMMUN);
		jp_form.add(lblNewLabel_1, "cell 0 3,alignx left");
		
		jtf_long_value = new STextField("");
		jp_form.add(jtf_long_value, "cell 1 3 2 1,growx");
		
		SLabel lblNewLabel_2 = new SLabel("Couleur de fond", SLabel.TYPE_COMMUN);
		jp_form.add(lblNewLabel_2, "cell 0 4,alignx left");
		
		jtf_background_color = new STextField("");
		jtf_background_color.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				colorExample();
			}
		});
		jp_form.add(jtf_background_color, "cell 1 4,growx");
		
		SLabel btnNewButton_1 = new SLabel("", SLabel.TYPE_COMMUN);
		btnNewButton_1.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Color c = JColorChooser.showDialog(ctrl_definition.getApplicationController().getView(), "Choix de la couleur", new SColor().isHex2Rgb(jtf_background_color.getText()) ? new SColor().hex2Rgb(jtf_background_color.getText()) : null);
				if (c != null)
				{
					jtf_background_color.setText(new SColor().rgb2Hex(c));
					colorExample();
				}
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(State_View.class.getResource("/org/apache/pdfbox/debugger/boolean.png")));
		jp_form.add(btnNewButton_1, "cell 2 4");
		
		SLabel lblNewLabel_3 = new SLabel("Couleur d'\u00E9criture", SLabel.TYPE_COMMUN);
		jp_form.add(lblNewLabel_3, "cell 0 5,alignx left");
		
		jtf_foregound_color = new STextField("");
		jtf_foregound_color.addKeyListener(new KeyAdapter() 
		{
			@Override
			public void keyReleased(KeyEvent e)
			{
				colorExample();
			}
		});
		jp_form.add(jtf_foregound_color, "cell 1 5,growx");
		
		SLabel button = new SLabel("", SLabel.TYPE_COMMUN);
		button.setIcon(new ImageIcon(State_View.class.getResource("/org/apache/pdfbox/debugger/boolean.png")));
		button.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				Color c = JColorChooser.showDialog(ctrl_definition.getApplicationController().getView(), "Choix de la couleur", new SColor().isHex2Rgb(jtf_foregound_color.getText()) ? new SColor().hex2Rgb(jtf_foregound_color.getText()) : null);
				if (c != null)
				{
					jtf_foregound_color.setText(new SColor().rgb2Hex(c));
					colorExample();
				}
			}
		});
		jp_form.add(button, "cell 2 5");
		
		SLabel lblNewLabel_4 = new SLabel("Indice", SLabel.TYPE_COMMUN);
		jp_form.add(lblNewLabel_4, "cell 0 6");
		
		js_critique_index = new SSpinner(SSpinner.TYPE_INT, 0, 0, 100, 1);
		jp_form.add(js_critique_index, "cell 1 6 2 1,growx");
		
		jl_example = new SLabel("   Exemple   ", SLabel.TYPE_COMMUN_BOLD);
		jl_example.setOpaque(true);
		jp_form.add(jl_example, "cell 0 8 3 1,alignx center");
		
		jp_form.add(jb_validate, "cell 0 10 3 1,alignx center");
		
		configure();
		updateTable();
	}

	public void colorExample()
	{
		if(new SColor().isHex2Rgb(jtf_foregound_color.getText()))
			jl_example.setForeground(new SColor().hex2Rgb(jtf_foregound_color.getText()));
		else
			jl_example.setForeground(null);
		
		if(new SColor().isHex2Rgb(jtf_background_color.getText()))
			jl_example.setBackground(new SColor().hex2Rgb(jtf_background_color.getText()));
		else
			jl_example.setBackground(null);
	}
	
	@Override
	public void updateTable()
	{
		jt_table.setModel(new Search_State_Table(new ArrayList<DBModel>(new State_DBAO("s").getAll(true))));
		jt_table.configureColumn(0, 0, 0, 0);
		jt_table.setDefaultRenderer(String.class, new Search_State_Renderer());
	}
	
	@Override
	public void configure()
	{
		State state = (State) model;
		if(state == null)
		{
			jl_title.setText("Ajouter un état");
			
			jtf_short_value.setText("");
			jtf_long_value.setText("");
			jtf_background_color.setText("");
			jtf_foregound_color.setText("");
			js_critique_index.setValue(0);
		}
		else
		{
			jl_title.setText("Modifier un état");
			
			jtf_short_value.setText(state.getShortValue());
			jtf_long_value.setText(state.getLongValue());
			jtf_background_color.setText(new SColor().rgb2Hex(state.getBackground()));
			jtf_foregound_color.setText(new SColor().rgb2Hex(state.getForeground()));
			js_critique_index.setValue(state.getCritiqueIndex());
		}
		colorExample();
	}
	
	@Override
	public String verification()
	{
		String error = "";
		
		if(jtf_short_value.getText().isEmpty())
			error += "'Nom Court' ne peut être vide !\n";
		if(jtf_long_value.getText().isEmpty())
			error += "'Nom Long' ne peut être vide !\n";
		if(!new SColor().isHex2Rgb(jtf_background_color.getText()))
			error += "'Couleur de fond' invalide. Vous pouvez vous aider du bouton '?' pour choisir un code couleur compatible !\n";
		if(!new SColor().isHex2Rgb(jtf_foregound_color.getText()))
			error += "'Couleur d'écriture' invalide. Vous pouvez vous aider du bouton '?' pour choisir un code couleur compatible !\n";
		
		return error;
	}
	
	@Override
	public void validation()
	{
		if(verification().isEmpty())
		{
			State state = (State) model;
			
			if(state == null)
			{
				state = new State(-1);
			}
			
			state.setShortValue(jtf_short_value.getText());
			state.setLongValue(jtf_long_value.getText());
			state.setBackground(new SColor().hex2Rgb(jtf_background_color.getText()));
			state.setForeground(new SColor().hex2Rgb(jtf_foregound_color.getText()));
			state.setCritiqueIndex((int) js_critique_index.getValue());
			
			ctrl_definition.save(state);
			state = null;
			updateTable();
			configure();
		}
		else
		{
			SOptionPane.showErrorMessage("Erreur",  verification());
		}
	}
}
