// import javafx.scene.control.ToggleGroup;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.util.List;

public class GameBoard extends JPanel {
    // Funktionshilfen
    //int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel

    // Grafiken
    ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
    ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.png");
    ImageIcon gameBoardX = new ImageIcon("graphics/gameboardx.png");

    // Labels
    JLabel frameTitle = new JLabel("GameBoard");
    JLabel kontextText = new JLabel("Beispielhafter Kontext-Text");
    //kontextText.setFont(new Font("Roboto", Font.BOLD, 24)); //TODO setFont fixen

    JButton backButton = new JButton(backButtonIcon);
    // Eigene ModulButtons

    GameBoard(MainFrame frame, int semesterCounter,Player p1, Player p2) {
        JToggleButton leftPlayerModul1 = new JToggleButton("Modul 1"); //TODO: Dynamische Namen durch abgleich mit Semester
        JToggleButton leftPlayerModul2 = new JToggleButton("Modul 2");
        JToggleButton leftPlayerModul3 = new JToggleButton("Modul 3");
        JToggleButton leftPlayerModul4 = new JToggleButton("Modul 4");
        JToggleButton leftPlayerModul5 = new JToggleButton("Modul 5");
        JToggleButton leftPlayerModul6 = new JToggleButton("Modul 6");
        JToggleButton leftPlayerModul7 = new JToggleButton("Reset");

        JToggleButton[] leftPlayerButtons=new JToggleButton[]{
                leftPlayerModul1, leftPlayerModul2, leftPlayerModul3, leftPlayerModul4,
                leftPlayerModul5, leftPlayerModul6, leftPlayerModul7,
        };
        // Gegnerische ModulButtons
        JToggleButton rightPlayerModul1 = new JToggleButton("Modul 1");
        JToggleButton rightPlayerModul2 = new JToggleButton("Modul 2");
        JToggleButton rightPlayerModul3 = new JToggleButton("Modul 3");
        JToggleButton rightPlayerModul4 = new JToggleButton("Modul 4");
        JToggleButton rightPlayerModul5 = new JToggleButton("Modul 5");
        JToggleButton rightPlayerModul6 = new JToggleButton("Modul 6");
        JToggleButton rightPlayerModul7 = new JToggleButton("Bereit");

        JToggleButton[] rightlayerButtons=new JToggleButton[]{
                rightPlayerModul1, rightPlayerModul2, rightPlayerModul3, rightPlayerModul4,
                rightPlayerModul5, rightPlayerModul6, rightPlayerModul7,
        };

        buildPanel(frame, semesterCounter,p1,p2,leftPlayerButtons,rightlayerButtons);
        List<Ship> shipsP1 =p1.getBoard().getShips();
        updateButtonLabels(shipsP1,leftPlayerButtons);
        backButton.addActionListener(e -> frame.showPanel("MainMenu"));
    }

    private void updateButtonLabels(List<Ship> ships,JToggleButton[] buttons) {
        for(int i=0;i<buttons.length &&i<ships.size();i++ ) {
            Ship ship = ships.get(i);
            buttons[i].setText(ship.getName());
        }
    }

    public void buildPanel(MainFrame frame, int semesterCounter,Player p1,Player p2,JToggleButton[] leftButtons,JToggleButton[] rightButtons) {
        // Hauptlayout - BorderLayout für die Anordnung der Komponenten
        setLayout(new BorderLayout());

        // Panel für das Kontext-Text-Feld
        JPanel headerPanel = new JPanel();
        headerPanel.setLayout(new BorderLayout());
        headerPanel.add(kontextText, BorderLayout.WEST);
        headerPanel.add(backButton, BorderLayout.EAST);

        // Panel für die Buttons des linken Spielers (ganz links)
        JPanel leftButtonsPanel = new JPanel();
        leftButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
        leftButtonsPanel.add(leftButtons[0]);
        leftButtonsPanel.add(leftButtons[1]);
        leftButtonsPanel.add(leftButtons[2]);
        leftButtonsPanel.add(leftButtons[3]);
        leftButtonsPanel.add(leftButtons[4]);
        leftButtonsPanel.add(leftButtons[5]);
        leftButtonsPanel.add(leftButtons[6]);

        // Panel für die Buttons des rechten Spielers (ganz rechts)
        JPanel rightButtonsPanel = new JPanel();
        rightButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
        rightButtonsPanel.add(rightButtons[0]);
        rightButtonsPanel.add(rightButtons[1]);
        rightButtonsPanel.add(rightButtons[2]);
        rightButtonsPanel.add(rightButtons[3]);
        rightButtonsPanel.add(rightButtons[4]);
        rightButtonsPanel.add(rightButtons[5]);
        rightButtonsPanel.add(rightButtons[6]);

        // Spielfelder erstellen (eigenes und gegnerisches)
        int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds
        // Spielfelder werden in BoardDisplay erstellt
        //JPanel ownBoardPanel = new JPanel(new GridLayout(gridSize, gridSize));
        //JPanel opponentBoardPanel = new JPanel(new GridLayout(gridSize, gridSize));
        JPanel ownBoardPanel = new BoardDisplay(gridSize,p1);
        JPanel opponentBoardPanel = new BoardDisplay(gridSize, p2);

        //Buttons in eine Gruppe packen damit diese beim drücken eines anderen Buttons wieder entwählt werden
        ButtonGroup leftButtonGroup= new ButtonGroup();
        leftButtonGroup.add(leftButtons[0]);
        leftButtonGroup.add(leftButtons[1]);
        leftButtonGroup.add(leftButtons[2]);
        leftButtonGroup.add(leftButtons[3]);
        leftButtonGroup.add(leftButtons[4]);
        leftButtonGroup.add(leftButtons[5]);
        leftButtonGroup.add(leftButtons[6]);

        ButtonGroup rightButtonGroup= new ButtonGroup();
        rightButtonGroup.add(rightButtons[0]);
        rightButtonGroup.add(rightButtons[1]);
        rightButtonGroup.add(rightButtons[2]);
        rightButtonGroup.add(rightButtons[3]);
        rightButtonGroup.add(rightButtons[4]);
        rightButtonGroup.add(rightButtons[5]);
        rightButtonGroup.add(rightButtons[6]);

        // Panel für beide Spielfelder (nebeneinander in der Mitte)
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(1, 2, 20, 0)); // 2 Spielfelder nebeneinander, mit Abstand von 20 Pixeln
        centerPanel.add(ownBoardPanel);
        centerPanel.add(opponentBoardPanel);

        // Panels dem Hauptlayout hinzufügen
        add(leftButtonsPanel, BorderLayout.WEST);
        add(rightButtonsPanel, BorderLayout.EAST);
        add(headerPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);

    }
}
