package client.control.probleme;

import client.Application;
import client.ApplicationHelper;
import client.model.problem.Methode;
import client.view.PanelGenerale;
import client.view.admin.PanelAdmin;
import client.view.member.PanelMembre;
import client.view.onglet.OngletSpecial;
import client.view.respoGroup.PanelRespoGroupe;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;

/**
 * <u><b>Explication générale de la classe :</b></u>
 * <P>
 * Cette classe permet de gérer l'évènement lié à la JComboBox des Méthodes
 * 
 * @author Dream Team - ING2
 */
public class ControleurComboBoxMethode implements ActionListener {

	//Déclaration des variables
    PanelRespoGroupe prg;
    PanelMembre pm;

    public ControleurComboBoxMethode() {}

    /**
	 * <u><i>Explication générale de la méthode :</i></u> <P> Cette méthode permet
	 * de gérer les évènements liés à la souris
	 * 
	 * @param e
	 *            Evènement souris
	 * 
	 */
    public void actionPerformed(ActionEvent e) {
        
        Component c = (Component) e.getSource();

        prg = null;
        pm = null;

        while (c != null) {
            if (c instanceof PanelRespoGroupe) {
                prg = (PanelRespoGroupe) c;
            } else if (c instanceof PanelMembre) {
                pm = (PanelMembre) c;
            }
            c = c.getParent();
        }
        
        JComboBox cb = (JComboBox) e.getSource();

        String selection = (String) cb.getSelectedItem();

        //Pour les responsables de groupe
        if (prg != null) {
            
            if (selection.equals("Méthodes déterministes") || selection.equals("------------------------------------------------")
                    || selection.equals(("Méthodes non déterministes"))) {

            	prg.getProbleme().getPanExecPb().CacherLabel();

            }

            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_PAS_FIXE)) {

                BloquerPanelRespo();
                prg.getProbleme().getPanExecPb().setVisible(true);
                prg.getProbleme().getPanExecPb().stepFixedMethod();
            }

            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_PAS_OPTIMAL)) {

                BloquerPanelRespo();
                prg.getProbleme().getPanCreerPb().getTableContrainte().setEnabled(false);
                prg.getProbleme().getPanExecPb().setVisible(true);
                prg.getProbleme().getPanExecPb().stepOptimalMethod();

            }

            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_NEWTON)) {

                BloquerPanelRespo();
                prg.getProbleme().getPanExecPb().setVisible(true);
                prg.getProbleme().getPanExecPb().newtonMethod();
            }
            
            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_RECUIT_SIMULE)) {

                BloquerPanelRespo();
                prg.getProbleme().getPanExecPb().setVisible(true);
                prg.getProbleme().getPanExecPb().recuitSimuleMethod();
            }
            
            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_TABOU)) {

                BloquerPanelRespo();
                prg.getProbleme().getPanExecPb().setVisible(true);
                prg.getProbleme().getPanExecPb().tabouMethod();
            }
            
            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_ALGO_EVOLUTIONNAIRE)) {

                BloquerPanelRespo();
                prg.getProbleme().getPanExecPb().setVisible(true);
                prg.getProbleme().getPanExecPb().geneticMethod();
            }
            
        //Pour les membres
        }else if (pm != null) {

            if (selection.equals("Méthodes déterministes") || selection.equals("------------------------------------------------")
                    || selection.equals(("Méthodes non déterministes"))) {

                pm.getProbleme().getPanExecPb().CacherLabel();

            }

            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_PAS_FIXE)) {

                BloquerPanelMembre();
                pm.getProbleme().getPanExecPb().setVisible(true);
                pm.getProbleme().getPanExecPb().stepFixedMethod();
            }

            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_PAS_OPTIMAL)) {

                BloquerPanelMembre();
                pm.getProbleme().getPanCreerPb().getTableContrainte().setEnabled(false);
                pm.getProbleme().getPanExecPb().setVisible(true);
                pm.getProbleme().getPanExecPb().stepOptimalMethod();

            }

            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_NEWTON)) {

                BloquerPanelMembre();
                pm.getProbleme().getPanExecPb().setVisible(true);
                pm.getProbleme().getPanExecPb().newtonMethod();
            }
            
            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_RECUIT_SIMULE)) {

                BloquerPanelMembre();
                pm.getProbleme().getPanExecPb().setVisible(true);
                pm.getProbleme().getPanExecPb().recuitSimuleMethod();
            }
            
            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_TABOU)) {

                BloquerPanelMembre();
                pm.getProbleme().getPanExecPb().setVisible(true);
                pm.getProbleme().getPanExecPb().tabouMethod();
            }
            
            //On affiche les éléments en fonction de la méthode choisie
            if (selection.equals(Methode.METHODE_ALGO_EVOLUTIONNAIRE)) {

                BloquerPanelMembre();
                pm.getProbleme().getPanExecPb().setVisible(true);
                pm.getProbleme().getPanExecPb().geneticMethod();
            }
            
        }
    }

    /** <u><i>Explication générale de la méthode :</i></u> 
	 * <P> Cette méthode permet de bloquer les panels des responsable de groupe
	 * 
	 */
    public void BloquerPanelRespo() {

        //On recupère l'onglet en cours
        OngletSpecial os = ((OngletSpecial) PanelGenerale.getInstance().getVueOnglet().getTabComponentAt(PanelGenerale.getInstance().getVueOnglet().getSelectedIndex()));

        int idUser = Application.idSelect;
        int idGroup = Application.getInstance().getIdGroupe(os.getTitle());
        int idRole = ApplicationHelper.IdRoleOfUserInGroup(idUser, idGroup);

        PanelGenerale.getInstance().getVueOnglet().setEnabled(false);
        if (ApplicationHelper.UserHaveGoodRole(idUser, idGroup, idRole)) {
            prg.getJtbOperation().setEnabled(false);
        } else if (ApplicationHelper.UserHaveGoodRole(Application.idSelect, idGroup, idRole)) {
            PanelAdmin.getInstance().setEnabled(false);
        }

        for (int i = 0; i < prg.getProbleme().getPanChoixPb().getComponentCount(); i++) {
            prg.getProbleme().getPanChoixPb().getComponent(i).setEnabled(false);
        }
    }

    /** <u><i>Explication générale de la méthode :</i></u> 
	 * <P> Cette méthode permet de bloquer les panels des membres
	 * 
	 */
    public void BloquerPanelMembre() {

        //On recupère l'onglet en cours
        OngletSpecial os = ((OngletSpecial) PanelGenerale.getInstance().getVueOnglet().getTabComponentAt(PanelGenerale.getInstance().getVueOnglet().getSelectedIndex()));

        int idUser = Application.idSelect;
        int idGroup = Application.getInstance().getIdGroupe(os.getTitle());
        int idRole = ApplicationHelper.IdRoleOfUserInGroup(idUser, idGroup);

        PanelGenerale.getInstance().getVueOnglet().setEnabled(false);
        if (ApplicationHelper.UserHaveGoodRole(idUser, idGroup, idRole)) {
            pm.getJtbOperation().setEnabled(false);
        }

        for (int i = 0; i < pm.getProbleme().getPanChoixPb().getComponentCount(); i++) {
            pm.getProbleme().getPanChoixPb().getComponent(i).setEnabled(false);
        }
    }
}
