import * as mc from "@minecraft/server";
import * as mcui from "@minecraft/server-ui";
import range from "../../../modules/arrayRange.js";
import registerSystem from "../../../register/registerSystem.js";

import standIndex from "../list/_standIndex_.js";


export default class settingsMenu extends registerSystem {
    constructor() {
        super();
        this.listenForEvent("minecraft:itemStartUse",(eventData) => this.menu_root(eventData));
        this.listenForEvent("minecraft:playerSpawn",(eventData) => {
            let { initialSpawn, player } = eventData;
            if (initialSpawn) {
                player?.addTag("initialUI");
            }
        });
        this.tick(() => {
            this.getPlayers({tags:["initialUI"]}).forEach(player => {
                this.menu_about(player)
                player?.removeTag("initialUI")
            })
        },0)
    }
    
    ui_using(player,value) {
        
        let type = [
            "remove",
            "add"
        ]
        this.runCommand(`tag @s ${type[value]} ui_using`, player)
        
        return null;
    }
    
    menu_root(eventData) {
        let { source, itemStack } = eventData;
        if (source.typeId === "minecraft:player") {
            let player = source;
            let item = itemStack;
            if (!player?.isSneaking && item.typeId == "jojo:stand_settings") {
                this.menu_op(player);
            }
        }
    }
    
    menu_about(player) {
        this.ui_using(player,1);
        
        let menu = this.ui();
        menu.layout(player, `
            <ui id="_stand_about_"/>
                <title text="ui.stand_settings.about"/>
                    <body text="undefined"/>
                        <button id="start" text="ui.stand_settings.about_image" icon="textures/ui/image"/>
                        <button id="start" text="ui.stand_settings.3.addon.golden_wind.description.start"/>
                        <button id="text1" text="ui.stand_settings.3.addon.golden_wind.description.1"/>
                        <button id="text2" text="ui.stand_settings.3.addon.golden_wind.description.2"/>
                        <button id="text3" text="ui.stand_settings.3.addon.golden_wind.description.3"/>
                        <button id="text4" text="ui.stand_settings.3.addon.golden_wind.description.4"/>
                        <button id="text5" text="ui.stand_settings.3.addon.golden_wind.description.5"/>
                        <button id="text6" text="ui.stand_settings.3.addon.golden_wind.description.6"/>
                        <button id="text7" text="ui.stand_settings.3.addon.golden_wind.description.7"/>
                        <button id="end" text="ui.stand_settings.3.addon.golden_wind.description.end"/>
                        <button id="close" text="ui.stand_settings.3"/>
                    </body>
                </title>
            </ui>
        `
        );
        menu.trigger("_stand_about_", e => {
            let canceled = e.canceled;
            this.ui_using(player,0);
        });
    }

