import { GameMode, system } from "@minecraft/server";
import Buffer from "../_Utilities/Buffer";
import { MFilms } from "./Data/_Buffers/MFilms";
import Camera_Manager from "../Camera/Camera_Manager";
import Audio_Manager from "../Audio/Audio_Manager";
export default class Film_Manager {
    static play(player, film_ID, index) {
        const film = MFilms.get(film_ID);
        if (film == undefined)
            return;
        if (index == 0)
            this.save_Player(player);
        if (index >= film.tracks.length)
            return this.reset_Player(player);
        const track = film.tracks[index];
        if (track.teleport != undefined)
            player.teleport(track.teleport.location, track.teleport.options);
        let frames = track.frames;
        if (track.localized != undefined)
            frames = Camera_Manager.localization(track.localized, track.frames);
        Camera_Manager.video(player.camera, track.camera_ID, frames, 0);
        const acoustics = track.acoustics;
        for (let i = 0; i < acoustics.length; i++) {
            system.runTimeout(() => {
                Audio_Manager.player_Sound(player, acoustics[i].audio);
            }, acoustics[i].timeout * 20);
        }
        const displays = track.displays;
        for (let i = 0; i < displays.length; i++) {
            system.runTimeout(() => {
                player.onScreenDisplay.setTitle(displays[i].message);
            }, displays[i].timeout * 20);
        }
        const events = track.events;
        for (let i = 0; i < events.length; i++) {
            system.runTimeout(() => {
                events[i].callback(player);
            }, events[i].timeout * 20);
        }
        let timeout = 0;
        for (let i = 0; i < track.frames.length; i++) {
            let fade_Time = 0;
            const fade = track.frames[i].fade;
            if (fade != undefined)
                fade_Time = fade.fadeTime.fadeInTime + fade.fadeTime.fadeOutTime + fade.fadeTime.holdTime;
            timeout = timeout + track.frames[i].ease.easeTime + fade_Time;
        }
        system.runTimeout(() => {
            Film_Manager.play(player, film_ID, ++index);
        }, timeout * 20);
    }
    static reset_Player(player) {
        system.runTimeout(() => {
            const saved = Buffer.get_Data("film_player_state", player.id);
            player.inputPermissions.cameraEnabled = saved.permissions.cameraEnabled;
            player.inputPermissions.movementEnabled = saved.permissions.movementEnabled;
            player.setGameMode(saved.gamemode);
            player.teleport(saved.teleport.location, saved.teleport.options);
            Audio_Manager.player_Sound_Stop(player);
            Camera_Manager.clear(player.camera);
            Buffer.delete_Data("film_player_state", player.id);
        }, 2);
    }
    static save_Player(player) {
        const location = player.location;
        const rotation = player.getRotation();
        const game_Mode = player.getGameMode();
        const permissions = {
            cameraEnabled: player.inputPermissions.cameraEnabled,
            movementEnabled: player.inputPermissions.movementEnabled,
        };
        Buffer.set_Data("film_player_state", player.id, {
            gamemode: game_Mode,
            permissions: permissions,
            teleport: { location: location, options: { rotation: rotation } },
        });
        player.inputPermissions.cameraEnabled = false;
        player.inputPermissions.movementEnabled = false;
        player.setGameMode(GameMode.spectator);
    }
}
