package Test;

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

public class MainMenu extends JFrame {
    private final static Dimension MINIMUM_SIZE = new Dimension(960, 540);
    private CardLayout cards;

    private AdminView av;
    private StudentView sv;
    private ProfView pv;

    private BLayout settings;

    private final Controller listener;

    private JButton[] buttonTab;

    public MainMenu(Controller listener) {
        super();

        this.av = listener.getAdminView();
        this.pv = listener.getProfView();
        this.sv = listener.getStudentView();

        this.cards = new CardLayout();

        this.listener = listener;

        this.buttonTab = new JButton[] {
                new JButton("Admin"),
                new JButton("Prof"),
                new JButton("Student")
        };

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setExtendedState(MAXIMIZED_BOTH);
        this.setMinimumSize(MINIMUM_SIZE);
        this.init();
        this.setVisible(true);
    }


    private void init() {
        this.setLayout(this.cards);

        this.add(first());
        this.add(adminView());
        this.add(profView());
        this.add(studentView());

        this.cards.first(this.getContentPane());
    }


    private JPanel first() {
        JPanel mainPanel = new JPanel();
        JPanel centerPanel = new JPanel();
        Dimension buttonDimension = new Dimension(300, 50);

        mainPanel.setLayout(new BorderLayout());
        centerPanel.setLayout(new GridBagLayout());

        Insets insets = new Insets(10, 10, 10, 10);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = insets;
        gbc.gridwidth = 1;
        gbc.gridx     = 0;

        for (int i = 0; i < buttonTab.length; i++) {
            gbc.gridy = i;
            buttonTab[i].setPreferredSize(buttonDimension);
            buttonTab[i].addActionListener(this::action);
            centerPanel.add(buttonTab[i], gbc);
        }

        mainPanel.add(centerPanel, BorderLayout.CENTER);

        return mainPanel;
    }


    private JPanel adminView() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(this.av, this.settings);
        return mainPanel;
    }

    private JPanel profView() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(this.pv, this.settings);
        return mainPanel;
    }

    private JPanel studentView() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.add(this.sv, this.settings);
        return mainPanel;
    }

    private void action(ActionEvent e) {
        JButton origin = (JButton) e.getSource();
        System.out.println(origin.getText());

        if (origin.getText() == "Admin") {
            cards.next(this.getContentPane());

        } else if (origin.getText() == "Prof") {
            cards.next(this.getContentPane());
            cards.next(this.getContentPane());
        } else if (origin.getText() == "Student") {
            cards.next(this.getContentPane());
            cards.next(this.getContentPane());
            cards.next(this.getContentPane());
        }

        else {
            JOptionPane.showMessageDialog(null, "En travaux");
        }
    }
}
