import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";
import { decrementstack, adjustItemDamage } from "./template_components.js";

world.afterEvents.playerPlaceBlock.subscribe((eventData) => {
    const block = eventData.block
    const player = eventData.player
    const inv = player.getComponent("minecraft:inventory").container
    const slot = player.selectedSlotIndex
    const item = player.getComponent('equippable').getEquipment('Mainhand')

    //Door flips for types of block
    door_flip(block, "template")

    if (block.hasTag('bumble:stairs')) {
        update_stairs_around(block)
    }

})

world.afterEvents.itemUse.subscribe((eventData) => {
    const block = eventData.source.getBlockFromViewDirection()?.block
    const player = eventData.source
    const inv = player.getComponent("minecraft:inventory").container
    const slot = player.selectedSlotIndex
    const item = player.getComponent('equippable').getEquipment('Mainhand')

    //Could Condense this if we really wanted to, but opens possibility of a different strip interaction with logs
    if (item?.hasTag('minecraft:is_axe')) {
        switch (block.typeId) {
            case "bumble:template_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
        }
    }
})


function door_flip(block, type) {
    let door = "bumble:" + type + "_door_bottom"
    if (block.typeId == door && ((block.permutation.getState('minecraft:cardinal_direction') == 'south' && block.east(1).hasTag('bumble:door')) || (block.permutation.getState('minecraft:cardinal_direction') == 'east' && block.north(1).hasTag('bumble:door')) || (block.permutation.getState('minecraft:cardinal_direction') == 'north' && block.west(1).hasTag('bumble:door')) || (block.permutation.getState('minecraft:cardinal_direction') == 'west' && block.south(1).hasTag('bumble:door')))) {
        let direction = block.permutation.getState('minecraft:cardinal_direction')
        block.setPermutation(BlockPermutation.resolve("bumble:" + type + "_door_bottom_flipped"))
        block.setPermutation(block.permutation.withState('minecraft:cardinal_direction', direction))
    }
}


function update_stairs_around(block) {
    update_stair(block)
    if (block.north(1).hasTag("bumble:stairs")) {
        update_stair(block.north(1))
    }
    if (block.south(1).hasTag("bumble:stairs")) {
        update_stair(block.south(1))
    }
    if (block.east(1).hasTag("bumble:stairs")) {
        update_stair(block.east(1))
    }
    if (block.west(1).hasTag("bumble:stairs")) {
        update_stair(block.west(1))
    }
}
function update_stair(block) {
    //Inner Corners
    if (block.north(1).hasTag("bumble:stairs_east") && !(block.north(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_north")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 6))

        }
    }
    if (block.north(1).hasTag("bumble:stairs_west") && !(block.north(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_north")) {

            block.setPermutation(block.permutation.withState('bumble:direction', 7))
        }
    }
    if (block.east(1).hasTag("bumble:stairs_south") && !(block.north(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_east")) {

            block.setPermutation(block.permutation.withState('bumble:direction', 4))
        }
    }

    if (block.east(1).hasTag("bumble:stairs_south") && !(block.east(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_east")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 4))

        }
    }
    if (block.east(1).hasTag("bumble:stairs_north") && !(block.east(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_east")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 6))

        }
    }
    if (block.west(1).hasTag("bumble:stairs_north") && !(block.west(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_west")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 7))

        }
    }
    if (block.west(1).hasTag("bumble:stairs_south") && !(block.west(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_west")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 5))

        }
    }
    if (block.south(1).hasTag("bumble:stairs_west") && !(block.south(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_south")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 5))

        }
    }
    if (block.south(1).hasTag("bumble:stairs_east") && !(block.south(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_south")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 4))

        }
    }
    //Extra
    if (block.south(1).hasTag("bumble:stairs_east") && !(block.south(1).hasTag("bumble:stairs_inner"))) {
        if (block.hasTag("bumble:stairs_south")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 4))

        }
    }
    //Inner Corners

    if (block.north(1).hasTag("bumble:stairs_east")) {
        if (block.hasTag("bumble:stairs_south")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 8))

        }
    }
    if (block.south(1).hasTag("bumble:stairs_east")) {
        if (block.hasTag("bumble:stairs_north")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 11))

        }
    }
    if (block.west(1).hasTag("bumble:stairs_north")) {
        if (block.hasTag("bumble:stairs_east")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 11))

        }
    }
    if (block.south(1).hasTag("bumble:stairs_west")) {
        if (block.hasTag("bumble:stairs_north")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 10))

        }
    }
    if (block.east(1).hasTag("bumble:stairs_south")) {
        if (block.hasTag("bumble:stairs_west")) {
            block.setPermutation(block.permutation.withState('bumble:direction', 9))

        }
    }
}

function strip_log(item, block, inv, slot, sound) {
    adjustItemDamage(inv, item, 1, slot)
    block.dimension.playSound(sound, block.center())
    let face = block.permutation.getState('minecraft:block_face')
    block.setPermutation(BlockPermutation.resolve(block.typeId + '_stripped'))
    block.setPermutation(block.permutation.withState('minecraft:block_face', face))
}



