
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.Base;

public class WindowBase extends JFrame{
    
    //Attributs
    protected JButton boutonAjout; 
    protected JComboBox baseCreees;
    protected JButton boutonSelection;
    protected JLabel labelBaseCreees;
    protected JTextArea cadreInfo;
    protected JLabel labelCadreInfo;
    
    public WindowBase(){
        //Création de la fenêtre
        this.setTitle("Bases Window");
        this.setSize(800, 600);
        
        //Création des composants
        boutonAjout = new JButton("Ajouter une base");
        labelBaseCreees = new JLabel("Bases disponibles : ");
        baseCreees = new JComboBox();
        boutonSelection = new JButton("Sélectionner");
        labelCadreInfo = new JLabel("Informations de la sélection : ");
        cadreInfo = new JTextArea();
        
        //Contenu combobox
        File rep = new File("src\\projet_freyja\\donnees\\bases");
        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('.')) : "";
            baseCreees.addItem(nomBase);
            nomBase = "";
        }
        
        
        //Mise sur écoute des boutons
        boutonAjout.addActionListener(new ButtonAjoutListener());
        boutonSelection.addActionListener(new ButtonSelectionListener());
        
        //Paramétrage du cadre info
        cadreInfo.setBackground(Color.white);
        cadreInfo.setPreferredSize(new Dimension(400, 400));
        
        //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(labelBaseCreees);
            l2.add(baseCreees);
            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);
            
            //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);
               
        //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){
            WindowDefBase win = new WindowDefBase();
        }
    }
    
    class ButtonSelectionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){
            //On refresh le cadre
            cadreInfo.setText("");
            
            //Lecture de la base
            ObjectInputStream ois = null;
            try {
                //On tente d'ouvrir le fichier
                ois = new ObjectInputStream(new FileInputStream("src\\projet_freyja\\donnees\\bases\\"+baseCreees.getSelectedItem()+".obj"));
            } catch (IOException ex) {
                Logger.getLogger(WindowDefAL.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                //On essai de désérialiser la base
                Base b = (Base)ois.readObject();
                
                //On affiche les informations
                cadreInfo.append("Libellé : "+b.getLibelle()+"\n");
                cadreInfo.append("Dimension : "+b.getDimension()+"\n");
                cadreInfo.append("Dimension des vecteurs : "+b.getDimensionVecteur()+"\n");
                //On écrit la famille de vecteurs
                cadreInfo.append("Famille : {");
                for(int i = 0; i < b.getElements().length; i++){
                    cadreInfo.append("(");
                    for(int j = 0; j < b.getValeur(i).getValeurs().length; j++){
                        if(j != b.getDimensionVecteur()-1){
                            cadreInfo.append(b.getValeur(i).getElement(j)+", ");
                        }
                        else{
                            cadreInfo.append(b.getValeur(i).getElement(j));
                        }
                        
                    }
                    if(i != b.getDimension()-1){
                        cadreInfo.append(");");
                    }
                    else{
                        cadreInfo.append(")");
                    }
                    
                }
                cadreInfo.append("}");
            } catch (IOException ex) {
                Logger.getLogger(WindowBase.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(WindowBase.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                //On tente de fermer le flux
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(WindowDefAL.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}