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

public class MainFrame extends JFrame {

    private CardLayout cardLayout;
    private JPanel mainPanel;

    // ---------- //
    // Diverse Hilfsvariablen (für Parameterübergabe etc.)
    // Von startMultiplayerGame an JoinGame
    int localMult;

    // Von startLocalGame an startLocalGameLoadingScreen
    // Von startLocalGameLoadingScreen an GameBoard
    int semesterCounter;
    // ---------- //


    public MainFrame() {

        GameController.setMainFrame(this);

        setTitle("Studium Versenken");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1500, 1000);
        setLocationRelativeTo(null);

        //JLabel backgroundLabel = new JLabel(new ImageIcon("graphics/mainmenubackground.png"));
       // backgroundLabel.setBounds(0, 0, 1500, 1000);
      //  getContentPane().add(backgroundLabel);

       // backgroundLabel.setOpaque(true);

        // CardLayout und Hauptpanel erstellen
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

        // Verschiedene Panels erstellen und hinzufügen
        MainMenuView mainMenuView = new MainMenuView(this);
        startLocalGame localGame = new startLocalGame(this);
        //startLocalGameLoadingScreen LocalGameLoadingScreen = new startLocalGameLoadingScreen(this);
        startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
        coinToss coinToss = new coinToss(this);
        Verbinden verbinden = new Verbinden(this);
        //JoinGame joinGame = new JoinGame(this,localMult);
        //GameBoard gameBoard = new GameBoard(this, localMult);

        mainPanel.add(mainMenuView, "MainMenu");
        mainPanel.add(localGame, "LocalGame");
        mainPanel.add(multiplayerGame, "MultiplayerGame");
        mainPanel.add(coinToss, "coinToss");
        mainPanel.add(verbinden, "Verbinden");
        //mainPanel.add(joinGame, "JoinGame");
        //mainPanel.add(gameBoard, "GameBoard");

        // Hauptpanel in JFrame hinzufügen
        add(mainPanel);

        // Hauptmenü anzeigen
        cardLayout.show(mainPanel, "MainMenu");
    }

    // Methode, um die Ansicht zu wechseln
    public void showPanel(String panelName) {
        cardLayout.show(mainPanel, panelName);
    }

    // --- ShowPanel der startMultiplayerGame Klasse
    public void showPanelSMG(String panelName, int num, int playerType,String playerName) {
        this.localMult = num;

        //if (!isPanelPresent(panelName)) { //TODO potentiell raus
            JoinGame joinGame = new JoinGame(this, localMult, playerType, playerName);
            mainPanel.add(joinGame, panelName);
            mainPanel.revalidate(); // Refresh
            mainPanel.repaint();
        //}

        cardLayout.show(mainPanel, panelName); // Show the panel
    }
    // --- ShowPanel der startLocalGameLoadingScreen Klasse (DURCH BACKEND AUFGERUFEN)
    public void showPanelSLG(String panelName,int semesterCounter, Player p1, Player p2) {
            this.semesterCounter = semesterCounter;

            //if (!isPanelPresent(panelName)) { //TODO potentiell raus
            // gameBoard muss player übergeben bekommen
            GameBoard gameBoard = new GameBoard(this, semesterCounter, p1, p2);
            //mainPanel.add(mainMenuView, "MainMenu");
            mainPanel.add(gameBoard, panelName);
            mainPanel.revalidate(); // Refresh
            mainPanel.repaint();
        //}

        cardLayout.show(mainPanel, panelName); // Show the panel
    }

    // --- ShowPanel der startLocalGame Klasse
    public void showPanelSLGLS(String panelName,int semesterCounter) {
        this.semesterCounter = semesterCounter;
        // gameBoard muss player übergeben bekommen
        startLocalGameLoadingScreen LocalGameLoadingScreen = new startLocalGameLoadingScreen(this, semesterCounter);
        mainPanel.add(LocalGameLoadingScreen, panelName);
        mainPanel.revalidate(); // Refresh
        mainPanel.repaint();
        //}

        cardLayout.show(mainPanel, panelName); // Show the panel
    }

    /* TODO ist dies unnötig?
    private boolean isPanelPresent(String panelName) {
        for (Component component : mainPanel.getComponents()) {
            if (panelName.equals(mainPanel.getClientProperty("name"))) {
                return true;
            }
        }
        return false;
    } */

    // Methoden für übergabe von playern an GameBoard
    /*
    public void setPlayerData() {
        this.semesterCounter = semesterCounter;

        //if (!isPanelPresent(panelName)) { //TODO potentiell raus
        // gameBoard muss player übergeben bekommen
        GameBoard gameBoard = new GameBoard(this, semesterCounter);
        mainPanel.add(gameBoard, panelName);
        mainPanel.revalidate(); // Refresh
        mainPanel.repaint();
    }*/

    /* Tut wohl gar nicht, weils nicht aufgerufen wird hoppla
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setVisible(true);
        });
    }
    */
}