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

/**
 * Das startLocalGame Panel dient dem Erstellen eines lokalen Spiels.
 * Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
 * @author Lucas Bronson, Joshua Kuklok
 */
public class startLocalGame extends JPanel {
    // Player
    // TODO: entfernen (generell auch test button)
    Player p1;
    Player p2;

    // Funktionshilfen
    int semesterCounter = 1; // Semester Counter Label
    String leftPlayerNickname = "Spieler 1";
    String rightPlayerNickname = "Einfach";

    // Grafiken
    ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
    ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
    ImageIcon aiPlayerEasyIcon = new ImageIcon("graphics/botPlayerEasy.png");
    ImageIcon aiPlayerNormalIcon = new ImageIcon("graphics/botPlayerNormal.png");
    ImageIcon aiPlayerHardIcon = new ImageIcon("graphics/botPlayerHard.png");

    // Labels
    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(aiPlayerEasyIcon);
    JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));

    // Buttons
    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!");
    JButton testButton = new JButton("Test");

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

    /**
     * Konstruktor der startLocalGame.
     * Fügt Buttons, Textfelder und Label hinzu.
     * Fügt ebenfalls ActionListeners hinzu, damit Buttons etc. ihre gewünschte Funktion haben
     *
     * @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
     * @author Lucas Bronson, Joshua Kuklok
     */
    startLocalGame(MainFrame frame) {
        // Layout des Panels
        setLayout(null);

        // 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);

        testButton.setBounds(500,800,50,50);
        add(testButton);

        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.
        // Um das Semester zu erhöhen.
        semesterUpButton.addActionListener(e -> {
            if (semesterCounter < 6) {
                semesterCounter++;
                semesterCounterLabel.setText(String.valueOf(semesterCounter));
            }
        });

        // Um das Semester zu senken.
        semesterDownButton.addActionListener(e -> {
            if (semesterCounter > 1) {
                semesterCounter--;
                semesterCounterLabel.setText(String.valueOf(semesterCounter));
            }
        });

        // Um linken Player nach links zu "rotieren".
        leftPlayerLeftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleLeftPlayerIconLeft();
                updateTextFields();
            }
        });

        // Um linken Player nach rechts zu "rotieren".
        leftPlayerRightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleLeftPlayerIconRight();
                updateTextFields();
            }
        });

        // Um rechten Player nach links zu "rotieren".
        rightPlayerLeftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleRightPlayerIconLeft();
                updateTextFields();
            }
        });

        // Um den rechten Player nach rechts zu "rotieren".
        rightPlayerRightButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toggleRightPlayerIconRight();
                updateTextFields();
            }
        });

        // Um Namen des linken Spielers zu speichern.
        leftPlayerTextField.addActionListener(e -> {
            leftPlayerNickname = leftPlayerTextField.getText();
            System.out.println("Linker Spielername geändert zu: " + leftPlayerNickname); // Debugging-Ausgabe
        });

        // Um Namen des linken Spielers zu speichern, auch wenn nicht Enter gedrückt wird.
        leftPlayerTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusLost(java.awt.event.FocusEvent evt) {
                leftPlayerNickname = leftPlayerTextField.getText();
                System.out.println("Linker Spielername geändert zu: " + leftPlayerNickname); // Debugging-Ausgabe
            }
        });

        // Um zum MainMenu zu wechseln.
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.showPanel("MainMenu");
            }
        });

        // Um zum Gameboard zu wechseln.
        testButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //frame.showPanelWin("WinPanel");
            }
        });

        // Um zum startLocalGameLoadingScreen zu wechseln und Daten an Backend weiterzureichen.
        startButton.addActionListener(new ActionListener() {
            @SuppressWarnings("unchecked")
            @Override
            public void actionPerformed(ActionEvent e) {
                HashMap<ImageIcon, Class<? extends LocalPlayer>> playerClassMap = new HashMap<>();
                playerClassMap.put(humanPlayerIcon, HumanPlayer.class);
                playerClassMap.put(aiPlayerEasyIcon, SpecificAiPlayerEasy.class);
                playerClassMap.put(aiPlayerNormalIcon, SpecificAiPlayerMedium.class);
                playerClassMap.put(aiPlayerHardIcon, SpecificAiPlayerHard.class);


                frame.showPanelSLGLS("startLocalGameLoadingScreen", semesterCounter); //TODO
                
                Class<? extends LocalPlayer> leftPlayerClass = playerClassMap.get(leftPlayerIcon.getIcon());
                Class<? extends AiPlayer> rightPlayerClass = (Class<? extends AiPlayer>) playerClassMap.get(rightPlayerIcon.getIcon());
                GameController.startLocalGame(
                    leftPlayerClass, leftPlayerNickname,
                    rightPlayerClass,
                    GameController.semesterToBoardSize(semesterCounter)
                );

                // if (leftPlayerIcon.getIcon() == humanPlayerIcon) {// TODO Wird name wirklich weitergegeben?
                //     if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
                //         GameController.startLocalGame(HumanPlayer.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
                //         GameController.startLocalGame(HumanPlayer.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
                //         GameController.startLocalGame(HumanPlayer.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
                //     }
                // } else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon) {
                //     if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerEasy.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerEasy.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerEasy.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
                //     }
                // } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
                //     if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerMedium.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerMedium.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerMedium.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
                //     }
                // } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
                //     if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerHard.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerHard.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
                //     } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
                //         GameController.startLocalGame(SpecificAiPlayerHard.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
                //     }
                // }

            }
        });

    }
    // Hilfsfunktionen

    /**
     * Setzt das jeweils "nächste" Icon, wenn der leftPlayerLeftButton gedrückt wird.
     * @author Joshua Kuklok
     */
    private void toggleLeftPlayerIconLeft() {
        if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
            leftPlayerIcon.setIcon(aiPlayerHardIcon);
        } else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
            leftPlayerIcon.setIcon(humanPlayerIcon);
        } else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
            leftPlayerIcon.setIcon(aiPlayerEasyIcon);
        } else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
            leftPlayerIcon.setIcon(aiPlayerNormalIcon);
        }
    }

    /**
     * Setzt das jeweils "nächste" Icon, wenn der leftPlayerRightButton gedrückt wird.
     * @author Joshua Kuklok
     */
    private void toggleLeftPlayerIconRight() {
        if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
            leftPlayerIcon.setIcon(aiPlayerEasyIcon);
        } else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
            leftPlayerIcon.setIcon(aiPlayerNormalIcon);
        } else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
            leftPlayerIcon.setIcon(aiPlayerHardIcon);
        } else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
            leftPlayerIcon.setIcon(humanPlayerIcon);
        }
    }

    /**
     * Setzt das jeweils "nächste" Icon, wenn der RightPlayerLeftButton gedrückt wird.
     * @author Joshua Kuklok
     */
    private void toggleRightPlayerIconLeft() {
        if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
            rightPlayerIcon.setIcon(aiPlayerHardIcon);
        } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon){
            rightPlayerIcon.setIcon(aiPlayerEasyIcon);
        } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
            rightPlayerIcon.setIcon(aiPlayerNormalIcon);
        }
    }

    /**
     * Setzt das jeweils "nächste" Icon, wenn der RightPlayerRightButton gedrückt wird.
     * @author Joshua Kuklok
     */
    private void toggleRightPlayerIconRight() {
        if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
            rightPlayerIcon.setIcon(aiPlayerNormalIcon);
        } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon){
            rightPlayerIcon.setIcon(aiPlayerHardIcon);
        } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
            rightPlayerIcon.setIcon(aiPlayerEasyIcon);
        }
    }

    /**
     * Aktualisiert die Textfelder basierend auf den Icons
     * @author Joshua Kuklok
     */
    private void updateTextFields() {
        // Für Linken Spieler
        if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
            leftPlayerTextField.setText(leftPlayerNickname);
        } else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
            leftPlayerTextField.setText("Einfach");
        } else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
            leftPlayerTextField.setText("Mittel");
        } else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
            leftPlayerTextField.setText("Schwer");
        }

        // Für Rechten Spieler
        if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon){
            rightPlayerTextField.setText("Einfach");
        } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
            rightPlayerTextField.setText("Mittel");
        } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
            rightPlayerTextField.setText("Schwer");
        }
    }
}