    menu_op(player) {
        this.ui_using(player,1);
        let barrage_camera_shake = this.GlobalVar(player,"barrage_camera_shake");
        let shoulder_surfing = this.GlobalVar(player,"shoulder_surfing");
        let mobHealthToggle = this.GlobalVar(player,"mobHealthToggle");
        let debugToggle = this.GlobalVar(player,"debugToggle");
        let ui_theme = this.GlobalVar(player,"statusBarUI_theme");
        let stand_fp_offset = this.GlobalVar(player,"stand_fp_offset");
        let mental_infinite = this.GlobalVar(mc.world,"mental.is_infinite");
        let mental_recovery = this.GlobalVar(mc.world,"mental.can_recovery");
        let stand_range_limit = this.GlobalVar(mc.world,"stand_range_limit");
        let clear_tnts = this.GlobalVar(mc.world,"clear_tnts");
        let clear_items = this.GlobalVar(mc.world,"clear_items");
        
        let form = new mcui.ModalFormData()
        form.title("ui.stand_settings.homepage")
        
        form.toggle("ui.stand_settings.1.text", false)
        form.toggle("ui.stand_settings.1", false)
        form.toggle("ui.stand_settings.1.rule.person", false)
        form.toggle("ui.stand_settings.0.toggle.0", barrage_camera_shake.var??=true)
        form.toggle("ui.stand_settings.0.toggle.0a", shoulder_surfing.var??=false)
        form.toggle("ui.stand_settings.0.toggle.0b", mobHealthToggle.var??=false)
        form.toggle("ui.stand_settings.0.toggle.0c", debugToggle.var??=false)
        form.toggle("ui.stand_settings.1.slider_text1", false)
        form.slider(" value", 0, 35, 1, stand_fp_offset.var??=0)
        form.toggle("ui.stand_settings.1.slider_text3", false)
        form.slider(" value", 1, 3, 1, ui_theme.var??=0)
        if (player?.isOp()) {
            form.toggle("ui.stand_settings.1.rule.world", false)
            form.toggle("ui.stand_settings.0.toggle.1", mental_infinite.var??=false)
            form.toggle("ui.stand_settings.0.toggle.2", mental_recovery.var??=false)
            form.toggle("ui.stand_settings.1.slider_text2", false)
            form.slider(" value", 4, 8, 1, stand_range_limit.var + 4)
        }
        // form.toggle("ui.stand_settings.1.rule.optimize", false)
        // form.toggle("ui.stand_settings.0.toggle.3", clear_tnts.var??=true)
        // form.toggle("ui.stand_settings.0.toggle.4", clear_items.var??=true)
        
        form.show(player).then(respond => {
            let [ text, description_button, person_rule, barrage_camera_shakex, shoulder_surfingx, mobHealthTogglex, debugTogglex, slider_text1, stand_fp_offsetx, slider_text3, ui_themex, world_rule, mental_boolean1, mental_boolean2, slider_text2, stand_range ] = respond.formValues;
            
            if (description_button) {
                this.menu_stand(player)
            }
            if (/*!about_button && */!description_button) {
                let chat_boolean = [
                    "ban",
                    "allow"
                ]
                barrage_camera_shake.var = barrage_camera_shakex;
                shoulder_surfing.var = shoulder_surfingx;
                mobHealthToggle.var = mobHealthTogglex;
                debugToggle.var = debugTogglex;
                if (!shoulder_surfing.var) {
                    this.runCommand(`camera @s clear`, player)
                }
                stand_fp_offset.var = stand_fp_offsetx;
                ui_theme.var = ui_themex;
                this.runCommand(`playanimation @s animation.stand.first_person_offset.variable.${stand_fp_offsetx} default 1`, player);
                
                // mental_infinite.var = mental_boolean1;
                // mental_recovery.var = mental_boolean2;
                
                // clear_tnts.var = clear_tntsx;
                // clear_items.var = clear_itemsx;
                
                // this.runCommand(`say hello world`, player)
                // console.error("hello world")
                player?.sendMessage({
                    rawtext: [
                        {
                            translate: `chat.stand_settings.saved_option`
                        }
                    ]
                })
                if (mental_boolean1 !== undefined && mental_boolean2 !== undefined && stand_range !== undefined) {
                    this.getPlayers().forEach(allPlayers => {
                        let mental_booleanA = [
                            null,
                            mental_infinite,
                            mental_recovery
                        ]
                        let mental_booleanB = [
                            null,
                            mental_boolean1,
                            mental_boolean2
                        ]
                        for (let v of range(1,2)) {
                            if (mental_booleanA[v].var !== mental_booleanB[v]) {
                                mental_booleanA[v].var = mental_booleanB[v];
                                allPlayers.sendMessage({
                                    rawtext: [
                                        {
                                            translate: `chat.stand_settings.world_rule.${chat_boolean[+mental_booleanB[v]]}`,
                                            with: [ player?.nameTag ]
                                        },
                                        {
                                            translate: `chat.stand_settings.0.toggle.${v}`
                                        }
                                    ]
                                })
                            }
                        }
                        if (stand_range_limit.var !== stand_range - 4) {
                            stand_range_limit.var = stand_range - 4;
                            allPlayers.sendMessage({
                                rawtext: [
                                    {
                                        translate: `chat.stand_settings.world_rule.stand_range_limit`,
                                        with: [ player?.nameTag, stand_range.toString() ]
                                    }
                                ]
                            })
                        }
                    })
                }
            }
        })
    }

    menu_stand(player) {
        this.ui_using(player,1);
        let tag = player?.getTags().join("|");
        if (!tag.includes("standMaster.")) {
            let menu = this.ui();
            menu.layout(player, `
                <ui id="_stand_description_"/>
                    <title text="ui.stand_settings.skills_description"/>
                        <body text="ui.how_to_use.no_stand"/>
                            <button id="close" text="ui.stand_settings.3"/>
                        </body>
                    </title>
                </ui>
            `
            );
            menu.trigger("_stand_description_", e => {
                let canceled = e.canceled;
                this.ui_using(player,0);
                
                let selection = e.getElementById;
                this.menu_op(player);
            });
        } 
        if (tag.includes("standMaster.")) {
            player?.getTags().filter(tags => tags.includes("standMaster.")).forEach(stand_tag => {
                standIndex[stand_tag.split(".")[1]].description(player,this);
            })
        }
    }
    
}

new settingsMenu();