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

public class startMultiplayerGame extends JPanel {
    // Funktionshilfen
    int semesterCounter = 1; // Semester Counter Label
    String PlayerNickname = "Spieler 1";

    // Grafiken
    ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
    ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
    ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png");

    // Labels
    JLabel frameTitle = new JLabel("Multiplayer Spiel");
    JLabel semesterLabel = new JLabel("Semester");
    JLabel PlayerName = new JLabel("Name");
    JLabel PlayerIcon = new JLabel(humanPlayerIcon);
    JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));

    // Buttons
    JButton backButton = new JButton(backButtonIcon);
    JButton PlayerLeftButton = new JButton("<-");
    JButton PlayerRightButton = new JButton("->");
    JButton semesterUpButton = new JButton("^");
    JButton semesterDownButton = new JButton("v");
    JButton createGameButton = new JButton("Spiel erstellen");
    JButton joinGameButton = new JButton("Spiel beitreten");

    // Textfelder
    JTextField PlayerTextField = new JTextField(20);

    // Konstruktor
    startMultiplayerGame(MainFrame frame) {
        setLayout(null);  // Setze das Layout für das Panel auf null

        // Setze Komponentenpositionen und füge sie dem Panel hinzu
        frameTitle.setBounds(20, 20, 200, 30);
        add(frameTitle);

        semesterLabel.setBounds(700, 300, 200, 30);
        add(semesterLabel);

        PlayerName.setBounds(50, 625, 200, 30);
        add(PlayerName);

        PlayerIcon.setBounds(75, 400, 200, 128);
        add(PlayerIcon);

        semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons
        semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(semesterCounterLabel);

        // Füge Buttons hinzu und setze ihre Positionen
        backButton.setBounds(1380, 20, 80, 80);
        add(backButton);

        PlayerLeftButton.setBounds(50, 450, 50, 50);
        add(PlayerLeftButton);

        PlayerRightButton.setBounds(250, 450, 50, 50);
        add(PlayerRightButton);

        semesterUpButton.setBounds(725, 400, 50, 50);
        add(semesterUpButton);

        semesterDownButton.setBounds(725, 550, 50, 50);
        add(semesterDownButton);

        joinGameButton.setBounds(1100, 350, 200, 50);
        add(joinGameButton);

        createGameButton.setBounds(1100, 550, 200, 50);
        add(createGameButton);

        // Füge das Textfeld hinzu
        PlayerTextField.setBounds(50, 650, 250, 50);
        PlayerTextField.setText(PlayerNickname);
        add(PlayerTextField);

        // ActionListener für Buttons
        // SEMESTERBUTTONS
        semesterUpButton.addActionListener(e -> {
            if (semesterCounter < 6) {
                semesterCounter++;
                semesterCounterLabel.setText(String.valueOf(semesterCounter));
            }
        });

        semesterDownButton.addActionListener(e -> {
            if (semesterCounter > 1) {
                semesterCounter--;
                semesterCounterLabel.setText(String.valueOf(semesterCounter));
            }
        });

        // PLAYERTOGGLEBUTTONS
        PlayerLeftButton.addActionListener(e -> {
            toggleLeftPlayerIcon();
            updateTextFields();
        });

        PlayerRightButton.addActionListener(e -> {
            toggleLeftPlayerIcon();
            updateTextFields();
        });

        // ActionListener für den "Back" Button, um zum vorherigen Panel zurückzukehren
        backButton.addActionListener(e -> frame.showPanel("MainMenu"));
    }

    // TOGGLE METHODEN
    private void toggleLeftPlayerIcon() {
        if (PlayerIcon.getIcon() == humanPlayerIcon) {
            PlayerIcon.setIcon(aiPlayerIcon);
        } else {
            PlayerIcon.setIcon(humanPlayerIcon);
        }
    }

    private void updateTextFields() {
        if (PlayerIcon.getIcon() == humanPlayerIcon) {
            PlayerTextField.setText(PlayerNickname);
        } else {
            PlayerTextField.setText("Leicht");
        }
    }
}
