import { world, system } from "@minecraft/server";

let wasRedstonePresent = false;
let isCutsceneActive = false;

// Function to run commands
function runCommand(command) {
    try {
        return world.getDimension("overworld").runCommand(command);
    } catch (error) {
        console.warn("Error running command: " + command);
        console.warn(error);
    }
}

// Helper functions for cutscene actions
function cameraFade(timeIn, hold, timeOut) {
    runCommand(`camera @a fade time ${timeIn} ${hold} ${timeOut}`);
}

function cameraSet(pos, facing) {
    runCommand(`camera @a set minecraft:free pos ${pos} facing ${facing}`);
}

function cameraEase(duration, type, pos, facing) {
    runCommand(`camera @a set minecraft:free ease ${duration} ${type} pos ${pos} facing ${facing}`);
}

// Main cutscene sequence
function startCutscene() {
    const block = world.getDimension("overworld").getBlock({ x: 24, y: -61, z: 22 });
    const isRedstonePresent = block?.typeId === "minecraft:redstone_block";
    
    if (isRedstonePresent && !wasRedstonePresent && !isCutsceneActive) {
        // Check if any player has the "alive" tag
        const players = world.getPlayers();
        for (const player of players) {
            if (player.hasTag("alive")) {
                runCommand("title @a actionbar §l§cGame in progress!");
                return; // Do not start cutscene
            }
        }
        isCutsceneActive = true;
        
        runCommand("inputpermission set @a movement disabled");
        runCommand("inputpermission set @a camera disabled");
        runCommand("tp @a 49.50 -58.00 29.58 facing 49.53 -58.00 24.68");
        runCommand("event entity @e[family=alarm] start");
        
        cameraFade(0.2, 1, 0.5);
        runCommand("clear @a jam:extinguisher");
        
        // 1. First camera after 1 second
        system.runTimeout(() => {
            cameraSet("64.93 -56.12 28.39", "47.30 -53.80 19.30");
            cameraEase(15, "linear", "48.26 -56.12 28.15", "47.30 -53.80 19.30");
            
            // 2. Fade after 5 seconds
            system.runTimeout(() => {
                cameraFade(0.3, 0.5, 0.3);
                
                // 3. Alarm2, next camera, ease after 4 seconds
                system.runTimeout(() => {
                    runCommand("event entity @e[family=alarm2] start");
                    cameraSet("22.76 -57.38 28.52", "26.70 -56.72 28.50");
                    cameraEase(15, "linear", "24.16 -57.38 28.51", "26.70 -56.72 28.50");
                    
                    // 4. Fade, then after 3 seconds, new camera and ease
                    system.runTimeout(() => {
                        cameraFade(0.5, 1, 0.5);
                        system.runTimeout(() => {
                            cameraSet("20.43 -56.55 36.37", "23.06 -56.55 43.20");
                            cameraEase(10, "linear", "21.33 -56.55 42.58", "23.06 -56.55 43.20");
                            
                            // 5. Dramatic overhead view after 0.5 seconds
                            system.runTimeout(() => {
                                cameraFade(0.5, 2, 2);
                                
                                // 6. Final clear/reset after 1 second
                                system.runTimeout(() => {
                                    runCommand("camera @a clear");
                                    runCommand("event entity @e[family=alarm] reset");
                                    runCommand("event entity @e[family=alarm2] reset");
                                    runCommand("inputpermission set @a movement enabled");
                                    runCommand("inputpermission set @a camera enabled");
                                    isCutsceneActive = false;
                                }, 20); // 1 second = 20 ticks
                            }, 60); // 0.5 seconds = 10 ticks
                        }, 20); // 3 seconds = 60 ticks
                    }, 30); // 2 seconds = 40 ticks
                }, 20); // 4 seconds = 80 ticks
            }, 100); // 5 seconds = 100 ticks
        }, 20); // 1 second = 20 ticks
    }
    wasRedstonePresent = isRedstonePresent;
}

system.runInterval(startCutscene, 0); // 60 ticks = 3 seconds
