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.Profil;
import DAO.LieuxDAO;
import DAO.ProfilDAO;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class Fenetre_Creer_Profil extends JFrame {

	private JPanel contentPane;
	protected JTextField titreprofil;
	protected JTextField nom_lieu;
	private Fenetre_Gerer_Profil fenetre2;
	protected JComboBox<Integer> Heure;
	protected JComboBox<Integer> Min;
	protected JComboBox<Integer> Sec;
	protected JComboBox<Integer> Heure1;
	protected JComboBox<Integer> Sec1;
	protected JComboBox<Integer> Min1;
	private int heure, min, sec; 
	protected JButton btnModifier;
	protected JButton btnCreer;
	

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

	/**
	 * Create the frame.
	 */
	public Fenetre_Creer_Profil() {
		
		ProfilDAO profilDAO = new ProfilDAO();
		LieuxDAO lieuDAO = new LieuxDAO();
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 557, 501);
		contentPane = new JPanel_Background();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Nom Profil : ");
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblNewLabel.setBounds(134, 87, 118, 37);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Horaire Ouverture : ");
		lblNewLabel_1.setForeground(Color.WHITE);
		lblNewLabel_1.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(69, 136, 173, 45);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Horaire Fermerture : ");
		lblNewLabel_2.setForeground(Color.WHITE);
		lblNewLabel_2.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblNewLabel_2.setBounds(69, 192, 156, 62);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Lieu associ\u00E9 : ");
		lblNewLabel_3.setForeground(Color.WHITE);
		lblNewLabel_3.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		lblNewLabel_3.setBounds(124, 265, 118, 45);
		contentPane.add(lblNewLabel_3);
		
		titreprofil = new JTextField();
		titreprofil.setBounds(248, 87, 171, 33);
		contentPane.add(titreprofil);
		titreprofil.setColumns(10);
		
		Heure = new JComboBox<Integer>();
		Heure.setBounds(248, 143, 45, 33);
		for(heure = 0; heure<= 24; heure++) {
			
			Heure.addItem(heure);
		}
		contentPane.add(Heure);
		
		Min = new JComboBox<Integer>();
		Min.setBounds(303, 143, 51, 33);
		for(min = 0; min<= 60; min++) {
			
			Min.addItem(min);
		}
		contentPane.add(Min);
		
		Sec = new JComboBox<Integer>();
		Sec.setBounds(364, 143, 55, 33);
		for(sec = 0; sec<= 60; sec++) {
			
			Sec.addItem(sec);
		}
		contentPane.add(Sec);
		
		nom_lieu = new JTextField();
		nom_lieu.setColumns(10);
		nom_lieu.setBounds(248, 269, 171, 33);
		contentPane.add(nom_lieu);
		
		JButton btnRetour = new JButton("   Retour");
		btnRetour.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(fenetre2 == null) {
					fenetre2 = new Fenetre_Gerer_Profil(); 
					
					fenetre2.setVisible(true);
					
				}
				else {
					
					fenetre2.setVisible(true);
				}		
			}
		});
		btnRetour.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		btnRetour.setBounds(21, 403, 109, 37);
		contentPane.add(btnRetour);
		
		btnCreer = new JButton("  Creer");
		btnCreer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int resultat = 0;
				
				Time Houverture = new Time(Heure.getSelectedIndex(), Min.getSelectedIndex(), Sec.getSelectedIndex());
				Time Hfermerture = new Time(Heure1.getSelectedIndex(), Min1.getSelectedIndex(), Sec1.getSelectedIndex());
				
				int idlieux = lieuDAO.get_Id_Lieu(nom_lieu.getText());
				
				Profil profil = new Profil(titreprofil.getText(), Houverture , Hfermerture, idlieux);
				
				resultat = profilDAO.Creer_profil(profil);
				
				JOptionPane.showMessageDialog(null, resultat + " Profil crée", "Resultat", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnCreer.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		btnCreer.setBounds(162, 361, 138, 45);
		contentPane.add(btnCreer);
		
		Heure1 = new JComboBox<Integer>();
		Heure1.setBounds(248, 204, 45, 33);
		for(heure = 0; heure<= 24; heure++) {
			
			Heure1.addItem(heure);
		}
		contentPane.add(Heure1);
		
		Min1 = new JComboBox<Integer>();
		Min1.setBounds(303, 204, 51, 33);
		for(min = 0; min<= 60; min++) {
			
			Min1.addItem(min);
		}
		contentPane.add(Min1);
		
		Sec1 = new JComboBox<Integer>();
		Sec1.setBounds(364, 204, 55, 33);
		for(sec = 0; sec<= 60; sec++) {
			
			Sec1.addItem(sec);
		}
		contentPane.add(Sec1);
		
		JLabel lblNewLabel_4 = new JLabel("* HH : Mm : Sec");
		lblNewLabel_4.setForeground(Color.WHITE);
		lblNewLabel_4.setFont(new Font("Century Gothic", Font.PLAIN, 12));
		lblNewLabel_4.setBounds(429, 149, 88, 22);
		contentPane.add(lblNewLabel_4);
		
		JLabel label = new JLabel("* HH : Mm : Sec");
		label.setForeground(Color.WHITE);
		label.setFont(new Font("Century Gothic", Font.PLAIN, 12));
		label.setBounds(429, 213, 88, 22);
		contentPane.add(label);
		
		btnModifier = new JButton("  Modifier");
		btnModifier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int resultat = 0;
				
				Time Houverture = new Time(Heure.getSelectedIndex(), Min.getSelectedIndex(), Sec.getSelectedIndex());
				Time Hfermerture = new Time(Heure1.getSelectedIndex(), Min1.getSelectedIndex(), Sec1.getSelectedIndex());
				
				int idlieux = lieuDAO.get_Id_Lieu(nom_lieu.getText());
				
				Profil profil = new Profil(titreprofil.getText(), Houverture , Hfermerture, idlieux);
				
				resultat = profilDAO.Modifier_profil(profil, idlieux);
				
				JOptionPane.showMessageDialog(null, resultat + " Profil modifié", "Resultat", JOptionPane.INFORMATION_MESSAGE);
				
			}
		});
		btnModifier.setFont(new Font("Century Gothic", Font.PLAIN, 16));
		btnModifier.setBounds(329, 361, 138, 45);
		contentPane.add(btnModifier);
	}
}
