package org.Views;

import org.Models.GridLayoutCarre;
import org.Models.PieceCourante;

import javax.swing.*;
import java.awt.*;

public class VueBandeauControle extends JPanel {
    private JLabel scoreLabel;
    private JPanel nextPiecePanel;
    private JButton pauseButton;
    private JPanel[][] caseNextPiece = new JPanel[4][4];
    private JLabel nbLigneLabel;
    private JButton quitterButton;

    public VueBandeauControle() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBackground(Color.gray);
        // setPreferredSize();

        // SCORE
        scoreLabel = new JLabel("SCORE : 0");
        scoreLabel.setForeground(Color.white);
        scoreLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        scoreLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        // NB LIGNE
        nbLigneLabel = new JLabel("LIGNES : 0");
        nbLigneLabel.setForeground(Color.white);
        nbLigneLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        nbLigneLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

        // NEXT PIECE
        nextPiecePanel = new JPanel();
        nextPiecePanel.setBackground(Color.LIGHT_GRAY);
        nextPiecePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
        initierNextPiecePanel();

        // PAUSE BUTTON & QUIT BUTTON
        pauseButton = new JButton("PAUSE");
        quitterButton = new JButton("QUITTER");
        Dimension buttonSize = new Dimension(85, 30);
        Insets margeBoutton = new Insets(2, 2, 2, 2);
        pauseButton.setPreferredSize(buttonSize);
        pauseButton.setMargin(margeBoutton);
        quitterButton.setPreferredSize(buttonSize);
        quitterButton.setMargin(margeBoutton);
        JPanel boutonsPanel = new JPanel();
        boutonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
        boutonsPanel.setOpaque(false);
        boutonsPanel.add(pauseButton);
        boutonsPanel.add(quitterButton);
        pauseButton.setFocusable(false);
        quitterButton.setFocusable(false);

        add(Box.createVerticalStrut(20));
        add(scoreLabel);
        add(Box.createVerticalStrut(20));
        add(nbLigneLabel);
        add(Box.createVerticalStrut(20));
        add(nextPiecePanel);
        add(Box.createVerticalStrut(20));
        add(boutonsPanel);

        // setVisible(true);
    }

    public void setScore(int score) {
        scoreLabel.setText("Score: " + score);
    }

    public JButton getPauseButton() {
        return pauseButton;
    }

    public JButton getQuitterButton() {
        return quitterButton;
    }

    public JPanel getNextPiecePanel() {
        return nextPiecePanel;
    }

    private void initierNextPiecePanel() {
        nextPiecePanel.removeAll();
        nextPiecePanel.setLayout(new GridLayoutCarre(4, 4));
        Dimension tailleCarre = new Dimension(120, 120);
        nextPiecePanel.setPreferredSize(tailleCarre);
        nextPiecePanel.setMaximumSize(tailleCarre);
        nextPiecePanel.setMinimumSize(tailleCarre);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                JPanel caseG = new JPanel();
                caseG.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                caseG.setBackground(Color.WHITE);
                caseNextPiece[i][j] = caseG;
                nextPiecePanel.add(caseG);
            }
        }
        nextPiecePanel.revalidate();
        nextPiecePanel.repaint();
    }

    public void afficherPieceSuivante(PieceCourante piece) {
        boolean[][] motif = piece.getMotif();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (motif[i][j]) {
                    caseNextPiece[i][j].setBackground(Color.RED);
                } else {
                    caseNextPiece[i][j].setBackground(Color.WHITE);
                }
            }
        }
        nextPiecePanel.revalidate();
        nextPiecePanel.repaint();
    }


}