package fr.iutfbleau.projetIHM2022FI2.Permanent.View;
import javax.swing.*;

import fr.iutfbleau.projetIHM2022FI2.Permanent.Util.User;

import java.awt.*;

public class Connexion extends JOptionPane{

    public static User seConnecter(){
        JFrame fenetre=new JFrame();
        fenetre.setSize(10,10);
        fenetre.setVisible(true);
        JPanel panel = new JPanel(new GridLayout(2,1));
        User[] use=new User[3];
        use[0]=User.ROOT;
        use[1]=User.PROF;
        use[2]=User.ETUDIANT;

        JComboBox<User> combo=new JComboBox<User>(use);

        JPanel myPanel = new JPanel();
        JTextField password = new JPasswordField(20);
        myPanel.add(new JLabel("Password:"));
        myPanel.add(password);
        panel.add(combo);
        panel.add(myPanel);

        if(JOptionPane.showConfirmDialog(fenetre, panel, "login", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION){
            fenetre.dispose();
            return null;
        }
        User choose=null;
        switch(combo.getSelectedIndex()){
            case 0:
                choose=use[0];
                break;
            case 1:
                choose=use[1];
                break;
            case 2:
                choose=use[2]; 
                break;
        }
        if(choose==User.ROOT){
            if(password.getText().equals("root")){
                fenetre.dispose();
                return User.ROOT;
            }
        }
        if(choose==User.PROF){
            if(password.getText().equals("prof")){
                fenetre.dispose();
                return User.PROF;
            }
        }
        if(choose==User.ETUDIANT){
            if(password.getText().equals("etuiutsen")){
                fenetre.dispose();
                return User.ETUDIANT;
            }
        }
        User ret=Connexion.retry(choose, fenetre);
        fenetre.dispose();
        return ret;
    }

    private static User retry(User first, JFrame fenetre){
        JPanel panel = new JPanel(new GridLayout(2,1));
        User[] use=new User[3];
        if(first==User.ETUDIANT){
            use[0]=User.ETUDIANT;
            use[1]=User.ROOT;
            use[2]=User.PROF;
        }

        if(first==User.ROOT){
            use[1]=User.ETUDIANT;
            use[0]=User.ROOT;
            use[2]=User.PROF;
        }

        if(first==User.PROF){
            use[1]=User.ETUDIANT;
            use[2]=User.ROOT;
            use[0]=User.PROF;
        }
        JComboBox<User> combo=new JComboBox<User>(use);

        JPanel myPanel = new JPanel();
        JTextField password = new JPasswordField(20);
        myPanel.add(new JLabel("Password:"));
        myPanel.add(password);
        JLabel erre=new JLabel("mdp incorect", SwingConstants.RIGHT);
        erre.setForeground(Color.RED);
        myPanel.add(erre);

        panel.add(combo);
        panel.add(myPanel);

        if(JOptionPane.showConfirmDialog(fenetre, panel, "login", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION){
            return null;
        }
        User choose=null;
        switch(combo.getSelectedIndex()){
            case 0:
                choose=use[0];
                break;
            case 1:
                choose=use[1];
                break;
            case 2:
                choose=use[2]; 
                break;
        }
        if(choose==User.ROOT){
            if(password.getText().equals("root")){
                fenetre.dispose();
                return User.ROOT;
            }
        }
        if(choose==User.PROF){
            if(password.getText().equals("prof")){
                fenetre.dispose();
                return User.PROF;
            }
        }
        if(choose==User.ETUDIANT){
            if(password.getText().equals("etuiutsen")){
                fenetre.dispose();
                return User.ETUDIANT;
            }
        }
        User ret=Connexion.retry(choose, fenetre);
        fenetre.dispose();
        return ret;
    }

    public static int Persistant(){
        JFrame fenetre=new JFrame();
        fenetre.setSize(10,10);
        fenetre.setVisible(true);
        String[] value=new String[2];
        value[0]="Modèle Persistant";
        value[1]="Modèle Non Persistant";
        String result;
        if((result=(String)JOptionPane.showInputDialog(fenetre, "Choisir un modèle de donnée", "Model de Donnée", JOptionPane.QUESTION_MESSAGE, null, value, value[0]))==null){
            fenetre.dispose();
            return 0;
        }
        if(result.equals(value[0])){
            fenetre.dispose();
            return 1;
        }
        if(result.equals(value[1])){
            fenetre.dispose();
            return 2;
        }
        return 0;
    }

}
