import { world } from "@minecraft/server";
//Import maps
import { cinematicListMap, cineRuntimeDataMap } from "./data/maps";
//Import Functions
import { framePlacementMenu } from "./functions/ui/path-placement/frame-placement";
import { cameraPlaybackMenu } from "./functions/ui/path-placement/camera-playback-menu";
import { OptimizedDatabase } from "../replay/data/data-hive";
import { frameManagementMenu } from "./functions/ui/path-placement/manage-frames";
import { cineMainMenu } from "./functions/ui/cine-main-menu";
import { panoramicCinematic } from "./functions/ui/panorama/panoramic-cinematic";
import { orbitalCinematic } from "./functions/ui/orbital-cine/orbital-cinematic";
const cineUiHandlers = {
    cineMainMenu: (player) => cineMainMenu(player),
    framePlacementMenu: (player) => framePlacementMenu(player),
    cameraPlaybackMenu: (player) => cameraPlaybackMenu(player),
    frameManagementMenu: (player) => frameManagementMenu(player),
    panoramicCinematic: (player) => panoramicCinematic(player),
    orbitalCinematic: (player) => orbitalCinematic(player),
};
//Initialise DataBase(s)
export let cinematicListDB;
export let cinematicFramesDB;
export let cinematicSettingsDB;
world.afterEvents.worldLoad.subscribe(() => {
    cinematicListDB = new OptimizedDatabase("cinematicList");
    cinematicFramesDB = new OptimizedDatabase("cinematicFramesData");
    cinematicSettingsDB = new OptimizedDatabase("cinematicSettingsData");
});
//ItemUse event
world.afterEvents.itemUse.subscribe(({ source, itemStack }) => {
    if (!itemStack || itemStack?.typeId !== "minecraft:stick" || !/^(cinematic|replaycraft1)$/i.test(itemStack.nameTag))
        return;
    // get saved data or init defaults
    const cinematicList = cinematicListDB.get(source.id) ?? [];
    cinematicListMap.set(source.id, cinematicList);
    const runtimeDefaults = {
        state: "cineMainMenu",
        isCameraInMotion: false,
        loadedCinematicType: "none",
    };
    const cineRuntimeData = cineRuntimeDataMap.get(source.id) ?? runtimeDefaults;
    cineRuntimeDataMap.set(source.id, cineRuntimeData);
    //show the ui depending on the ui state
    const handler = cineUiHandlers[cineRuntimeData.state] ?? cineUiHandlers.framePlacementMenu;
    handler(source);
});
//Frame Particles Loop
// system.runInterval(() => {
//     for (const player of world.getAllPlayers()) {
//         const frames = frameDataMap.get(player.id) ?? [];
//         const otherData = otherDataMap.get(player.id);
//         const settingsData = settingsDataMap.get(player.id);
//         if (!frames.length || !settingsData.cineParSwitch || otherData.isCameraInMotion) {
//             continue;
//         }
//         const partSelected = particlesStr[settingsData.cineParType];
//         frames.map((frame: FrameData) => {
//             player.runCommand(`particle ${partSelected} ${frame.pos.x} ${frame.pos.y} ${frame.pos.z}`);
//         });
//     }
// }, 8);
