import * as SERVER from '@minecraft/server';
import * as UI from '@minecraft/server-ui';

// Subscribe to events to run code when specific in game actions occur
let daList = []
const cantMove = [
    'minecraft:barrier',
    'minecraft:beacon',
    'minecraft:bedrock',
    'minecraft:calibrated_sculk_sensor',
    'minecraft:command_block.png',
    'minecraft:crying_obsidian',
    'minecraft:enchanting_table',
    'minecraft:end_portal',
    'minecraft:end_portal_frame',
    'minecraft:ender_chest',
    'minecraft:grindstone',
    'minecraft:jigsaw',
    'minecraft:jukebox',
    'minecraft:light_block',
    'minecraft:lodestone',
    'minecraft:mob_spawner',
    'minecraft:moving_piston',
    'minecraft:portal',
    'minecraft:obsidian',
    'minecraft:piston_arm_collision',
    'minecraft:sticky_piston_arm_collision',
    'minecraft:reinforced_deepslate',
    'minecraft:respawn_anchor',
    'minecraft:sculk_catalyst',
    'minecraft:sculk_sensor',
    'minecraft:sculk_shrieker',
    'minecraft:structure_block',
    'minecraft:structure_void',
    'minecraft:trial_spawner',
    'minecraft:vault',
    'minecraft:allow',
    'minecraft:deny',
    'minecraft:border',
    'minecraft:water',
    'minecraft:flowing_water',
    'minecraft:lava',
    'minecraft:flowing_lava'
]
SERVER.system.afterEvents.scriptEventReceive.subscribe((data) => {
    if (data.id === "vc:rotatateBlocks") {
        daList = []
        addBlockToRotate(data.sourceBlock.above(1), data.sourceBlock.above(1).typeId, "below")
        //console.log(JSON.stringify(daList))
        daList.forEach(dablock => {
            if (data.sourceBlock.permutation.getState("vc:direction") == "counterclockwise")
                rotateBlockAboutOrigin270(dablock, data.sourceBlock.location)
            else
                rotateBlockAboutOrigin90(dablock, data.sourceBlock.location)
        })
    }
})
function addBlockToRotate(block, source, last) {
    if (daList.length > 24) return;
    if (!daList.includes(block)) if (!block.isAir && !cantMove.includes(block.typeId)) daList.push(block)
    else return;
    if ((block.typeId == 'minecraft:slime' || block.typeId == 'minecraft:honey_block') && block.typeId == source) {
        if (last != "above") addBlockToRotate(block.above(1), source, "below")
        if (last != "below") addBlockToRotate(block.below(1), source, "above")
        if (last != "north") addBlockToRotate(block.north(1), source, "south")
        if (last != "east") addBlockToRotate(block.east(1), source, "west")
        if (last != "south") addBlockToRotate(block.south(1), source, "north")
        if (last != "west") addBlockToRotate(block.west(1), source, "east")
    }
}
function rotateBlockAboutOrigin90(block, origin) {
    var oldCoords = block.location
    var newCoords = {
        x: origin.x - (oldCoords.z - origin.z),
        y: oldCoords.y,
        z: origin.z + (oldCoords.x - origin.x)
    }
    var newBlock = block.dimension.getBlock(newCoords);
    //console.log(`${newCoords.x} ${newCoords.y} ${newCoords.z}`)
    if (newBlock.isAir) {
        block.dimension.runCommand(`structure save §k${newBlock.id} ${oldCoords.x} ${oldCoords.y} ${oldCoords.z} ${oldCoords.x} ${oldCoords.y} ${oldCoords.z} false`)
        block.dimension.runCommand(`structure load §k${newBlock.id} ${newCoords.x} ${newCoords.y} ${newCoords.z} 90_degrees`)
        block.dimension.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air`)
    }
}
function rotateBlockAboutOrigin270(block, origin) {
    var oldCoords = block.location
    var newCoords = {
        x: origin.x + (oldCoords.z - origin.z),
        y: oldCoords.y,
        z: origin.z - (oldCoords.x - origin.x)
    }
    var newBlock = block.dimension.getBlock(newCoords);
    //console.log(`${newCoords.x} ${newCoords.y} ${newCoords.z}`)
    if (newBlock.isAir) {
        block.dimension.runCommand(`structure save §k${newBlock.id} ${oldCoords.x} ${oldCoords.y} ${oldCoords.z} ${oldCoords.x} ${oldCoords.y} ${oldCoords.z} false`)
        block.dimension.runCommand(`structure load §k${newBlock.id} ${newCoords.x} ${newCoords.y} ${newCoords.z} 270_degrees`)
        block.dimension.runCommand(`structure delete §k${newBlock.id}`)
        block.setType('minecraft:air')
    }
}