import { system, MinecraftDimensionTypes, ItemStack, BlockPermutation } from '@minecraft/server';
//converts the dimensionID to a Y value
export function dimensionToHeight(dimension) {
    const heights = [
        {
            id: MinecraftDimensionTypes.overworld,
            maxHeight: 320
        },
        {
            id: MinecraftDimensionTypes.nether,
            maxHeight: 128
        },
        {
            id: MinecraftDimensionTypes.theEnd,
            maxHeight: 256
        }
    ];
    const data = heights.find((f) => f.id == dimension);
    if (data != undefined) {
        //return the Y value
        return data.maxHeight;
    }
    else
        return 0;
}
export function spawnItemAnywhere(item, location, dimension) {
    //spawn the item at y100
    const itemEntity = dimension.spawnItem(item, { x: location.x, y: 100, z: location.z });
    //tp the item to the specified location
    itemEntity.teleport(location);
    //return the itemEntity
    return itemEntity;
}
export class doorManager {
    //set the door tag
    static doorTag = "gs_cb:is_door";
    //interact with a door block
    static interactWithDoor(block) {
        const dim = block.dimension;
        const loc = block.location;
        //get the top half of the block
        const topHalf = block.permutation.getState("gs_cb:upper_block_bit");
        //get if the block is open or not
        const open = block.permutation.getState("gs_cb:open_bit");

        let doorPart = undefined;
        if (topHalf == false) {
            //gets the block above
            try {
                doorPart = block.above(1);
            }
            catch { }
        }
        else
            try {
                doorPart = block.below(1);
            }
            catch { } //gets the block below
        //checks if the doorPart is undefined
        if (doorPart != undefined) {
            const data = this.doors.find((f) => f.id == block.typeId);
            let bool = false;

            

            if (open == true) {
                //play the close sound and set the bool constant to false
                if (data != undefined && data.closeSound != undefined)
                    dim.playSound(data.closeSound.id, loc, { pitch: data.closeSound.pitch, volume: data.closeSound.volume });
                bool = false;
            }
            else {
                //play the open sound and set the bool constant to true
                if (data != undefined && data.openSound != undefined)
                    dim.playSound(data.openSound.id, loc, { pitch: data.openSound.pitch, volume: data.openSound.volume });
                bool = true;
            }
            const blocks = [block, doorPart];
            for (const door of blocks) {
                //sets the open state
                try {
                    door.setPermutation(door.permutation.withState("gs_cb:open_bit", bool));
                }
                catch { }
            }
        }
    }
    static breakDoor(blockID, block, topHalf) {
        //does this stuff a tick later
        system.runTimeout(() => {
            let doorPart = undefined;
            if (topHalf == false) {
                try {
                    doorPart = block.above(1);
                }
                catch { }
            }
            else
                try {
                    doorPart = block.below(1);
                }
                catch { }
            //sets the doorPart to air
            if (doorPart != undefined && doorPart.hasTag(this.doorTag))
                doorPart.setPermutation(BlockPermutation.resolve("minecraft:air"));
            //gets the door data
            const data = this.doors.find((f) => f.id == blockID);
            if (data == undefined)
                return;
            const item = new ItemStack(data.itemID, 1);
            const loc = block.location;
            spawnItemAnywhere(item, { x: loc.x + 0.5, y: loc.y + 0.5, z: loc.z + 0.5 }, block.dimension); //spawns the item
            block.setPermutation(BlockPermutation.resolve("minecraft:air")); //sets the main block to air
        });
    }
    static placeDoor(block, aboveBlock, permutationToPlace) {
        //does this stuff a tick later
        let reversed = false;
        const facing = permutationToPlace.getState("minecraft:cardinal_direction");
        switch (facing) {
            case "north":
                try {
                    const otherblock = block.west(1);
                    if (otherblock?.typeId.includes("door")) {
                        const otherfacing = otherblock.permutation.getState("minecraft:cardinal_direction");
                        if (otherfacing == facing) {
                            reversed = true;
                        }
                    }
                }
                catch { }
                break;
            case "south":
                try {
                    const otherblock = block.east(1);
                    if (otherblock?.typeId.includes("door")) {
                        const otherfacing = otherblock.permutation.getState("minecraft:cardinal_direction");
                        if (otherfacing == facing) {
                            reversed = true;
                        }
                    }
                }
                catch { }
                break;
            case "east":
                try {
                    const otherblock = block.north(1);
                    if (otherblock?.typeId.includes("door")) {
                        const otherfacing = otherblock.permutation.getState("minecraft:cardinal_direction");
                        if (otherfacing == facing) {
                            reversed = true;
                        }
                    }
                }
                catch { }
                break;
            case "west":
                try {
                    const otherblock = block.south(1);
                    if (otherblock?.typeId.includes("door")) {
                        const otherfacing = otherblock.permutation.getState("minecraft:cardinal_direction");
                        if (otherfacing == facing) {
                            reversed = true;
                        }
                    }
                }
                catch { }
                break;
        }
        system.runTimeout(() => {
            block.setPermutation(permutationToPlace.withState("gs_cb:reversed", reversed));
        });
        aboveBlock.setPermutation(BlockPermutation.resolve(permutationToPlace.type.id)); //sets aboveBlock to the main block typeId
        aboveBlock.setPermutation(aboveBlock.permutation.withState("gs_cb:upper_block_bit", true)); //sets the upper block bit state to true
        aboveBlock.setPermutation(aboveBlock.permutation
            .withState("minecraft:cardinal_direction", facing)
            .withState("gs_cb:reversed", reversed)); //sets the facing direction to the main blocks facing direction
    }
    static doors = [];
}
