package pp.monopoly.client.gui;

import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Checkbox;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.Slider;
import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.style.ElementId;

import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp;

public class SettingsMenu extends Dialog {
    private final MonopolyApp app;
    private final Container settingsContainer;
    private Geometry blockLayer;
    private final Node savedGuiNodeContent = new Node("SavedGuiNodeContent");

    public SettingsMenu(MonopolyApp app) {
        super(app.getDialogManager());
        this.app = app;

        // Blockierungsebene hinzufügen
        addBlockLayer();

        // Hintergrundbild
        addBackgroundImage();

        settingsContainer = new Container();

        // Hintergrundfarbe für das Container-Element setzen, um es undurchsichtig zu machen
        settingsContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.1f, 0.1f, 0.1f, 0.8f))); // Teiltransparent, falls gewünscht

        // Titel "Einstellungen"
        Label settingsTitle = settingsContainer.addChild(new Label("Einstellungen", new ElementId("settings-title")));
        settingsTitle.setFontSize(48);
        settingsTitle.setColor(ColorRGBA.White);

        // Effekt Sound mit Slider und Checkbox
        Container effectSoundContainer = settingsContainer.addChild(new Container());
        Label effectSoundLabel = effectSoundContainer.addChild(new Label("Effekt Sound", new ElementId("label")));
        effectSoundLabel.setFontSize(24);
        effectSoundLabel.setColor(ColorRGBA.White);

        Slider effectSoundSlider = effectSoundContainer.addChild(new Slider());
        effectSoundSlider.setPreferredSize(new com.jme3.math.Vector3f(300, 30, 0));

        Checkbox effectSoundCheckbox = effectSoundContainer.addChild(new Checkbox(""));
        effectSoundCheckbox.setChecked(true);

        // Hintergrund Musik mit Slider und Checkbox
        Container backgroundMusicContainer = settingsContainer.addChild(new Container());
        Label backgroundMusicLabel = backgroundMusicContainer.addChild(new Label("Hintergrund Musik", new ElementId("label")));
        backgroundMusicLabel.setFontSize(24);
        backgroundMusicLabel.setColor(ColorRGBA.White);

        Slider backgroundMusicSlider = backgroundMusicContainer.addChild(new Slider());
        backgroundMusicSlider.setPreferredSize(new com.jme3.math.Vector3f(300, 30, 0));

        Checkbox backgroundMusicCheckbox = backgroundMusicContainer.addChild(new Checkbox(""));
        backgroundMusicCheckbox.setChecked(true);

        // Beenden Button
        Button quitButton = settingsContainer.addChild(new Button("Beenden", new ElementId("menu-button")));
        quitButton.setFontSize(32);
        quitButton.setColor(ColorRGBA.White);
        quitButton.addClickCommands(source -> app.stop());

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

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

    private void addBlockLayer() {
        // Sichern des aktuellen GUI-Inhalts
        for (var child : app.getGuiNode().getChildren()) {
            savedGuiNodeContent.attachChild(child);
        }
        app.getGuiNode().detachAllChildren();

        // Blockierungsebene erstellen und hinzufügen
        blockLayer = new Geometry("BlockLayer", new Quad(app.getCamera().getWidth(), app.getCamera().getHeight()));
        blockLayer.setMaterial(createTransparentMaterial());
        blockLayer.setLocalTranslation(0, 0, 0); // Platzierung unterhalb des SettingsMenu
        app.getGuiNode().attachChild(blockLayer);
    }

    private com.jme3.material.Material createTransparentMaterial() {
        com.jme3.material.Material material = new com.jme3.material.Material(
            app.getAssetManager(),
            "Common/MatDefs/Misc/Unshaded.j3md"
        );
        material.setColor("Color", new ColorRGBA(0, 0, 0, 0.5f)); // Halbtransparent
        material.getAdditionalRenderState().setBlendMode(com.jme3.material.RenderState.BlendMode.Alpha);
        return material;
    }

    private void addBackgroundImage() {
        Texture backgroundImage = app.getAssetManager().loadTexture("Pictures/unibw-Bib2.png");
        Quad quad = new Quad(app.getCamera().getWidth(), app.getCamera().getHeight());
        Geometry background = new Geometry("Background", quad);
        com.jme3.material.Material backgroundMaterial = new com.jme3.material.Material(
            app.getAssetManager(),
            "Common/MatDefs/Misc/Unshaded.j3md"
        );
        backgroundMaterial.setTexture("ColorMap", backgroundImage);
        background.setMaterial(backgroundMaterial);
        background.setLocalTranslation(0, 0, -1); // Platzierung hinter dem SettingsMenu
        app.getGuiNode().attachChild(background);
    }

    @Override
    public void close() {
        // Entfernt das SettingsMenu und die Blockierungsebene
        app.getGuiNode().detachChild(settingsContainer);
        app.getGuiNode().detachChild(blockLayer);

        // Stellt die ursprüngliche GUI wieder her
        for (var child : savedGuiNodeContent.getChildren()) {
            app.getGuiNode().attachChild(child);
        }
        savedGuiNodeContent.detachAllChildren();

        app.setSettingsMenuOpen(false);
    }
}
