package fr.iutfbleau.projetIHM2021FI2.Controller;

import fr.iutfbleau.projetIHM2021FI2.View.*;
import fr.iutfbleau.projetIHM2021FI2.API.*;
import fr.iutfbleau.projetIHM2021FI2.Model.PrereservationFactoryModel;

import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;   
import javax.swing.*;
import java.util.Set;

/**
 * La classe SearchButtonController permet de répertorier les actions effectués lors d'une recherche.
 *  
 * @version 1.0
 * @author Anice TAJGALT, Gianni MANZARI, Alban DUMAS
 */
public class SearchButtonController implements ActionListener {
    private JTextField champNom;
    private JTextField champPrenom;
    private JTextField champRef;

    public SearchButtonController(JTextField champNom, JTextField champPrenom, JTextField champRef) {
        this.champNom = champNom;
        this.champPrenom = champPrenom;
        this.champRef = champRef;
    }

    public void fillTable(String nom, String prenom) {



        PrereservationFactoryModel factory = new PrereservationFactoryModel();
        Set<Prereservation> cResSet = factory.getPrereservations(nom, prenom);


        for (Prereservation currRes : cResSet) {
            String[] rowToAdd = {currRes.getReference(), currRes.getDateDebut().toString(), String.valueOf(currRes.getJours()), currRes.getTypeChambre().name()};

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

    public void fillTable(String reference) {


        PrereservationFactoryModel factory = new PrereservationFactoryModel();
        Prereservation cRes = factory.getPrereservation(reference);

        String[] rowToAdd = {cRes.getReference(), cRes.getDateDebut().toString(), String.valueOf(cRes.getJours()), cRes.getTypeChambre().name()};

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

    public void actionPerformed(ActionEvent e)
    {

        PrereservationView.getInstance().viderTableau();
        System.out.println(this.champNom.getText() + champPrenom.getText() + this.champRef.getText());

        if (this.champRef.getText().isEmpty()) {
            fillTable(this.champNom.getText(), champPrenom.getText());
        }
        else {
            fillTable(this.champRef.getText());
        }
    }
}