
package projet_freyja.ihm;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import projet_freyja.math.ApplicationLineaire;
import projet_freyja.math.Vecteur;

public class WindowAL extends JFrame{
    
    //Attributs
    protected JButton boutonAjout; 
    protected JComboBox alCrees;
    protected JButton boutonSelection;
    protected JLabel labelAlCrees;
    protected JTextArea cadreInfo;
    protected JLabel labelCadreInfo;
    protected JTextField saisieVecteur;
    protected JButton boutonCalculImage;
    protected JLabel labelRes;
    protected JTextArea res;
    protected ApplicationLineaire al;
    
    public WindowAL(){
        this.setTitle("Applications Linéaires Window");
        this.setSize(800, 600);
        
        //Création des composants
        boutonAjout = new JButton("Ajouter une application linéaire");
        labelAlCrees = new JLabel("Applications linéaires disponibles : ");
        alCrees = new JComboBox();
        boutonSelection = new JButton("Sélectionner");
        labelCadreInfo = new JLabel("Informations de la sélection : ");
        cadreInfo = new JTextArea();
        saisieVecteur = new JTextField("Saisissez un vecteur");
        boutonCalculImage = new JButton("Calculer l'image");
        labelRes = new JLabel("Résultat : ");
        res = new JTextArea();
        al = null;
        
        //Contenu combobox
        File rep = new File("src\\projet_freyja\\donnees\\applicationsLineaires");
        File[] fichiers = rep.listFiles();
        String nomBase = "";
        for(int i = 0; i < fichiers.length; i++){
            //On récupère le nom du fichier sans l'extension
            nomBase = (fichiers[i].getName() != null) ? fichiers[i].getName().substring(0,fichiers[i].getName().indexOf('.')) : "";
            alCrees.addItem(nomBase);
            nomBase = "";
        }
        
        //Mise sur écoute des boutons
        boutonAjout.addActionListener(new ButtonAjoutListener());
        boutonSelection.addActionListener(new ButtonSelectionListener());
        boutonCalculImage.addActionListener(new ButtonCalculImageListener());
        
        //Paramétrage des panels
        cadreInfo.setBackground(Color.white);
        cadreInfo.setPreferredSize(new Dimension(400, 400));
        res.setBackground(Color.white);
        res.setPreferredSize(new Dimension(100, 20));
        
        //Mise en place des composants
            //1ere ligne
            JPanel l1 = new JPanel();
            l1.setLayout(new BoxLayout(l1, BoxLayout.LINE_AXIS));
            l1.add(boutonAjout);
            
            //2eme ligne
            JPanel l2 = new JPanel();
            l2.setLayout(new BoxLayout(l2, BoxLayout.LINE_AXIS));
            l2.add(labelAlCrees);
            l2.add(alCrees);
            l2.add(boutonSelection);
            
            //3eme ligne
            JPanel l3 = new JPanel();
            l3.setLayout(new BoxLayout(l3, BoxLayout.LINE_AXIS));
            l3.add(labelCadreInfo);
            
            //4eme ligne
            JPanel l4 = new JPanel();
            l4.setLayout(new BoxLayout(l4, BoxLayout.LINE_AXIS));
            l4.add(cadreInfo);
            
            //5eme ligne
            JPanel l5 = new JPanel();
            l5.setLayout(new BoxLayout(l5, BoxLayout.LINE_AXIS));
            l5.add(saisieVecteur);
            l5.add(boutonCalculImage);
            
            //6eme ligne
            JPanel l6 = new JPanel();
            l6.setLayout(new BoxLayout(l6, BoxLayout.LINE_AXIS));
            l6.add(labelRes);
            l6.add(res);
            
            //Mise en colonne
            JPanel c = new JPanel();
            c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
            c.add(l1);
            c.add(l2);
            c.add(l3);
            c.add(l4);
            c.add(l5);
            c.add(l6);
               
        //Ajout du conteneur
        this.getContentPane().add(c);
        
        this.pack();
        //Affichage de la fenêtre
        this.setVisible(true);
    }
      
