import { EntityInitializationCause, system, world } from "@minecraft/server";
import EntityManager from "./EntityManager";
import LaserBeam from "./entity/LaserBeam";
import FSWorldHelper from "./fs/FSWorldHelper";
import FSDynamicProperty from "./fs/libs/FSDynamicProperty";
import FSUtil from "./fs/FSUtil";
import FSLocalizedString from "./fs/FSLocalizedString";
import { FSCompassDirection } from "./fs/FSCompassDirection";
import FSEntityHelper from "./fs/FSEntityHelper";
import FSSoundManager from "./fs/FSSoundManager";
import { LASER_CONSOLE_DISTANCE } from "./fs/FSConstants";
export default class EntranceManager {
    constructor() { }
    static sharedInstance() {
        if (!EntranceManager.instance) {
            EntranceManager.instance = new EntranceManager();
        }
        return EntranceManager.instance;
    }
    init() { }
    initEntranceEntity(entity, spawnCause) {
        if (entity.typeId === "57d_sbunk:entrance_bed" && spawnCause === EntityInitializationCause.Spawned) {
            const compass_direction = FSEntityHelper.getCompassDirection(entity);
            let event = "fs:facing_south";
            switch (compass_direction) {
                case FSCompassDirection.South:
                    event = "fs:set_facing_south";
                    break;
                case FSCompassDirection.West:
                    event = "fs:set_facing_west";
                    break;
                case FSCompassDirection.North:
                    event = "fs:set_facing_north";
                    break;
                case FSCompassDirection.East:
                    event = "fs:set_facing_east";
                    break;
                default:
                    break;
            }
            entity.triggerEvent(event);
        }
        if (entity.typeId === "57d_sbunk:elevator_placeable") {
            FSEntityHelper.snapRotation(entity);
            let valid_placement = true;
            let x = entity.location.x - 1;
            let y = entity.location.y;
            let z = entity.location.z - 1;
            const width = 3;
            const height = 3;
            const depth = 3;
            const dimension = entity.dimension;
            for (let i = 0; i < width; i++) {
                for (let j = 0; j < height; j++) {
                    for (let k = 0; k < depth; k++) {
                        const location = { x: x + i, y: y + j, z: z + k };
                        const block = dimension.getBlock(location);
                        if (block && !block.permutation.matches("minecraft:air")) {
                            valid_placement = false;
                            break;
                        }
                    }
                }
            }
            if (!valid_placement) {
                entity.triggerEvent("fs:pop_to_item");
                world.playSound("random.pop", entity.location);
                world.playSound("fs.sbunk.chat_ping", entity.location);
                const message = {
                    rawtext: [
                        { text: "§l§c" },
                        {
                            translate: FSLocalizedString.localize("bunker.elevator_placeable.invalid_placement.title", "Invalid Placement!", "Invalid placement title.")
                        },
                        { text: "§r " },
                        {
                            translate: FSLocalizedString.localize("bunker.elevator_placeable.invalid_placement.message", "Make sure there is a 3x3x3 block area clear when placing elevators!", "Tip/instructions for placing elevators.")
                        }
                    ]
                };
                world.sendMessage(message);
                system.runTimeout(() => {
                    entity.remove();
                }, 1);
            }
            else {
                EntranceManager.sharedInstance().elevatorClosed(entity, true, true);
            }
        }
        if (entity.typeId === "57d_sbunk:entrance_bed" || entity.typeId === "57d_sbunk:entrance_chest" || entity.typeId === "57d_sbunk:entrance_tree") {
            let location = entity.location;
            const compass_direction = FSEntityHelper.getCompassDirection(entity);
            let facing_direction = 2;
            switch (compass_direction) {
                case FSCompassDirection.South:
                    facing_direction = 3;
                    break;
                case FSCompassDirection.West:
                    facing_direction = 4;
                    break;
                case FSCompassDirection.North:
                    facing_direction = 2;
                    break;
                case FSCompassDirection.East:
                    facing_direction = 5;
                    break;
                default:
                    break;
            }
            if (entity.typeId === "57d_sbunk:entrance_bed") {
                location = EntityManager.getLocationOffsetByRotation(entity, 0, 0, -1);
            }
            const command = `fill  ${location.x} ${location.y - 1} ${location.z} ${location.x} ${location.y - 3} ${location.z} ladder ["facing_direction":${facing_direction}] destroy`;
            entity.dimension.runCommand(command);
        }
        this.entranceClosed(entity);
    }
    static isEntranceEntity(entity) {
        return entity.typeId.indexOf("57d_sbunk:entrance_") > -1;
    }
    entranceInteraction(entity) {
        if (!EntranceManager.isEntranceEntity(entity)) {
            return;
        }
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            this.entranceClosed(entity);
        }
        else {
            this.entranceOpened(entity);
        }
    }
    entranceRemoved(entity) {
        if (!EntranceManager.isEntranceEntity(entity)) {
            return;
        }
        let block = "air";
        const dimension = entity.dimension;
        let location0 = entity.location;
        let location1 = entity.location;
        if (entity.typeId === "57d_sbunk:entrance_boulder") {
            location0 = entity.location;
            location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 1, 0);
        }
        if (entity.typeId === "57d_sbunk:entrance_tree") {
            location0 = entity.location;
            location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 5, 0);
        }
        if (entity.typeId === "57d_sbunk:entrance_bed") {
            location0 = entity.location;
            location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, -1);
        }
        if (entity.typeId === "57d_sbunk:entrance_chest") {
            location0 = entity.location;
            location1 = entity.location;
        }
        if (entity.typeId === "57d_sbunk:entrance_bunker") {
            location0 = EntityManager.getLocationOffsetByRotation(entity, 1, -1, -3);
            location1 = EntityManager.getLocationOffsetByRotation(entity, 2, -1, -3);
        }
        system.run(() => {
            FSWorldHelper.fillBlocks(dimension, location0, location1, block);
        });
    }
    entranceOpened(entity) {
        if (!EntranceManager.isEntranceEntity(entity)) {
            return;
        }
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            return;
        }
        EntityManager.playAnimation(entity, "open");
        FSDynamicProperty.setBoolean(entity, "is_open", true);
        const block = "air";
        if (entity.typeId === "57d_sbunk:entrance_boulder") {
            const location0 = entity.location;
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 1, 0);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            let wso = {
                volume: 1,
                pitch: 0.2
            };
            world.playSound("use.stone", entity.location, wso);
            world.playSound("use.stone", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_tree") {
            const location0 = entity.location;
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 5, 0);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            world.playSound("random.chestopen", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_bed") {
            const location0 = entity.location;
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, -1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            world.playSound("random.chestopen", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_chest") {
            const location0 = entity.location;
            const location1 = entity.location;
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            world.playSound("random.chestopen", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_bunker") {
            const location0 = EntityManager.getLocationOffsetByRotation(entity, 1, -1, -3);
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 2, -1, -3);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, "redstone_block");
            FSDynamicProperty.setBoolean(entity, "has_been_opened", true);
        }
        system.runTimeout(() => {
            this.entranceClosed(entity);
        }, 100);
    }
    entranceClosed(entity) {
        if (!entity.isValid()) {
            return;
        }
        if (!EntranceManager.isEntranceEntity(entity)) {
            return;
        }
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            EntityManager.playAnimation(entity, "close");
        }
        FSDynamicProperty.setBoolean(entity, "is_open", false);
        const block = "barrier";
        if (entity.typeId === "57d_sbunk:entrance_boulder") {
            const location0 = entity.location;
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 1, 0);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            let wso = {
                volume: 1,
                pitch: 0.2
            };
            world.playSound("use.stone", entity.location, wso);
            world.playSound("use.stone", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_tree") {
            const location0 = entity.location;
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 5, 0);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            world.playSound("random.chestclosed", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_bed") {
            const location0 = entity.location;
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, -1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            world.playSound("random.chestclosed", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_chest") {
            const location0 = entity.location;
            const location1 = entity.location;
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            world.playSound("random.chestclosed", entity.location);
        }
        if (entity.typeId === "57d_sbunk:entrance_bunker") {
            const location0 = EntityManager.getLocationOffsetByRotation(entity, 1, -1, -3);
            const location1 = EntityManager.getLocationOffsetByRotation(entity, 2, -1, -3);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, "andesite");
        }
    }
    consoleInteraction(entity, allowed) {
        world.playSound("random.pop", entity.location);
        EntityManager.playAnimation(entity, "open");
        system.runTimeout(() => {
            world.playSound("fs.sbunk.scanning_1s", entity.location);
        }, 7);
        system.runTimeout(() => {
            if (allowed) {
                if (entity.isValid()) {
                    EntranceManager.sharedInstance().consoleOpened(entity);
                }
            }
            else {
                EntityManager.playAnimation(entity, "deny");
                world.playSound("fs.sbunk.access_denied", entity.location);
            }
        }, 40);
    }
    consoleEnableNearbyLasers(entity, allowed) {
        world.playSound("random.pop", entity.location);
        EntityManager.playAnimation(entity, "open");
        system.runTimeout(() => {
            world.playSound("fs.sbunk.scanning_1s", entity.location);
        }, 7);
        system.runTimeout(() => {
            if (allowed) {
                EntranceManager.sharedInstance().consoleEnableDisable(entity, true);
            }
            else {
                world.playSound("fs.sbunk.access_denied", entity.location);
                EntityManager.playAnimation(entity, "deny");
            }
        }, 40);
    }
    consoleDisableNearbyLasers(entity, allowed) {
        world.playSound("random.pop", entity.location);
        EntityManager.playAnimation(entity, "open");
        system.runTimeout(() => {
            world.playSound("fs.sbunk.scanning_1s", entity.location);
        }, 7);
        system.runTimeout(() => {
            if (allowed) {
                EntranceManager.sharedInstance().consoleEnableDisable(entity, false);
            }
            else {
                world.playSound("fs.sbunk.access_denied", entity.location);
                EntityManager.playAnimation(entity, "deny");
            }
        }, 40);
    }
    consoleEnableDisable(entity, enable) {
        if (entity.typeId === "57d_sbunk:laser_console") {
            const eqo = {
                location: entity.location,
                maxDistance: LASER_CONSOLE_DISTANCE,
                families: ["57d_sbunk_laser"]
            };
            entity.dimension.getEntities(eqo).forEach(laser => {
                if (enable) {
                    LaserBeam.onEnable(laser);
                }
                else {
                    LaserBeam.onDisable(laser);
                }
            });
            let wso = {
                volume: 0.25,
                pitch: 1
            };
            world.playSound("fs.sbunk.access_granted", entity.location, wso);
            if (enable) {
                entity.dimension.spawnParticle("fs_sbunk:sbunk_laser_console_open_radius", entity.location);
            }
            else {
                entity.dimension.spawnParticle("fs_sbunk:sbunk_laser_console_close_radius", entity.location);
            }
        }
    }
    consoleOpened(entity) {
        try {
            if (entity.typeId === "57d_sbunk:laser_console") {
                const eqo = {
                    location: entity.location,
                    maxDistance: LASER_CONSOLE_DISTANCE,
                    families: ["57d_sbunk_laser"],
                    excludeTags: [FSUtil.namespacedStringCr("disabled")]
                };
                entity.dimension.getEntities(eqo).forEach(laser => {
                    LaserBeam.onOpen(laser);
                });
                let wso = {
                    volume: 0.25,
                    pitch: 1
                };
                world.playSound("fs.sbunk.access_granted", entity.location, wso);
                entity.dimension.spawnParticle("fs_sbunk:sbunk_laser_console_open_radius", entity.location);
            }
            system.runTimeout(() => {
                if (entity.isValid()) {
                    this.consoleClosed(entity);
                }
            }, 60);
        }
        catch (error) { }
    }
    consoleClosed(entity) {
        if (entity.typeId === "57d_sbunk:laser_console") {
            const eqo = {
                location: entity.location,
                maxDistance: LASER_CONSOLE_DISTANCE,
                families: ["57d_sbunk_laser"],
                excludeTags: [FSUtil.namespacedStringCr("disabled")]
            };
            entity.dimension.getEntities(eqo).forEach(laser => {
                LaserBeam.onClose(laser);
            });
            entity.dimension.spawnParticle("fs_sbunk:sbunk_laser_console_close_radius", entity.location);
        }
    }
    elevatorInteraction(entity) {
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            this.elevatorClosed(entity);
        }
        else {
            this.elevatorOpened(entity);
        }
    }
    elevatorOpened(entity) {
        if (entity.typeId !== "57d_sbunk:elevator" && entity.typeId !== "57d_sbunk:elevator_placeable") {
            return;
        }
        if (FSDynamicProperty.getBoolean(entity, "is_open")) {
            return;
        }
        EntityManager.playAnimation(entity, "open");
        FSDynamicProperty.setBoolean(entity, "is_open", true);
        world.playSound("fs.sbunk.elevator_open", entity.location);
        const block = "air";
        if (entity.typeId === "57d_sbunk:elevator") {
            let location0 = EntityManager.getLocationOffsetByRotation(entity, 3, 0, 0);
            let location1 = EntityManager.getLocationOffsetByRotation(entity, 3, 3, 1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, -2, 0, 0);
            location1 = EntityManager.getLocationOffsetByRotation(entity, -2, 3, 1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, -2);
            location1 = EntityManager.getLocationOffsetByRotation(entity, 1, 3, -2);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, 3);
            location1 = EntityManager.getLocationOffsetByRotation(entity, 1, 3, 3);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
        }
        else if (entity.typeId === "57d_sbunk:elevator_placeable") {
            let location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, 1);
            let location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, 1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, -1);
            location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, -1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, 0);
            location1 = EntityManager.getLocationOffsetByRotation(entity, 1, 2, 0);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, -1, 0, 0);
            location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, 0);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
        }
        if (entity.nameTag.length >= 1) {
            let entityName = FSUtil.stripFormatting(entity.nameTag);
            if (entityName.toLowerCase() === "elevator") {
                entityName = "Untitled Floor";
            }
            const player_radius = entity.typeId === "57d_sbunk:elevator" ? 2 : 1.5;
            let entryMessage = FSDynamicProperty.getString(entity, FSUtil.namespacedString("entry_message"));
            if (entryMessage.length >= 1) {
                entity.runCommand(`execute as @a[r=${player_radius}] run tellraw @s {"rawtext":[{"text":"§b§l[!]§r§b ${entryMessage}"}]}`);
                entity.runCommand(`execute as @a[r=${player_radius}] run playsound fs.sbunk.chat_ping @s`);
            }
            system.runTimeout(() => {
                const sound = FSDynamicProperty.getNumber(entity, FSUtil.namespacedString("sound"));
                FSSoundManager.playElevatorSound(entity, sound);
            }, 5);
            entity.runCommand(`execute as @a[r=${player_radius}] run title @s subtitle ${entityName}`);
            entity.runCommand(`execute as @a[r=${player_radius}] run title @s title §r`);
        }
        system.runTimeout(() => {
            if (entity.isValid()) {
                this.elevatorClosed(entity);
            }
        }, 100);
    }
    elevatorClosed(entity, force = false, init = false) {
        if (entity.typeId !== "57d_sbunk:elevator" && entity.typeId !== "57d_sbunk:elevator_placeable") {
            return;
        }
        if (!force && !FSDynamicProperty.getBoolean(entity, "is_open")) {
            return;
        }
        EntityManager.playAnimation(entity, "close");
        FSDynamicProperty.setBoolean(entity, "is_open", false);
        world.playSound("fs.sbunk.elevator_close", entity.location);
        const block = "barrier";
        const block_replace = "air";
        if (entity.typeId === "57d_sbunk:elevator") {
            let location0 = EntityManager.getLocationOffsetByRotation(entity, 3, 0, 0);
            let location1 = EntityManager.getLocationOffsetByRotation(entity, 3, 3, 1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, -2, 0, 0);
            location1 = EntityManager.getLocationOffsetByRotation(entity, -2, 3, 1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, -2);
            location1 = EntityManager.getLocationOffsetByRotation(entity, 1, 3, -2);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            location0 = EntityManager.getLocationOffsetByRotation(entity, 0, 0, 3);
            location1 = EntityManager.getLocationOffsetByRotation(entity, 1, 3, 3);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
        }
        else if (entity.typeId === "57d_sbunk:elevator_placeable") {
            let location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, 1);
            let location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, 1);
            FSWorldHelper.fillBlocks(entity.dimension, location0, location1, block);
            if (init) {
                let block_wrapper = "stone";
                location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, -1);
                location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, 1);
                FSWorldHelper.fillBlocks(entity.dimension, location0, location1, "air");
                location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, -2);
                location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, -2);
                FSWorldHelper.replaceBlocks(entity.dimension, location0, location1, block_wrapper, block_replace);
                location0 = EntityManager.getLocationOffsetByRotation(entity, 2, 0, -1);
                location1 = EntityManager.getLocationOffsetByRotation(entity, 2, 2, 1);
                FSWorldHelper.replaceBlocks(entity.dimension, location0, location1, block_wrapper, block_replace);
                location0 = EntityManager.getLocationOffsetByRotation(entity, -2, 0, -1);
                location1 = EntityManager.getLocationOffsetByRotation(entity, -2, 2, 1);
                FSWorldHelper.replaceBlocks(entity.dimension, location0, location1, block_wrapper, block_replace);
                location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 3, -1);
                location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 3, 1);
                FSWorldHelper.replaceBlocks(entity.dimension, location0, location1, block_wrapper, block_replace);
                location0 = EntityManager.getLocationOffsetByRotation(entity, 1, -1, -1);
                location1 = EntityManager.getLocationOffsetByRotation(entity, -1, -1, 1);
                FSWorldHelper.replaceBlocks(entity.dimension, location0, location1, block_wrapper, block_replace);
            }
        }
    }
    elevatorDenied(entity) {
        if (entity.typeId === "57d_sbunk:elevator") {
            EntityManager.playAnimation(entity, "deny");
            const message_0 = {
                rawtext: [
                    { text: "§l§c" },
                    {
                        translate: FSLocalizedString.localize("bunker.access_denied.title", "Access Denied!", "Access Denied Title")
                    },
                    { text: "§r " },
                    {
                        translate: FSLocalizedString.localize("bunker.access_denied.0", "You require §lElevator Access§r to use this elevator!", "Tip/instructions for player about renaming floor names via the elevator.")
                    }
                ]
            };
            const message_1 = {
                rawtext: [
                    {
                        translate: FSLocalizedString.localize("bunker.access_denied.1", "Speak with the §lBunker Admin§r to request access.", "Instruction to speak to bunker admin for access")
                    }
                ]
            };
            let player = EntityManager.getClosestPlayer(entity);
            if (player) {
                player.sendMessage(message_0);
                player.sendMessage(message_1);
            }
            else {
                world.sendMessage(message_0);
                world.sendMessage(message_1);
            }
        }
    }
    elevatorKilled(entity) {
        if (entity.typeId !== "57d_sbunk:elevator" && entity.typeId !== "57d_sbunk:elevator_placeable") {
            return;
        }
        const block = "air";
        const block_replace = "barrier";
        const dimension = entity.dimension;
        const placeable = entity.typeId === "57d_sbunk:elevator_placeable";
        const location0 = EntityManager.getLocationOffsetByRotation(entity, 1, 0, 1);
        const location1 = EntityManager.getLocationOffsetByRotation(entity, -1, 2, 1);
        const location0a = EntityManager.getLocationOffsetByRotation(entity, 3, 0, -3);
        const location1a = EntityManager.getLocationOffsetByRotation(entity, -3, 3, 3);
        system.run(() => {
            if (placeable) {
                FSWorldHelper.replaceBlocks(dimension, location0, location1, block, block_replace);
            }
            else {
                FSWorldHelper.replaceBlocks(dimension, location0a, location1a, block, block_replace);
            }
        });
    }
}

//# sourceMappingURL=../../../_SBUNK_BPDebug/fs_sbunk/EntranceManager.js.map
