
package projet_freyja.ihm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import projet_freyja.math.Base;
import projet_freyja.math.EspaceVectoriel;

public class WindowDefEV extends JFrame{
    
    //Attributs
    protected JLabel labelLibelle;
    protected JTextField libelle;
    protected JLabel labelBase;
    protected JComboBox base;
    protected JLabel labelCorps;
    protected ButtonGroup choixCorps;
    protected JRadioButton r;
    protected JRadioButton c;
    protected JLabel labelDimension;
    protected JTextField dimension;
    //protected JRadioButton infinie;
    protected JButton boutonEnregistrer;
    
    public WindowDefEV(){
        //Création de la fenêtre
        this.setTitle("New espace vectoriel");
        this.setSize(800, 600);
        
        //Création des composants
        labelLibelle = new JLabel("Libellé : ");
        libelle = new JTextField("Entrez un libellé");
        labelBase = new JLabel("Base : ");
        base = new JComboBox();
        labelCorps = new JLabel("Corps : ");
        choixCorps = new ButtonGroup();
        r = new JRadioButton("R");
        c = new JRadioButton("C");
        choixCorps.add(r);
        choixCorps.add(c);
        labelDimension = new JLabel("Dimension : ");
        dimension = new JTextField("Entrez une dimension");
        //infinie = new JRadioButton("Infinie");
        boutonEnregistrer = new JButton("Enregistrer");
        
        //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++){
            nomBase = (fichiers[i].getName() != null) ? fichiers[i].getName().substring(0,fichiers[i].getName().indexOf('.')) : "";
            base.addItem(nomBase);
            nomBase = "";
        }
        
        //Mise sur écoute du boutons
        boutonEnregistrer.addActionListener(new ButtonEnregistrerListener());
        
        //Mise en place des composants
            //1ere ligne
            JPanel l1 = new JPanel();
            l1.setLayout(new BoxLayout(l1, BoxLayout.LINE_AXIS));
            l1.add(labelLibelle);
            l1.add(libelle);
            
            //2eme ligne
            JPanel l2 = new JPanel();
            l2.setLayout(new BoxLayout(l2, BoxLayout.LINE_AXIS));
            l2.add(labelBase);
            l2.add(base);
            
            //3eme ligne
            JPanel l3 = new JPanel();
            l3.setLayout(new BoxLayout(l3, BoxLayout.LINE_AXIS));
            l3.add(labelCorps);
            l3.add(r);
            l3.add(c);
            
            //4eme ligne
            JPanel l4 = new JPanel();
            l4.setLayout(new BoxLayout(l4, BoxLayout.LINE_AXIS));
            l4.add(labelDimension);
            l4.add(dimension);
            //l4.add(infinie);
            
            //5eme ligne
            JPanel l5 = new JPanel();
            l5.setLayout(new BoxLayout(l5, BoxLayout.LINE_AXIS));
            l5.add(boutonEnregistrer);
            
            //Mise en colone
            JPanel col = new JPanel();
            col.setLayout(new BoxLayout(col, BoxLayout.PAGE_AXIS));
            col.add(l1);
            col.add(l2);
            col.add(l3);
            col.add(l4);
            col.add(l5);
            
        //Ajout du conteneur
        this.getContentPane().add(col);
        
        //Dimensionnement de la fenêtre
        this.pack();
        
        //Affichage de la fenêtre
        this.setVisible(true);
    }
    
    //Ecoute des boutons
    class ButtonEnregistrerListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){
            String lib = libelle.getText();
            int dim = Integer.parseInt(dimension.getText());
            
            //On récupère la base
            ObjectInputStream ois = null;
            try {
                //On tente d'ouvrir le fichier
                ois = new ObjectInputStream(new FileInputStream("src\\projet_freyja\\donnees\\bases\\"+base.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 créer l'application linéaire
                EspaceVectoriel e = new EspaceVectoriel(lib, b, dim, "");
                if(r.isSelected()){
                    e.setCorps(r.getText());
                }
                
                else if(c.isSelected()){
                    e.setCorps(c.getText());
                }
                
                //On sérialise l'espace vectoriel
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\projet_freyja\\donnees\\espacesVectoriels\\"+lib+".obj"));
                oos.writeObject(e);
                JOptionPane popup = new JOptionPane();
                popup.showMessageDialog(null, "Espace Vectoriel créé", "Information", JOptionPane.INFORMATION_MESSAGE);
                oos.close();         
            } catch (IOException ex) {
                Logger.getLogger(WindowDefAL.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(WindowDefAL.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);
            } 
        }
    }
    
}