    //Ecoute des boutons
    class ButtonAjoutListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){
            WindowDefAL win = new WindowDefAL();
        }
    }
    
    class ButtonSelectionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){
            //On refresh le cadre
            cadreInfo.setText("");
            
            //Lecture de l'application linéaire
            ObjectInputStream ois = null;
            try {
                //On tente d'ouvrir le fichier
                ois = new ObjectInputStream(new FileInputStream("src\\projet_freyja\\donnees\\applicationsLineaires\\"+alCrees.getSelectedItem()+".obj"));
            } catch (IOException ex) {
                Logger.getLogger(WindowDefAL.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                //On essai de désérialiser la base
                al = (ApplicationLineaire)ois.readObject();
                
                //On affiche les informations
                cadreInfo.append("Libellé : "+al.getLibelle()+"\n\n");
                cadreInfo.append("***Espace de départ***\n");
                cadreInfo.append("Libellé : "+al.getDepart().getLibelle()+"\n");
                cadreInfo.append("Dimension : "+al.getDepart().getDimension()+"\n");
                //cadreInfo.append("Corps : "+al.getDepart().getCorps()+"\n");
                cadreInfo.append("  ***Base associée***\n");
                cadreInfo.append("      Libellé : "+al.getDepart().getBase().getLibelle()+"\n");
                cadreInfo.append("      Dimension : "+al.getDepart().getBase().getDimension()+"\n");
                cadreInfo.append("      Dimension des vecteurs : "+al.getDepart().getBase().getDimensionVecteur()+"\n");
                cadreInfo.append("      Famille : {");
                for(int i = 0; i < al.getDepart().getBase().getElements().length; i++){
                    cadreInfo.append("(");
                    for(int j = 0; j < al.getDepart().getBase().getValeur(i).getValeurs().length; j++){
                        if(j != al.getDepart().getBase().getDimensionVecteur()-1){
                            cadreInfo.append(al.getDepart().getBase().getValeur(i).getElement(j)+", ");
                        }
                        else{
                            cadreInfo.append(al.getDepart().getBase().getValeur(i).getElement(j));
                        }
                        
                    }
                    if(i != al.getDepart().getBase().getDimension()-1){
                        cadreInfo.append(");");
                    }
                    else{
                        cadreInfo.append(")");
                    }
                    
                }
                cadreInfo.append("}\n\n");
                cadreInfo.append("***Espace d'arrivé***\n");
                cadreInfo.append("Libellé : "+al.getArrivee().getLibelle()+"\n");
                cadreInfo.append("Dimension : "+al.getArrivee().getDimension()+"\n");
                //cadreInfo.append("Corps : "+al.getArrivee().getCorps()+"\n");
                cadreInfo.append("  ***Base associée***\n");
                cadreInfo.append("      Libellé : "+al.getArrivee().getBase().getLibelle()+"\n");
                cadreInfo.append("      Dimension : "+al.getArrivee().getBase().getDimension()+"\n");
                cadreInfo.append("      Dimension des vecteurs : "+al.getArrivee().getBase().getDimensionVecteur()+"\n");
                cadreInfo.append("      Famille : {");
                for(int i = 0; i < al.getArrivee().getBase().getElements().length; i++){
                    cadreInfo.append("(");
                    for(int j = 0; j < al.getArrivee().getBase().getValeur(i).getValeurs().length; j++){
                        if(j != al.getArrivee().getBase().getDimensionVecteur()-1){
                            cadreInfo.append(al.getArrivee().getBase().getValeur(i).getElement(j)+", ");
                        }
                        else{
                            cadreInfo.append(al.getArrivee().getBase().getValeur(i).getElement(j));
                        }
                        
                    }
                    if(i != al.getArrivee().getBase().getDimension()-1){
                        cadreInfo.append(");");
                    }
                    else{
                        cadreInfo.append(")");
                    }
                    
                }
                cadreInfo.append("}\n\n");
                cadreInfo.append("Matrice associée : \n"+al.matToString()+"\n\n");
                cadreInfo.append("Rang : "+al.calculerRang()+"\n");
            }
            catch (IOException ex) {
                Logger.getLogger(WindowAL.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(WindowAL.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    class ButtonCalculImageListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){
            String vec = saisieVecteur.getText();
            int i = 0;
            int k = 0;
            String nbr;
            int nbrVirgule = 0;
            int c;
            for(int j = 0; j < vec.length(); j++){
                if(vec.charAt(j) == 44){
                    nbrVirgule++;
                }
            }
            String[] coeffS = new String[nbrVirgule + 1];
            double[] coeff = new double[nbrVirgule + 1];
            while(i < vec.length()){ //On parcours la chaine saisie
                if(vec.charAt(i) == 40){ //Si le caractère courant est "("
                    k = 0;
                    nbr = "";
                    c = i+1;
                    while(vec.charAt(c) != 44 && vec.charAt(c) != 41){ //Tant qu'on lit pas ","
                       if(vec.charAt(c) != 40 && vec.charAt(c) != 41){ //On s'assure que c'est pas un mauvais caractère
                            nbr += vec.charAt(c); //On mémorise les chiffres
                       }

                       c++;
                    }    
                    coeff[k] = Double.parseDouble(nbr); //On ajoute les chiffres dans la première case du tableau
                    coeffS[k] = Double.toString(coeff[k]);
                    k++;
                    if(vec.charAt(c) == 41){
              
                        Vecteur v = new Vecteur("Vecteur solution", coeffS);
                        res.setText(al.calculerImage(v).toString());
                    }
                }
                else if(vec.charAt(i) == 44){ //Si le caractère courant est ","
                    if(vec.charAt(i + 1) == 41){
                        Vecteur v = new Vecteur("Vecteur solution", coeffS);
                        res.setText(al.calculerImage(v).toString());
                    }
                    //else{
                        nbr = "";
                        c = i+1;
                        while(vec.charAt(c) != 44 && vec.charAt(c) != 41){//Tant qu'on lit pas "," ou ")"
                            if(vec.charAt(c) != 40){ //On s'assure que c'est pas un mauvais caractère
                                nbr += vec.charAt(c); //On mémorise les chiffres
                            }
                            c++;
                        }
                        coeff[k] = Double.parseDouble(nbr); //On ajoute les chiffres dans le tableau
                        coeffS[k] = Double.toString(coeff[k]);
                        k++;
                        if(vec.charAt(c) == 41){ //Si le caractère courant est ")" => On a lu le vecteur en entier
                            Vecteur v = new Vecteur("Vecteur solution", coeffS);
                            res.setText(al.calculerImage(v).toString());
                        }
                    //}
                }
                i++;//On continue de lire la chaine
            }
        }
    }
}