import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";
import { decrementstack, adjustItemDamage } from "./corrupted_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, "corrupted")

    if (block.hasTag('lv_zo: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 "lv_zo:corrupted_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
        }
    }
})


function door_flip(block, type) {
    let door = "lv_zo:" + type + "_door_bottom"
    if (block.typeId == door && ((block.permutation.getState('minecraft:cardinal_direction') == 'south' && block.east(1).hasTag('lv_zo:door')) || (block.permutation.getState('minecraft:cardinal_direction') == 'east' && block.north(1).hasTag('lv_zo:door')) || (block.permutation.getState('minecraft:cardinal_direction') == 'north' && block.west(1).hasTag('lv_zo:door')) || (block.permutation.getState('minecraft:cardinal_direction') == 'west' && block.south(1).hasTag('lv_zo:door')))) {
        let direction = block.permutation.getState('minecraft:cardinal_direction')
        block.setPermutation(BlockPermutation.resolve("lv_zo:" + 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("lv_zo:stairs")) {
        update_stair(block.north(1))
    }
    if (block.south(1).hasTag("lv_zo:stairs")) {
        update_stair(block.south(1))
    }
    if (block.east(1).hasTag("lv_zo:stairs")) {
        update_stair(block.east(1))
    }
    if (block.west(1).hasTag("lv_zo:stairs")) {
        update_stair(block.west(1))
    }
}
function update_stair(block) {
    //Inner Corners
    if (block.north(1).hasTag("lv_zo:stairs_east") && !(block.north(1).hasTag("lv_zo:stairs_inner"))) {
        if (block.hasTag("lv_zo:stairs_north")) {
            block.setPermutation(block.permutation.withState('lv_zo:direction', 6))

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

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

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

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

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

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

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

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

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

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

        }
    }
    //Inner Corners

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

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

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

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

        }
    }
    if (block.east(1).hasTag("lv_zo:stairs_south")) {
        if (block.hasTag("lv_zo:stairs_west")) {
            block.setPermutation(block.permutation.withState('lv_zo: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))
}



