package fr.iutfbleau.projetIHM2021FI2.View;

import fr.iutfbleau.projetIHM2021FI2.Controller.*;
import fr.iutfbleau.projetIHM2021FI2.Model.*;
import fr.iutfbleau.projetIHM2021FI2.API.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;

/**
 * Ce fichier permet d'ouvrir la fenetre principale. Au démarrage, après avoir exécuté la commande "make", l'utilisateur se retrouve sur cette page.
 * Cette page permet à l'utilisateur de faire une recherche ainsi que de valider une préréservation.
 *  
 * @version 1.0
 * @author Anice TAJGALT, Gianni MANZARI, Alban DUMAS
 */
public class WindowView extends JFrame {

    //              CONSTRUCTEUR
    public  WindowView() {
        
        super("Finir la réservation d'une Chambre");
        this.setSize(1200, 700);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //          CREATION PANEL
        JPanel panel = new JPanel();

        
        //          ELEMENTS DU PANEL
        JLabel phrase1 = new JLabel("Effectuer une recherche :");        
        JTextField reference = new JTextField("rentrer la référence");
        reference.setPreferredSize(new Dimension(200, 30));
        JTextField nom = new JTextField("rentrer le nom");
        nom.setPreferredSize(new Dimension(200, 30));
        JTextField prenom = new JTextField("rentrer le prenom");
        prenom.setPreferredSize(new Dimension(200, 30));


        //          BOUTON
        JButton buttonSearch = new JButton("Rechercher");
        JButton selectR = new JButton("Valider la préréservation");

        //          LISTENERS
        SearchButtonController searchBtnListener = new SearchButtonController(nom, prenom, reference);
        buttonSearch.addActionListener(searchBtnListener);
        SelectButtonController selectBtnListener = new SelectButtonController(selectR);
        selectR.addActionListener(selectBtnListener);

        //          VIDER LE TEXTE DES CHAMPS
        nom.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {}         
            @Override
            public void mousePressed(MouseEvent e) {}          
            @Override
            public void mouseExited(MouseEvent e) {}           
            @Override
            public void mouseEntered(MouseEvent e) {}          
            @Override
            public void mouseClicked(MouseEvent e) {
                JTextField texteField = ((JTextField)e.getSource());
                texteField.setText("");
            }
        });
        reference.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {}         
            @Override
            public void mousePressed(MouseEvent e) {}          
            @Override
            public void mouseExited(MouseEvent e) {}           
            @Override
            public void mouseEntered(MouseEvent e) {}          
            @Override
            public void mouseClicked(MouseEvent e) {
                JTextField texteField = ((JTextField)e.getSource());
                texteField.setText("");
            }
        });
        prenom.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {}         
            @Override
            public void mousePressed(MouseEvent e) {}          
            @Override
            public void mouseExited(MouseEvent e) {}           
            @Override
            public void mouseEntered(MouseEvent e) {}          
            @Override
            public void mouseClicked(MouseEvent e) {
                JTextField texteField = ((JTextField)e.getSource());
                texteField.setText("");
            }
        });


        PrereservationView tableau = PrereservationView.getInstance();
        PrereservationFactoryModel prefactory = new PrereservationFactoryModel();
        Set<Prereservation> preresList = prefactory.getAllPrereservations();


        //          REMPLIS LE TABLEAU
        for (Prereservation currRes : preresList) {
            String[] rowToAdd = {currRes.getReference(),
                                 currRes.getDateDebut().toString(),
                                 String.valueOf(currRes.getJours()),
                                 currRes.getTypeChambre().name()};

            PrereservationView.getInstance().ajoutTuples(rowToAdd);
        }


        panel.setLayout(new GridBagLayout());

        //             TEXTE
        GridBagConstraints gridP = new GridBagConstraints();
        gridP.gridx = 0;
        gridP.gridy = 0;
        gridP.fill = GridBagConstraints.HORIZONTAL;
        gridP.insets = new Insets(20, 20, 20, 20);

        GridBagConstraints gridN = new GridBagConstraints();
        gridN.gridx = 1;
        gridN.gridy = 0;
        gridN.fill = GridBagConstraints.HORIZONTAL;
        gridN.insets = new Insets(20, 20, 20, 20);

        GridBagConstraints gridR = new GridBagConstraints();
        gridR.gridx = 2;
        gridR.gridy = 0;
        gridR.gridwidth = 1;
        gridR.fill = GridBagConstraints.HORIZONTAL;
        gridR.insets = new Insets(20, 20, 20, 20);

        //             BOUTON 
        GridBagConstraints gridS = new GridBagConstraints();
        gridS.gridx = 0;
        gridS.gridy = 1;
        gridS.fill = GridBagConstraints.HORIZONTAL;
        gridS.insets = new Insets(20, 20, 20, 20);

        GridBagConstraints gridSelect = new GridBagConstraints();
        gridSelect.gridx = 0;
        gridSelect.gridy = 4;
        gridSelect.gridwidth = 2;
        gridSelect.fill = GridBagConstraints.BOTH;
        gridSelect.insets = new Insets(20, 20, 20, 20);


        //              TABLEAU
        GridBagConstraints gridT = new GridBagConstraints();
        gridT.gridx = 0;
        gridT.gridy = 3;
        gridT.gridwidth = 4;
        gridT.fill = GridBagConstraints.BOTH;
        gridT.insets = new Insets(20, 20, 20, 20);
        gridT.weightx = 1.0;
        gridT.weighty = 1.0;

        panel.add(nom,gridN);
        panel.add(prenom,gridP);
        panel.add(buttonSearch,gridS);
        panel.add(reference,gridR);
        panel.add(tableau, gridT);
        panel.add(selectR, gridSelect);
        
        
        this.add(panel);
        this.setVisible(true);
    }
}