package view.contact;

import controller.client.Client_Controller;
import controller.contact.Add_Contact_Controller;
import net.miginfocom.swing.MigLayout;
import org.sig.fw.db.corrida.dao.Contact_DBAO;
import org.sig.fw.db.corrida.model.Contact;
import org.sig.fw.layout.component.SButton;
import org.sig.fw.layout.component.SLabel;
import org.sig.fw.layout.pane.SOptionPane;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Contact_View extends JPanel
{
	private static final long serialVersionUID = -6272469469710473924L;
	
	private SLabel jl_name;
	private SLabel jl_fixe_number;
	private SLabel jl_mail;
	private SLabel jl_mobile_number;

	public Contact_View(Client_Controller ctrl_client, Contact contact) 
	{
		super();
		setBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(0, 0, 0)));
		setLayout(new MigLayout("gap 5px 1px, insets 4px 4px", "[][grow][][100px:100px:100px][]", "[grow][grow]"));
		
		jl_name = new SLabel(contact.getCompleteName(), SLabel.TYPE_COMMUN);
		add(jl_name, "cell 0 0 2 1");
		
		SLabel lblNewLabel_1 = new SLabel("", SLabel.TYPE_COMMUN);
		lblNewLabel_1.setIcon(new ImageIcon(Contact_View.class.getResource("/icons/fixe.png")));
		add(lblNewLabel_1, "cell 2 0");
		
		jl_fixe_number = new SLabel(contact.getPhone(), SLabel.TYPE_COMMUN);
		add(jl_fixe_number, "cell 3 0");
		
		SButton lblNewLabel_2 = new SButton("", "Modifier le contact");
		lblNewLabel_2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				new Add_Contact_Controller(ctrl_client.getApplicationController(), contact.getClient(), contact);
				ctrl_client.updateContactList();
			}
		});
		lblNewLabel_2.setIcon(new ImageIcon(Contact_View.class.getResource("/icons/update_contact.png")));
		add(lblNewLabel_2, "cell 4 0");  
		
		SLabel label_4 = new SLabel("", SLabel.TYPE_COMMUN);
		label_4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		label_4.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				if(!contact.getEmail().isEmpty()) {
					String mail = "mailto:"+contact.getEmail();
					try {
						Desktop.getDesktop().mail(new URI(mail));
					} catch (IOException | URISyntaxException e) {
						e.printStackTrace();
					} 
				}
			}
		});
		label_4.setIcon(new ImageIcon(Contact_View.class.getResource("/icons/mail.png")));
		add(label_4, "cell 0 1");
		
		jl_mail = new SLabel(contact.getEmail(), SLabel.TYPE_COMMUN);
		add(jl_mail, "cell 1 1");
		
		SLabel label_3 = new SLabel("", SLabel.TYPE_COMMUN);
		label_3.setIcon(new ImageIcon(Contact_View.class.getResource("/icons/mobile.png")));
		add(label_3, "cell 2 1");
		
		jl_mobile_number = new SLabel(contact.getMobile(), SLabel.TYPE_COMMUN);
		add(jl_mobile_number, "cell 3 1");
		
		SButton lblNewLabel_3 = new SButton("", "Supprimer le contact");
		lblNewLabel_3.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if(SOptionPane.showYesNoMessage("Supprimer", "Voulez vous vraiment supprimer ce contact ?") == SOptionPane.YES_OPTION)
				{
					new Contact_DBAO("con").delete(contact);
					ctrl_client.updateContactList();
				}
			}
		});
		lblNewLabel_3.setIcon(new ImageIcon(Contact_View.class.getResource("/icons/remove_contact.png")));
		add(lblNewLabel_3, "cell 4 1");

	}

}
