package pp.monopoly.client.gui;

import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture;
import com.simsilica.lemur.Axis;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.TextField;
import com.simsilica.lemur.component.SpringGridLayout;

import pp.monopoly.client.MonopolyApp;
import pp.monopoly.client.StartMenu;

/**
 * CreateGameMenu class represents the menu for creating a new game.
 */
public class CreateGameMenu {
    private final MonopolyApp app;
    private final Container menuContainer;
    private Geometry background;

    public CreateGameMenu(MonopolyApp app) {
        this.app = app;

        // Hintergrundbild laden und hinzufügen
        addBackgroundImage();

        // Hauptcontainer für das Menü
        menuContainer = new Container(new SpringGridLayout(Axis.Y, Axis.X));
        menuContainer.setPreferredSize(new Vector3f(600, 400, 0)); // Feste Größe des Containers

        // Titel
        Label title = menuContainer.addChild(new Label("Neues Spiel"));
        title.setFontSize(48);

        // Eingabefelder-Container
        Container inputContainer = menuContainer.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X)));
        inputContainer.setPreferredSize(new Vector3f(200, 150, 0)); // Eingabefelder nicht ganz so breit
        inputContainer.setLocalTranslation(20, 0, 0); // Abstand vom Rand

        inputContainer.addChild(new Label("Server-Adresse:"));
        TextField playerNameField = inputContainer.addChild(new TextField("localhost"));
        playerNameField.setPreferredWidth(400); // Breite des Textfelds

        inputContainer.addChild(new Label("Port:"));
        TextField serverAddressField = inputContainer.addChild(new TextField("42069"));
        serverAddressField.setPreferredWidth(400); // Breite des Textfelds

        // Button-Container
        Container buttonContainer = menuContainer.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y)));
        buttonContainer.setPreferredSize(new Vector3f(400, 50, 0));
        buttonContainer.setLocalTranslation(20, 0, 0); // Abstand vom Rand

        // "Abbrechen"-Button
        Button cancelButton = buttonContainer.addChild(new Button("Abbrechen"));
        cancelButton.setPreferredSize(new Vector3f(120, 40, 0));
        cancelButton.addClickCommands(source -> goBackToStartMenu());

        // "Spiel hosten"-Button
        Button hostButton = buttonContainer.addChild(new Button("Spiel hosten"));
        hostButton.setPreferredSize(new Vector3f(120, 40, 0));
        hostButton.addClickCommands(source -> {
        closeCreateGameMenu(); // Schließt das Menü
        app.startTestWorld(); // Startet die Testwelt in der Hauptanwendung
});


        // "Beitreten"-Button
        Button joinButton = buttonContainer.addChild(new Button("Beitreten"));
        joinButton.setPreferredSize(new Vector3f(120, 40, 0));
        // joinButton.addClickCommands(source -> joinGame()); // Placeholder for joining logic

        // Zentrierung des Containers
        menuContainer.setLocalTranslation(
            (app.getCamera().getWidth() - menuContainer.getPreferredSize().x) / 2,
            (app.getCamera().getHeight() + menuContainer.getPreferredSize().y) / 2,
            1  // Höhere Z-Ebene für den Vordergrund
        );

        app.getGuiNode().attachChild(menuContainer);
    }

    /**
     * Lädt das Hintergrundbild und fügt es als geometrische Ebene hinzu.
     */
    private void addBackgroundImage() {
        Texture backgroundImage = app.getAssetManager().loadTexture("Pictures/unibw-Bib2.png");
        Quad quad = new Quad(app.getCamera().getWidth(), app.getCamera().getHeight());
        background = new Geometry("Background", quad);
        Material backgroundMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        backgroundMaterial.setTexture("ColorMap", backgroundImage);
        background.setMaterial(backgroundMaterial);
        background.setLocalTranslation(0, 0, -1); // Hintergrundebene

        app.getGuiNode().attachChild(background);
    }

    /**
     * Geht zum Startmenü zurück, wenn "Abbrechen" angeklickt wird.
     */
    private void goBackToStartMenu() {
        app.getGuiNode().detachChild(menuContainer);
        app.getGuiNode().detachChild(background); // Entfernt das Hintergrundbild
        StartMenu.createStartMenu(app);
    }
    /*
     * Link zwischen createGame und TestWorld
     */
    
    private void startTestWorld() {
        // Entfernt das Menü
        app.getGuiNode().detachChild(menuContainer);
        app.getGuiNode().detachChild(background);
        
        // Startet die Testszene
        TestWorld.startTestWorld();
    }

    private void closeCreateGameMenu() {
        app.getGuiNode().detachChild(menuContainer); // Entfernt den Menü-Container
        app.getGuiNode().detachChild(background); // Entfernt das Hintergrundbild
    }
    

}
