import * as mc from "@minecraft/server"
import * as ui from "@minecraft/server-ui"
import './difficulty'

const options = function (id, value = null) {
    if (value !== null) {
        mc.world.setDynamicProperty("ld_eb:" + id, value)
        return
    }
    return mc.world.getDynamicProperty("ld_eb:" + id) ?? true // Definindo o padrão como true
}

mc.world.beforeEvents.worldInitialize.subscribe(a => {
    a.itemComponentRegistry.registerCustomComponent(
        "ld_eb:settings_use",
        {
            onUse: (data) => {
                const player = data.source
                menus[0](player)
                !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
            }
        }
    )
    mc.world.getDynamicProperty(`ld_eb:efeitos_sonoros`) == undefined ? mc.world.setDynamicProperty(`ld_eb:efeitos_sonoros`, true) : null
    mc.world.getDynamicProperty(`ld_eb:camera`) == undefined ? mc.world.setDynamicProperty(`ld_eb:camera`, true) : null
    mc.world.getDynamicProperty(`ld_eb:animations`) == undefined ? mc.world.setDynamicProperty(`ld_eb:animations`, true) : null
    mc.world.getDynamicProperty(`ld_eb:passive_skills`) == undefined ? mc.world.setDynamicProperty(`ld_eb:passive_skills`, true) : null
    mc.world.getDynamicProperty(`ld_eb:elemental_table_border`) == undefined ? mc.world.setDynamicProperty(`ld_eb:elemental_table_border`, true) : null
})

