import { ReplayCraft2F } from "../ui/replaycraft-replay-menu";
import { ReplayCraft2D } from "../ui/replaycraft-preview-menu";
import { uiReplayCraftMainMenu } from "../ui/replaycraft-main-menu";
import { ReplayCraft2B } from "../ui/replaycraft-recording-menu";
import { ReplayCraft2C } from "../ui/replaycraft-pause-menu";
import { ReplayCraft2E } from "../ui/replaycraft-camera-menu";
import { saveToDB } from "../functions/replayControls/save-to-database";
import { respawnCameraEntities } from "../functions/camera/camera-load-from-database";
import { removeEntities } from "../functions/remove-entities";
export class ReplayStateMachine {
    session;
    state;
    nextData;
    states;
    constructor(session) {
        this.session = session;
        this.state = "default";
        this.states = {
            recStartRep: this.handleRecStartRep.bind(this), //Playback of the replay not preview mode
            viewStartRep: this.handleViewStartRep.bind(this), //Preview mode of the replay
            recCompleted: this.handleRecCompleted.bind(this), //Replay PLayback completed/Stopped
            recCamSetup: this.handleRecCamSetup.bind(this), //Camera setup post recording
            recSaved: this.handleRecSaved.bind(this), //Replay recording has been stop & saved
            recPaused: this.handleRecPaused.bind(this), //Replay recording has been paused
            recPending: this.handleRecPending.bind(this), //Replay is recording
            editingCameraPos: this.handleEditingCameraPos.bind(this), //Editing camera position
            default: this.handleDefault.bind(this), //Default state, shows main menu
        };
    }
    handleRecStartRep(player) {
        ReplayCraft2F(player);
    }
    handleViewStartRep(player) {
        ReplayCraft2D(player);
    }
    handleRecCompleted(player, data) {
        if (data === true) {
            ReplayCraft2E(player);
        }
        else {
            ReplayCraft2F(player);
        }
    }
    handleRecCamSetup(player) {
        ReplayCraft2E(player);
    }
    handleRecSaved(player) {
        ReplayCraft2D(player);
    }
    handleRecPaused(player) {
        ReplayCraft2C(player);
    }
    handleRecPending(player) {
        ReplayCraft2B(player);
    }
    handleDefault(player) {
        uiReplayCraftMainMenu(player);
    }
    handleEditingCameraPos(player) {
        const camIndex = this.session.currentEditingCamIndex;
        if (camIndex === -1) {
            player.sendMessage("§cNo camera point selected to edit.");
            if (this.session.soundCue) {
                player.playSound("note.bass");
            }
            return;
        }
        this.session.replayCamPos[camIndex].position = player.getHeadLocation();
        this.session.replayCamRot[camIndex].rotation = player.getRotation();
        this.session.currentEditingCamIndex = -1;
        saveToDB(player, this.session); // Optional: pass `this.session` if needed
        removeEntities(player, false);
        respawnCameraEntities(player);
        player.sendMessage(`§4[ReplayCraft] §fCamera point ${camIndex + 1} updated successfully.`);
        if (this.session.soundCue) {
            player.playSound("random.orb");
        }
        this.setState("recCamSetup");
        this.handleEvent(player);
    }
    setState(newState, data) {
        if (this.states[newState]) {
            this.state = newState;
            this.nextData = data;
        }
        else {
            this.state = "default";
            this.nextData = undefined;
        }
    }
    handleEvent(player) {
        const handler = this.states[this.state];
        if (handler) {
            handler(player, this.nextData);
            this.nextData = undefined;
        }
        else {
            this.handleDefault(player);
        }
    }
}
