package view.contact;

import controller.contact.Add_Contact_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.db.corrida.model.Contact;
import org.sig.fw.layout.component.SButton;
import org.sig.fw.layout.component.SComboBox;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.component.STextField;
import org.sig.fw.layout.contener.SDialog;
import org.sig.fw.layout.pane.SOptionPane;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Add_Contact_View extends SDialog
{
	private static final long serialVersionUID = -3841037651708687386L;
	
	private Add_Contact_Controller ctrl_add_contact;
	
	private SComboBox jcb_civility;
	private STextField jtf_firstname;
	private STextField jtf_name;
	private STextField jtf_phone;
	private STextField jtf_mobile;
	private STextField jtf_email;
	
	public Add_Contact_View(Add_Contact_Controller ctrl_add_contact) 
	{
		super("Ajouter un Contact", true);
		setBounds(100, 100, 380, 270);
		this.ctrl_add_contact = ctrl_add_contact;
		getContentPane().setLayout(new MigLayout("gap 2px 2px, insets 4px 4px", "[][grow]", "[][][][][][][grow]"));
		
		SLabel lblNewLabel = new SLabel("Civilité", SLabel.TYPE_COMMUN);
		getContentPane().add(lblNewLabel, "cell 0 0,alignx left");
		
		jcb_civility = new SComboBox();
		getContentPane().add(jcb_civility, "cell 1 0,growx");
		
		SLabel label = new SLabel("Prénom", SLabel.TYPE_COMMUN);
		getContentPane().add(label, "cell 0 1,alignx left");
		
		jtf_firstname = new STextField("");
		getContentPane().add(jtf_firstname, "cell 1 1,growx");
		jtf_firstname.setColumns(10);
		
		SLabel label_1 = new SLabel("Nom", SLabel.TYPE_COMMUN);
		getContentPane().add(label_1, "cell 0 2,alignx left");
		
		jtf_name = new STextField("");
		getContentPane().add(jtf_name, "cell 1 2,growx");
		
		SLabel label_2 = new SLabel("N° Téléphone Fixe", SLabel.TYPE_COMMUN);
		getContentPane().add(label_2, "cell 0 3,alignx left");
		
		jtf_phone = new STextField("");
		getContentPane().add(jtf_phone, "cell 1 3,growx");
		
		SLabel label_3 = new SLabel("N° Téléphone Mobile", SLabel.TYPE_COMMUN);
		getContentPane().add(label_3, "cell 0 4,alignx left");
		
		jtf_mobile = new STextField("");
		getContentPane().add(jtf_mobile, "cell 1 4,growx");
		
		SLabel label_4 = new SLabel("Email", SLabel.TYPE_COMMUN);
		getContentPane().add(label_4, "cell 0 5,alignx left");
		
		jtf_email = new STextField("");
		getContentPane().add(jtf_email, "cell 1 5,growx");
		
		SButton btnNewButton = new SButton("Valider", "Validation du formulaire");
		btnNewButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				btnNewButton.setEnabled(false);
				validation();
				btnNewButton.setEnabled(true);
			}
		});
		getContentPane().add(btnNewButton, "cell 0 6 2 1,alignx center");
		
		SwingUtilities.invokeLater(new Runnable() 
		{
			@Override
			public void run()
			{
				insertData();
			}
		});
		computePosition();
	}
	
	public void insertData()
	{
		Contact c = ctrl_add_contact.getContact();
		
		this.setTitle(c.getDbId() == -1 ? "Ajouter un Contact" : "Modifier un Contact");
		
		jcb_civility.removeAllItems();
		jcb_civility.addItem("M.");
		jcb_civility.addItem("Mme.");
		jcb_civility.setSelectedItem(c.getCivility());
		
		jtf_name.setText(c.getName());
		jtf_firstname.setText(c.getFirstname());
		jtf_phone.setText(c.getPhone());
		jtf_mobile.setText(c.getMobile());
		jtf_email.setText(c.getEmail());		
	}
	
	public String verification()
	{
		String error = "";
		if(jtf_name.getText().isEmpty())
			error += "Le nom ne peut être vide !\n";
		if(jtf_firstname.getText().isEmpty())
			error += "Le prénom ne peut être vide !\n";
		if(jtf_phone.getText().isEmpty() && jtf_mobile.getText().isEmpty() && jtf_email.getText().isEmpty())
			error += "Au moins 1 moyen de contact doit être renseigné !";
		return error;
	}
	
	public void validation()
	{
		String error = verification();
		
		if(error.isEmpty())
		{
			Contact c = ctrl_add_contact.getContact();
			
			c.setCivility((String) jcb_civility.getSelectedItem());
			c.setName(jtf_name.getText());
			c.setFirstname(jtf_firstname.getText());
			c.setPhone(jtf_phone.getText());
			c.setMobile(jtf_mobile.getText());
			c.setEmail(jtf_email.getText());
			
			ctrl_add_contact.save();
		}
		else
			SOptionPane.showErrorMessage("Validation", error);
	}

}