const menus = [
    function (player) {
        menu(
            player,
            "Elements Bending Settings", // Alterado para inglês
            "Select an option below to change the settings.",
            [
                {
                    text: "Sound Effects", icon: "sound_effects", run: function () {
                        menus[1](player, "efeitos_sonoros")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
                {
                    text: "Difficulty", icon: "difficulty_settings", run: function () {
                        menus[3](player, "dificuldade")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
                {
                    text: "Leveling System", icon: "leveling_system", run: function () {
                        menus[4](player, "nivelamento")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
                {
                    text: "Camera Movements", icon: "cinematic_controls", run: function () {
                        menus[1](player, "camera")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
                {
                    text: "Additional Animations", icon: "additional_animations", run: function () {
                        menus[1](player, "animations")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
                {
                    text: "Passive Skills", icon: "passive_skills", run: function () {
                        menus[1](player, "passive_skills")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
                {
                    text: "Elemental Ward Border", icon: "elemental_ward_border", run: function () {
                        menus[1](player, "elemental_table_border")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        
                    }
                },
            ]
        )
    },
    function (player, id = "efeitos_sonoros") {
        menu(
            player,
            `ld_eb:${id}.title`,
            options(id) ? `ld_eb:${id}.body_1` : `ld_eb:${id}.body_2`,
            [
                {
                    text: options(id) ? `ld_eb:${id}.button_1_1` : `ld_eb:${id}.button_1_2`, icon: options(id) ? "on" : "off", run: function () {
                        if (options(id)) {
                            options(id, false)
                            mc.world.sendMessage({
                                translate: `ld_eb:${id}.result_1`,
                                with: [player.nameTag]
                            })
                            if (id === "elemental_table_border") {
                                mc.world.getAllPlayers().forEach(p => {
                                    p.dimension.getEntities({
                                        type: "ld_eb:elemental_table",
                                        location: p.location
                                    }).forEach(e => {
                                        e.setProperty("ld_eb:active", false)
                                        //console1-console.warn("setou em: " + e.typeId)
                                    })
                                })
                            }
                        } else {
                            mc.world.sendMessage({
                                translate: `ld_eb:${id}.result_2`,
                                with: [player.nameTag]
                            })
                            options(id, true)
                            if (id === "elemental_table_border") {
                                mc.world.getAllPlayers().forEach(p => {
                                    p.dimension.getEntities({
                                        type: "ld_eb:elemental_table",
                                        location: p.location
                                    }).forEach(e => {
                                        e.setProperty("ld_eb:active", true)
                                        //console1-console.warn("setou em: " + e.typeId)
                                    })
                                })
                            }
                        }
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.put")
                        menus[2](player, id)
                    }
                },
                {
                    text: `ld_eb:${id}.button_2`, icon: "", run: function () {
                        menus[0](player)
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
            ]
        )
    },
    function (player, id = "efeitos_sonoros") {
        menu(
            player,
            `ld_eb:${id}_confirm.title`,
            options(id) ? `ld_eb:${id}_confirm.body_1` : `ld_eb:${id}_confirm.body_2`,
            [
                {
                    text: `ld_eb:${id}_confirm.button_1`, icon: "", run: function () {
                        menus[0](player)
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
            ]
        )
    },
    function (player, id = "efeitos_sonoros") {
        let mode
        {
            switch (mc.world.getDifficulty()) {
                case `Easy`:
                    mode = 1
                    break;
                case `Normal`:
                    mode = 2
                    break;
                case `Hard`:
                    mode = 3
                    break;
            
                default:
                    mode = 2
                    break;
            }
        }
        menu(
            player,
            `ld_eb:${id}.title`,
            `ld_eb:${id}.body_${mode}`,
            [
                {
                    text: `ld_eb:${id}.button_1`, icon: "easy", run: function () {
                        mc.system.runTimeout(()=>{
                        menus[3](player, 'dificuldade')
                        },1*20)
                        player.runCommand("/difficulty easy")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        mc.world.sendMessage({
                            translate: `ld_eb:${id}.result_1`,
                            with: [player.nameTag]
                        })
                    }
                },
                {
                    text: `ld_eb:${id}.button_2`, icon: "normal", run: function () {
                        mc.system.runTimeout(()=>{
                        menus[3](player, 'dificuldade')
                        },1*20)
                        player.runCommand("/difficulty normal")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        mc.world.sendMessage({
                            translate: `ld_eb:${id}.result_2`,
                            with: [player.nameTag]
                        })
                    }
                },
                {
                    text: `ld_eb:${id}.button_3`, icon: "hard", run: function () {
                        mc.system.runTimeout(()=>{
                        menus[3](player, 'dificuldade')
                        },1*20)
                        player.runCommand("/difficulty hard")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        mc.world.sendMessage({
                            translate: `ld_eb:${id}.result_3`,
                            with: [player.nameTag]
                        })
                    }
                },
                {
                    text: `ld_eb:${id}.button_4`, icon: "", run: function () {
                        menus[0](player)
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
            ]
        )
    },
    function (player, id = "nivelamento") {
        const id2 = 'reset_level'
        if(mc.world.getDynamicProperty('ld_eb.:boost_time')<=0||mc.world.getDynamicProperty('ld_eb.:boost_time')==undefined){
        menu(
            player,
            `ld_eb:${id}.title`,
            `ld_eb:${id}.body_1`,
            [
                {
                    text: `ld_eb:${id}.button_1_1`, icon: '2xbooster', run: function () {
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.put")
                        menu(
                            player,
                            `ld_eb:level_boost.confirm.title`,
                            `ld_eb:level_boost.confirm.body`,
                            [
                                {
                                    text: `ld_eb:level_boost.confirm.button_1`, icon: '', run: function() {
                        const hasDiamond = getItemOnInventory(player, "minecraft:diamond");
                        if (hasDiamond?.amount>0) {
                                        player.playSound("note.pling", { pitch: 1.0, volume: 1.0 })
                                        player.runCommand("clear @s diamond 0 1")
                                        boostTime(300)
                            mc.world.sendMessage({
                                translate: `ld_eb:level_boost.done`,
                                with: [player.nameTag]
                            })
                            if (id === "elemental_table_border") {
                                mc.world.getAllPlayers().forEach(p => {
                                    p.dimension.getEntities({
                                        type: "ld_eb:elemental_table",
                                        location: p.location
                                    }).forEach(e => {
                                        e.setProperty("ld_eb:active", false)
                                        //console1-console.warn("setou em: " + e.typeId)
                                    })
                                })
                            }
                                        menus[0](player, 'nivelamento')
                        } else {
                                        player.playSound("note.bass", { pitch: 1.0, volume: 1.0 })
                            mc.world.sendMessage({
                                translate: `ld_eb:level_boost.fail`,
                                with: [player.nameTag]
                            })
                        }
                                    }
                                },
                                {
                                    text: `ld_eb:level_boost.confirm.button_2`, icon: '', run:function() {
                                        menus[4](player, 'nivelamento')
                                    }
                                }
                            ]
                        )
                    }
                },
                {
                    text: `ld_eb:${id2}.button_2_1`,icon: 'leveling_reset', run: function () {
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.put")
                        menu(
                            player,
                            `ld_eb:reset_level.confirm.title`,
                            `ld_eb:reset_level.confirm.body`,
                            [
                                {
                                    text: `ld_eb:reset_level.confirm.button_1`, icon: '', run: function() {
                        const hasDiamond = getItemOnInventory(player, "minecraft:diamond");
                        if (hasDiamond?.amount>0) {
                                        player.playSound("note.pling", { pitch: 1.0, volume: 1.0 })
                                        player.runCommand("clear @s diamond 0 1")
                            resetLevel(player)
                            mc.world.sendMessage({
                                translate: `ld_eb:reset_level.done`,
                                with: [player.nameTag]
                            })
                            if (id === "elemental_table_border") {
                                mc.world.getAllPlayers().forEach(p => {
                                    p.dimension.getEntities({
                                        type: "ld_eb:elemental_table",
                                        location: p.location
                                    }).forEach(e => {
                                        e.setProperty("ld_eb:active", false)
                                        //console1-console.warn("setou em: " + e.typeId)
                                    })
                                })
                            }
                                        menus[0](player, 'nivelamento')
                        } else {
                                        player.playSound("note.bass", { pitch: 1.0, volume: 1.0 })
                            mc.world.sendMessage({
                                translate: `ld_eb:reset_level.fail`,
                                with: [player.nameTag]
                            })
                        }
                                    }
                                },
                                {
                                    text: `ld_eb:reset_level.confirm.button_2`, icon: '', run:function() {
                                        menus[4](player, 'nivelamento')
                                    }
                                }
                            ]
                        )
                    }
                },
                {
                    text: `ld_eb:${id}.button_2`, icon: "", run: function () {
                        menus[0](player)
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
            ]
        )
        } else {
        menu(
            player,
            `ld_eb:${id}.title`,
            `ld_eb:${id}.body_1`,
            [
                {
                    text: `ld_eb:${id2}.button_2_1`,icon: 'leveling_reset', run: function () {
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.put")
                        menu(
                            player,
                            `ld_eb:reset_level.confirm.title`,
                            `ld_eb:reset_level.confirm.body`,
                            [
                                {
                                    text: `ld_eb:reset_level.confirm.button_1`, icon: '', run: function() {
                        const hasDiamond = getItemOnInventory(player, "minecraft:diamond");
                        if (hasDiamond?.amount>0) {
                                        player.playSound("note.pling", { pitch: 1.0, volume: 1.0 })
                                        player.runCommand("clear @s diamond 0 1")
                            resetLevel(player)
                            mc.world.sendMessage({
                                translate: `ld_eb:reset_level.done`,
                                with: [player.nameTag]
                            })
                            if (id === "elemental_table_border") {
                                mc.world.getAllPlayers().forEach(p => {
                                    p.dimension.getEntities({
                                        type: "ld_eb:elemental_table",
                                        location: p.location
                                    }).forEach(e => {
                                        e.setProperty("ld_eb:active", false)
                                        //console1-console.warn("setou em: " + e.typeId)
                                    })
                                })
                            }
                                        menus[0](player, 'nivelamento')
                        } else {
                                        player.playSound("note.bass", { pitch: 1.0, volume: 1.0 })
                            mc.world.sendMessage({
                                translate: `ld_eb:reset_level.fail`,
                                with: [player.nameTag]
                            })
                        }
                                    }
                                },
                                {
                                    text: `ld_eb:reset_level.confirm.button_2`, icon: '', run:function() {
                                        menus[4](player, 'nivelamento')
                                    }
                                }
                            ]
                        )
                    }
                },
                {
                    text: `ld_eb:${id}.button_2`, icon: "", run: function () {
                        menus[0](player)
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
            ]
        )
        }
    },
    function (player, id = "konfirmasi") {
        //console1-console.warn(`ld_eb:${id}.button_1`)
        menu(
            player,
            `ld_eb:${id}.title`,
            options(id) ? `ld_eb:${id}.body_1` : `ld_eb:${id}.body_2`,
            [
                {
                    text: `ld_eb:${id}.button_1`, icon: "easy", run: function () {
                        menus[3](player)
                        player.runCommand("/difficulty easy")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        mc.world.sendMessage({
                            translate: `ld_eb:${id}.result_1`,
                            with: [player.nameTag]
                        })
                    }
                },
                {
                    text: `ld_eb:${id}.button_2`, icon: "normal", run: function () {
                        menus[3](player)
                        player.runCommand("/difficulty normal")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        mc.world.sendMessage({
                            translate: `ld_eb:${id}.result_2`,
                            with: [player.nameTag]
                        })
                    }
                },
                {
                    text: `ld_eb:${id}.button_3`, icon: "hard", run: function () {
                        menus[3](player)
                        player.runCommand("/difficulty hard")
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                        mc.world.sendMessage({
                            translate: `ld_eb:${id}.result_3`,
                            with: [player.nameTag]
                        })
                    }
                },
                {
                    text: `ld_eb:${id}.button_4`, icon: "", run: function () {
                        menus[0](player)
                        !mc.world.getDynamicProperty("ld_eb:efeitos_sonoros") ? null : player.playSound("item.book.page_turn")
                    }
                },
            ]
        )
    },
]

function menu(player, title, body, buttons = []) {
    const UI = new ui.ActionFormData()
        .title(title)
        .body(body)
    buttons.forEach(b => {
        b.icon === "" ? UI.button(b.text) : UI.button(b.text, "textures/ld/eb/items/" + b.icon)
    })
    UI.show(player).then(r => {
        if (r.cancelationReason) return
        buttons[r.selection].run(player)
    })
}

function getItemOnInventory(player, item) {

    /** @type { mc.EntityInventoryComponent } */
    const inventory = player.getComponent("minecraft:inventory")
    for (let i = 0; i < inventory.container?.size; i++) {

        if (!inventory.container.getItem(i)) { continue }

        const itemI = inventory.container.getItem(i)
        
        if (itemI.typeId === item) {

            return itemI;

            
        }

    }
    return false



}

function boostTime(value = undefined) {

    value ? mc.world.setDynamicProperty("ld_eb.:boost_time", value): null

    const tickBoost = mc.system.runInterval(() => {

        // variable
        const time = mc.world.getDynamicProperty("ld_eb.:boost_time")
        if (time === 0) {

            mc.system.clearRun(tickBoost)
            mc.world.sendMessage({ translate: "settings.ld_eb.boost.end" })
            return


        } else if (time === 180) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.2" })


        } else if (time === 60) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.4" })


        } else if (time === 30) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.5" })


        } else if (time === 10) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.6" })


        } else if (time === 5) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.7" })


        } else if (time === 4) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.8" })


        } else if (time === 3) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.9" })


        } else if (time === 2) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.10" })


        } else if (time === 1) {

            mc.world.sendMessage({ translate: "settings.ld_eb.boost.11" })


        }
        mc.world.setDynamicProperty("ld_eb.:boost_time", time - 1)


    }, 1*20)



}
function resetLevel(player) {
    const wp = [
        "ld_eb:air_staff",
        "ld_eb:earth_glove",
        "ld_eb:electricity_staff",
        "ld_eb:fire_staff",
        "ld_eb:spirit_staff",
        "ld_eb:water_staff",
        "ld_eb:weapons.cetro_do_vazio.normal",
        "ld_eb:weapons.glaive_congelada.normal",
        "ld_eb:weapons.martelo_de_metal.normal",
        "ld_eb:weapons.punhos_derretidos.normal"
    ]
    const dfexp = 0
    const dflvl = 1
    const dfult = 0
    
    wp.forEach(weapon=>{
        player.setDynamicProperty(weapon + ".lvl", dflvl)
        player.setDynamicProperty(weapon + ".xp", dfexp)
        player.setDynamicProperty(weapon + ".xpm", 10)
        player.setDynamicProperty(weapon + ".ult", dfult)
    })
}
export function maxLevel(player) {
    const wp = [
        "ld_eb:air_staff",
        "ld_eb:earth_glove",
        "ld_eb:electricity_staff",
        "ld_eb:fire_staff",
        "ld_eb:spirit_staff",
        "ld_eb:water_staff",
        "ld_eb:weapons.cetro_do_vazio.normal",
        "ld_eb:weapons.glaive_congelada.normal",
        "ld_eb:weapons.martelo_de_metal.normal",
        "ld_eb:weapons.punhos_derretidos.normal"
    ]
    const dfexp = 0
    const dflvl = 10
    const dfult = 0
    
    wp.forEach(weapon=>{
        player.setDynamicProperty(weapon + ".lvl", dflvl)
        player.setDynamicProperty(weapon + ".xp", dfexp)
        player.setDynamicProperty(weapon + ".xpm", 0)
        player.setDynamicProperty(weapon + ".ult", dfult)
    })
}
mc.world.setDynamicProperty('ld_eb.:boost_time', 0)