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

public class startLocalGame extends JPanel {
    // Funktionshilfen
    int semesterCounter = 1; // Semester Counter Label
    String leftPlayerNickname = "Spieler 1";
    String rightPlayerNickname = "Spieler 2";

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

    // Labels und Buttons
    JLabel frameTitle = new JLabel("Lokales Spiel");
    JLabel semesterLabel = new JLabel("Semester");
    JLabel leftPlayerName = new JLabel("Name");
    JLabel rightPlayerName = new JLabel("KI-Level");
    JLabel leftPlayerIcon = new JLabel(humanPlayerIcon);
    JLabel rightPlayerIcon = new JLabel(aiPlayerIcon);
    JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));

    JButton backButton = new JButton(backButtonIcon);
    JButton leftPlayerLeftButton = new JButton("<-");
    JButton leftPlayerRightButton = new JButton("->");
    JButton semesterUpButton = new JButton("^");
    JButton semesterDownButton = new JButton("v");
    JButton rightPlayerLeftButton = new JButton("<-");
    JButton rightPlayerRightButton = new JButton("->");
    JButton startButton = new JButton("Start!");

    JTextField leftPlayerTextField = new JTextField(20);
    JTextField rightPlayerTextField = new JTextField(20);

    // Methode zur Erstellung des Panels
    startLocalGame(MainFrame frame) {
        setLayout(null);  // Stelle das Layout des Panels ein

        // Setze Komponentenpositionen
        frameTitle.setBounds(20, 20, 200, 30);
        add(frameTitle);

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

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

        rightPlayerName.setBounds(1200, 625, 200, 30);
        add(rightPlayerName);

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

        rightPlayerIcon.setBounds(1225, 400, 200, 128);
        add(rightPlayerIcon);

        semesterCounterLabel.setBounds(725, 475, 50, 50);
        semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(semesterCounterLabel);

        backButton.setBounds(1380, 20, 80, 80);
        add(backButton);

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

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

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

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

        rightPlayerLeftButton.setBounds(1200, 450, 50, 50);
        add(rightPlayerLeftButton);

        rightPlayerRightButton.setBounds(1400, 450, 50, 50);
        add(rightPlayerRightButton);

        startButton.setBounds(700, 750, 100, 50);
        add(startButton);

        leftPlayerTextField.setBounds(50, 650, 250, 50);
        leftPlayerTextField.setText(leftPlayerNickname);
        add(leftPlayerTextField);

        rightPlayerTextField.setBounds(1200, 650, 250, 50);
        rightPlayerTextField.setText(rightPlayerNickname);
        add(rightPlayerTextField);

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

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

        leftPlayerLeftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleLeftPlayerIcon();
                updateTextFields();
            }
        });

        leftPlayerRightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleLeftPlayerIcon();
                updateTextFields();
            }
        });

        rightPlayerLeftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleRightPlayerIcon();
                updateTextFields();
            }
        });

        rightPlayerRightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleRightPlayerIcon();
                updateTextFields();
            }
        });

        backButton.addActionListener(e -> frame.showPanel("MainMenu"));

        startButton.addActionListener(e -> frame.showPanel("coinToss")); // TODO ECHTE FUNKTION EINFÜGEN
    }

    private void toggleLeftPlayerIcon() {
        if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
            leftPlayerIcon.setIcon(aiPlayerIcon);
        } else {
            leftPlayerIcon.setIcon(humanPlayerIcon);
        }
    }

    private void toggleRightPlayerIcon() {
        if (rightPlayerIcon.getIcon() == humanPlayerIcon) {
            rightPlayerIcon.setIcon(aiPlayerIcon);
        } else {
            rightPlayerIcon.setIcon(humanPlayerIcon);
        }
    }

    // Methode zum Aktualisieren der Textfelder basierend auf den ausgewählten Icons
    private void updateTextFields() {
        // Linker Spieler
        if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
            leftPlayerTextField.setText("Spieler 1");
        } else {
            leftPlayerTextField.setText("Leicht");
        }

        // Rechter Spieler
        if (rightPlayerIcon.getIcon() == humanPlayerIcon) {
            rightPlayerTextField.setText("Spieler 2");
        } else {
            rightPlayerTextField.setText("Leicht");
        }
    }
}