import { world, system, ItemStack, BlockPermutation, MolangVariableMap } from "@minecraft/server";
import { decrementstack, adjustItemDamage } from "./bum_crops_custom_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, "cherry")
    door_flip(block, "lemon")
    door_flip(block, "maple")
    door_flip(block, "orange")
    door_flip(block, "peach")
    door_flip(block, "pear")

    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:cherry_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
            case "bumble:pear_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
            case "bumble:peach_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
            case "bumble:maple_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
            case "bumble:orange_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
            case "bumble:lemon_log":
                strip_log(item, block, inv, slot, 'dig.wood')
                break
            case "bumble:corn_block":
                strip_log(item, block, inv, slot, 'dig.grass')
                break
        }
    }
})

world.beforeEvents.playerInteractWithBlock.subscribe((eventData) => {
    const block = eventData.block
    const player = eventData.player
    const item = eventData.itemStack

    if (getTimeSinceLastClick(player) < 100) return;
    if (block.typeId == 'bumble:sap_tap_block' && block.permutation.getState('bumble:has_bucket') == false) {
        if (item?.typeId == "minecraft:bucket") {
            system.run(() => {
                decrementstack(item, player)
                block.dimension.playSound('mob.sniffer.plop', block.center())
                block.setPermutation(block.permutation.withState('bumble:has_bucket', true))
                block.setPermutation(block.permutation.withState('bumble:sap_level', 0))
            })
        }
    }
    if (block.typeId == 'minecraft:flower_pot') {
        switch (item?.typeId) {
            case 'bumble:chamomile_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_chamomile", "use.grass")
                })
                break;
            case 'bumble:lavender_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_lavender", "use.grass")
                })
                break;
            case 'bumble:mint_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_mint", "use.grass")
                })
                break;
            case 'bumble:aloe_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_aloe", "use.grass")
                })
                break;
            case 'bumble:briars':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_briar", "use.grass")
                })
                break;
            case 'bumble:snapdragons_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_snapdragons", "use.grass")
                })
                break;
            case 'bumble:small_rose_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_small_rose", "use.grass")
                })
                break;
            case 'bumble:gold_rose_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_gold_rose", "use.grass")
                })
                break;
            case 'bumble:dafodil_white_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_dafodil_white", "use.grass")
                })
                break;
            case 'bumble:dafodil_yellow_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_dafodil_yellow", "use.grass")
                })
                break;
            case 'bumble:hyacinth_blue_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_hyacinth_blue", "use.grass")
                })
                break;
            case 'bumble:hyacinth_white_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_hyacinth_white", "use.grass")
                })
                break;
            case 'bumble:hyacinth_pink_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_hyacinth_pink", "use.grass")
                })
                break;
            case 'bumble:hyacinth_purple_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_hyacinth_purple", "use.grass")
                })
                break;
            case 'bumble:iris_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_iris", "use.grass")
                })
                break;
            case 'bumble:lupines_pink_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_lupines_pink", "use.grass")
                })
                break;
            case 'bumble:lupines_blue_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_lupines_blue", "use.grass")
                })
                break;
            case 'bumble:mums_orange_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_mums_orange", "use.grass")
                })
                break;
            case 'bumble:mums_red_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_mums_red", "use.grass")
                })
                break;
            case 'bumble:mums_yellow_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_mums_yellow", "use.grass")
                })
                break;
            case 'bumble:mums_white_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_mums_white", "use.grass")
                })
                break;
            case 'bumble:morning_glory_pink_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_morning_glory_pink", "use.grass")
                })
                break;
            case 'bumble:pansies_purple_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_pansies_purple", "use.grass")
                })
                break;
            case 'bumble:small_cactus_item':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_small_cactus", "use.grass")
                })
                break;
            case 'bumble:cattail_reed':
                system.run(() => {
                    insert_flower_pot(item, player, block, "bumble:pot_cattails", "use.grass")
                })
                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))
    }
}



// courtesy of fatalconfuzion (discord)
function getTimeSinceLastClick(entity) {
    const entityTimeStamp = entity[Symbol.for('dateStamp')] ?? 0;
    entity[Symbol.for('dateStamp')] = Date.now();
    return entity[Symbol.for('dateStamp')] - entityTimeStamp;
}

export function insert_flower_pot(item, player, block, flower_pot_block, sound) {
    block.dimension.playSound(sound, block.center())
    block.setPermutation(BlockPermutation.resolve(flower_pot_block))
    decrementstack(item, player)
}

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))
}



