package Fenetres;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import ClassesJava.Personne;
import ClassesJava.Profil;
import DAO.PersonneDAO;
import DAO.ProfilDAO;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Fenetre_Recherche_Profil extends JFrame {

	private JPanel contentPane;
	protected JTextField nomPersonne;
	protected JTextField nomprofil;
	protected JTextField prenomPersonne;
	protected JTextField nouveauProfil;
	protected JLabel lblNouveauProfil;
	protected JLabel lblNomPersonne;
	protected JLabel lblPrenomPersonne;
	protected JLabel nomProfil;
	protected JButton Rechercher, SupprimerProfil, Switchprofil;
	protected Fenetre_Resultats_Profil fenetre1;
	private Profil profil;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Fenetre_Recherche_Profil frame = new Fenetre_Recherche_Profil();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Fenetre_Recherche_Profil() {
		
		ProfilDAO profilDAO = new ProfilDAO();
		PersonneDAO personneDAO = new PersonneDAO();
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 832, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblNomPersonne = new JLabel("Nom Personne: ");
		lblNomPersonne.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblNomPersonne.setBounds(450, 29, 154, 49);
		contentPane.add(lblNomPersonne);
		
		nomPersonne = new JTextField();
		nomPersonne.setBounds(574, 38, 176, 36);
		contentPane.add(nomPersonne);
		nomPersonne.setColumns(10);
		
		Rechercher = new JButton("Rechercher Profil");
		Rechercher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				String nom = nomProfil.getText();
				profil = profilDAO.getProfil(nom);
							
						if(fenetre1 == null) {
								fenetre1 = new Fenetre_Resultats_Profil(); 
								fenetre1.listeProfil.addElement(profil);
								fenetre1.listeProfils.addElement(profil.afficher());
								fenetre1.setVisible(true);	
							}	
				
			}
		});
		Rechercher.setFont(new Font("Century Gothic", Font.PLAIN, 14));
		Rechercher.setBounds(129, 182, 163, 49);
		contentPane.add(Rechercher);
		
		SupprimerProfil = new JButton("Supprimer Profil");
		SupprimerProfil.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int sup = 0;
				
				profil = profilDAO.getProfil(nomprofil.getText());
				
				sup = profilDAO.Supprimer_profil(profil);
				
				JOptionPane.showMessageDialog(null, sup + " Profil supprimé", "Resultat", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		SupprimerProfil.setFont(new Font("Century Gothic", Font.PLAIN, 14));
		SupprimerProfil.setBounds(323, 182, 163, 49);
		contentPane.add(SupprimerProfil);
		
		Switchprofil = new JButton("Changer Profil Personne");
		Switchprofil.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				int res = 0;
				
				Personne personne = personneDAO.getPersonne(nomPersonne.getText(),prenomPersonne.getText());
				profil = profilDAO.getProfil(nouveauProfil.getText());
				
				int idprofil = profilDAO.get_Id_Profil(profil.getTitre_profil());
				
				res = personneDAO.ajouter_profil(personne, idprofil);
				 
				JOptionPane.showMessageDialog(null, " Profil mofifié ", "Resultat", JOptionPane.INFORMATION_MESSAGE);
				
			}
		});
		Switchprofil.setFont(new Font("Century Gothic", Font.PLAIN, 14));
		Switchprofil.setBounds(513, 182, 220, 49);
		contentPane.add(Switchprofil);
		
		nomProfil = new JLabel("Nom du Profil : ");
		nomProfil.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		nomProfil.setBounds(38, 29, 140, 49);
		contentPane.add(nomProfil);
		
		nomprofil = new JTextField();
		nomprofil.setColumns(10);
		nomprofil.setBounds(167, 38, 176, 36);
		contentPane.add(nomprofil);
		
		prenomPersonne = new JTextField();
		prenomPersonne.setColumns(10);
		prenomPersonne.setBounds(574, 112, 176, 36);
		contentPane.add(prenomPersonne);
		
		lblPrenomPersonne = new JLabel("Prenom Personne : ");
		lblPrenomPersonne.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblPrenomPersonne.setBounds(424, 103, 154, 49);
		contentPane.add(lblPrenomPersonne);
		
		nouveauProfil = new JTextField();
		nouveauProfil.setColumns(10);
		nouveauProfil.setBounds(167, 112, 176, 36);
		contentPane.add(nouveauProfil);
		
		lblNouveauProfil = new JLabel("Nouveau Profil : ");
		lblNouveauProfil.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblNouveauProfil.setBounds(26, 103, 140, 49);
		contentPane.add(lblNouveauProfil);
	}
}
