import * as Minecraft from "@minecraft/server"

Minecraft.system.beforeEvents.startup.subscribe((event) => {
    event.blockComponentRegistry.registerCustomComponent("aria_ah:candle", {
        onPlayerInteract(event) {
            const equipment = event.player.getComponent(Minecraft.EntityComponentTypes.Equippable)
            const hand = equipment.getEquipment(Minecraft.EquipmentSlot.Mainhand)
            
            if (!event.block.permutation.getState("aria_ah:lit") && (hand?.typeId == "minecraft:flint_and_steel" || hand?.typeId == "minecraft:fire_charge")) {
                event.block.setPermutation(event.block.permutation.withState("aria_ah:lit", true))
                spawnFlames(event.block)

                if (event.player.getGameMode() != Minecraft.GameMode.Creative) {
                    const durability = hand.getComponent(Minecraft.ItemComponentTypes.Durability)
                    let shouldBreak
                    if (durability.damage != NaN) {
                        durability.damage++
                        shouldBreak = (durability.damage == durability.maxDurability)
                    } else {
                        const amount = hand.amount

                        if (amount > 1) {
                            amount--
                        } else {
                            shouldBreak = true
                        }
                    }
                    if (shouldBreak) {
                        equipment.setEquipment(Minecraft.EquipmentSlot.Mainhand, undefined)
                    } else {
                        equipment.setEquipment(Minecraft.EquipmentSlot.Mainhand, hand)
                    }
                }
            } else if (event.block.permutation.getState("aria_ah:lit")) {
                event.block.setPermutation(event.block.permutation.withState("aria_ah:lit", false))
            }
        },

        onTick(event) {
            spawnFlames(event.block)
        }
    })
})


/**
 * 
 * @param {Minecraft.Block} block 
 */
function spawnFlames(block) {
    if (block.permutation.getState("aria_ah:lit")) {
        const location = block.location
        
        if (block.typeId == "aria_ah:candle") {
            const rotation = block.permutation.getState("minecraft:cardinal_direction")
            const particleLocations = [
                { x: 0.5, y: 0.78125, z: 0.5 }, // Middle
                { x: 0.71875, y: 0.65625, z: 0.71875 }, // Top Left
                { x: 0.34375, y: 0.40625, z: 0.71875 }, // Top Right
                { x: 0.28125, y: 0.46875, z: 0.40625 }, // Bottom Right
                { x: 0.59375, y: 0.53125, z: 0.28125 }, // Bottom left
            ]

            for (const { x, y, z } of particleLocations) {
                if (rotation == "north") {
                    block.dimension.spawnParticle("minecraft:candle_flame_particle", { x: location.x + x, y: location.y + y, z: location.z + z })
                } else if (rotation == "east") {
                    block.dimension.spawnParticle("minecraft:candle_flame_particle", { x: location.x + 1 - z, y: location.y + y, z: location.z + x })
                } else if (rotation == "south") {
                    block.dimension.spawnParticle("minecraft:candle_flame_particle", { x: location.x + 1 - x, y: location.y + y, z: location.z + 1 - z })
                } else if (rotation == "west") {
                    block.dimension.spawnParticle("minecraft:candle_flame_particle", { x: location.x + z, y: location.y + y, z: location.z + 1 - x })
                }                    
            }
        } else if (block.typeId == "aria_ah:candlestick") {
            const particleLocations = [
                { x: 0.5, y: 1.5625, z: 0.5 }, // Middle
                { x: 0.5, y: 1.25, z: 0.8125 }, // Top
                { x: 0.8125, y: 1.375, z: 0.5 }, // Right
                { x: 0.5, y: 1.25, z: 0.1875 }, // Bottom
                { x: 0.1875, y: 1.375, z: 0.5 }, // Left
            ]

            for (const { x, y, z } of particleLocations) {
                block.dimension.spawnParticle("minecraft:candle_flame_particle", { x: location.x + x, y: location.y + y, z: location.z + z })             
            }
        }
    }
}
