import { system, EntityComponentTypes, ItemStack } from "@minecraft/server"
import { sendDebugMessage } from "./JM_Utils"

const hiddenBlocks = {
    axe: "armor_axe",
    captain: "armor_captain",
    commander: "armor_commander",
    general: "armor_general",
    horseman: "armor_horseman",
    lance: "armor_lance",
    mage: "armor_mage",
    stand: "armor_stand",
    sword: "armor_sword",
    diamond_armor: "diamond_armor",
    float_heart: "floating_heart",
    gold_armor: "gold_armor",
    heart: "heart",
    iron_armor: "iron_armor",
    leather_armor: "leather_armor",
    good_horse: "armor_horse_good",
    bad_horse: "armor_horse_bad"
}

Object.keys(hiddenBlocks).forEach(key => hiddenBlocks[key] = `lb_as:${hiddenBlocks[key]}_block`)

system.afterEvents.scriptEventReceive.subscribe(scriptEvent => {
    const [namespace, root] = scriptEvent.id.split(":")
    if (namespace !== "lb_as") return
    switch(root) {
        case "give":
            const player = scriptEvent.sourceEntity
            if (!player.hasTag("lifeboat:debug")) {
                player.sendMessage(`not allowed`)
                return
            }
            const inventory = player.getComponent(EntityComponentTypes.Inventory).container
            for (const key in hiddenBlocks) {
                inventory.addItem(new ItemStack(hiddenBlocks[key], 1))
            }
            break
        case "horse_pose":
            // this event should only ever be run by horses switching their pose
            const horse = scriptEvent.sourceEntity
            if (!horse || horse.typeId !== "lb_as:armor_horse") return
            const poseId = horse.getProperty("lb_as:pose")
            try {
                horse.setProperty("lb_as:pose", poseId + 1)
            } catch (error) {
                horse.setProperty("lb_as:pose", 0)                
            }
            break
        default:
            sendDebugMessage(`unknown scriptevent: ${root}`)
    }
})