package fr.iutfbleau.projetIHM2021FI2.View;

import fr.iutfbleau.projetIHM2021FI2.Controller.*;
import fr.iutfbleau.projetIHM2021FI2.Model.*;

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.ArrayList;

/**
 * La classe ChambresView permet d'afficher les autres chambres disponibles que l'on peut sélectionner.
 *  
 * @version 1.0
 * @author Anice TAJGALT, Gianni MANZARI, Alban DUMAS
 */
public class ChambresView extends JFrame {
    private JLabel instructionsLabel;
    private JButton confirmBtn;
    private JScrollPane scrollPane;


    //          MODELE TABLE
    private DefaultTableModel model;
    //          TABLE
    private JTable table;


    private ArrayList<RecupChambre> rooms;
    private static ChambresView instance = null;


    private ChambresView() {
        super();
        this.rooms = null;

        this.setLayout(new GridBagLayout());
        this.scrollPane = new JScrollPane();
        this.instructionsLabel = new JLabel("Selectionnez une chambre");
        this.confirmBtn = new JButton("Sélectionner");

        SelectButtonController btnListener = new SelectButtonController(this.confirmBtn);
        this.confirmBtn.addActionListener(btnListener);

        this.setSize(550, 600);
        this.setLocation(500,400);
 
        String[] columnName = {"Chambres disponibles"};
        this.model = new DefaultTableModel(null, columnName);
        this.table = new JTable(this.model);
        scrollPane.setViewportView(this.table);


        GridBagConstraints gridBtn = new GridBagConstraints();
        gridBtn.gridx = 0;
        gridBtn.gridy = 2;
        gridBtn.fill = GridBagConstraints.HORIZONTAL;
        gridBtn.insets = new Insets(10, 10, 10, 10);

        GridBagConstraints gridLabel = new GridBagConstraints();
        gridLabel.gridx = 0;
        gridLabel.gridy = 0;
        gridLabel.fill = GridBagConstraints.HORIZONTAL;
        gridLabel.insets = new Insets(10, 10, 10, 10);

        GridBagConstraints gridRoom = new GridBagConstraints();
        gridRoom.gridx = 0;
        gridRoom.gridy = 1;
        gridRoom.fill = GridBagConstraints.BOTH;
        gridRoom.insets = new Insets(10, 10, 10, 10);
        gridRoom.weighty = 10.0;


        this.add(this.instructionsLabel, gridLabel);
        this.add(this.scrollPane, gridRoom);
        this.add(this.confirmBtn, gridBtn);

        this.setVisible(true);
    }

    public void ajoutTuples(String[] data) {
        this.model.addRow(data);
    }

    public void viderTableau() {
        this.model.setRowCount(0);
    }

    public RecupChambre chambreSelectionnee() {

        int selectedRow = this.table.getSelectedRow();

        return this.rooms.get(selectedRow);
    }

    public void update(ArrayList<RecupChambre> rooms) {
        this.rooms = rooms;

        this.viderTableau();

        for (int i = 0; i<rooms.size(); i++) {
            this.ajoutTuples(new String[]{rooms.get(i).monPrint()});
        }
    }

	public static ChambresView getInstance() {
		if (instance == null)
			instance = new ChambresView();

		return instance;
	}
}
