
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generateurmdp;

import com.mysql.jdbc.Statement;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author cedrickaiser2
 */
public class ConnectionLog implements ActionListener{

    private final String ACTION_VALIDER = "OK";
    private final String ACTION_NEWUSER = "New User";
    private JFrame JFrameLogin;
    private JLabel JLabLogin;
    private JLabel JLabMDP;
    private JTextField JTxtLogin;
    private JPasswordField JTxtMDP;
    private JButton btValider;
    private JButton btNewUser;
    private Connection connec;

    ConnectionLog() {
        JFrameLogin = new JFrame();
        JFrameLogin.setSize(200, 200);
        JFrameLogin.setLayout(null);

        JLabLogin = new JLabel("Login :");
        JLabLogin.setLocation(10, 10);
        JLabLogin.setSize(150, 20);

        JTxtLogin = new JTextField("test");
        JTxtLogin.setLocation(10, 30);
        JTxtLogin.setSize(150, 20);

        JLabMDP = new JLabel("Mot de passe :");
        JLabMDP.setLocation(10, 50);
        JLabMDP.setSize(150, 20);

        JTxtMDP = new JPasswordField("test");
        JTxtMDP.setLocation(10, 70);
        JTxtMDP.setSize(150, 20);


        btValider = new JButton(ACTION_VALIDER);
        btValider.setLocation(10, 110);
        btValider.setSize(150, 20);
        btValider.setEnabled(true);
        btValider.addActionListener(this);

        btNewUser = new JButton(ACTION_NEWUSER);
        btNewUser.setLocation(10, 135);
        btNewUser.setSize(150, 20);
        btNewUser.setEnabled(true);
        btNewUser.addActionListener(this);

        JFrameLogin.getContentPane().add(JLabLogin);
        JFrameLogin.getContentPane().add(JLabMDP);
        JFrameLogin.getContentPane().add(JTxtLogin);
        JFrameLogin.getContentPane().add(JTxtMDP);
        JFrameLogin.getContentPane().add(btValider);
        JFrameLogin.getContentPane().add(btNewUser);

        JFrameLogin.setVisible(true);
    }

    public boolean isValid() {
        ConnectionBDD cbdd = ConnectionBDD.getInstance();
        connec = cbdd.getConnexion();

        String requete = "select login, mdp from user where login = '" + JTxtLogin.getText() + "' and mdp = '" + JTxtMDP.getText() + "'";


        Statement st;
        try {
            st = (Statement) connec.createStatement();
            ResultSet rs = st.executeQuery(requete);
            System.out.print(st);

            while(rs.next()){
                Fenetre fen = new Fenetre(JTxtLogin.getText());
            }
            
            return true;

        } catch (SQLException ex) {
            System.out.println("Erreur de selection de la Base de Données : " + ex.getMessage());
            return false;
        }
    }

    public void actionPerformed(ActionEvent e) {
        String action = e.getActionCommand();
        if (action.equals(ACTION_VALIDER)) {
            if(isValid()) JFrameLogin.dispose();
        }
        if (action.equals(ACTION_NEWUSER)) {
            NewUser user = new NewUser();
        }
    }
}
