import { AppObjectUC } from "@vived/core";
import { Quaternion, Vector3 } from "@vived/core";
import { StateMachineEntity, } from "../../StateMachine/Entities/StateMachineEntity";
import { StoredView } from "../../StoredView";
import { SceneRootEntity } from "../Entities/SceneRootEntity";
export class SerializeSceneRoot extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.name = "sceneRoot";
    }
}
SerializeSceneRoot.type = "SerializeSceneRoot";
export function makeSerializeSceneRoot(appObject) {
    return new SerializeSceneRootImp(appObject);
}
class SerializeSceneRootImp extends SerializeSceneRoot {
    getSystemState() {
        if (!this.sceneRoot) {
            this.warn("No scene root entity");
            return {};
        }
        if (this.storedView && this.storedView.hasStoredView) {
            const data = {
                position: this.storedView.position.dto,
                rotation: this.storedView.rotation.dto,
                scale: this.storedView.scale,
            };
            return data;
        }
        else {
            const data = {
                position: this.sceneRoot.position.dto,
                rotation: this.sceneRoot.rotation.dto,
                scale: this.sceneRoot.scale,
            };
            return data;
        }
    }
    applySystemState(serializedState, version, transitionTime) {
        try {
            if (version === 1) {
                const castState = serializedState;
                this.applyV1Data(castState, transitionTime);
            }
            else if (version === 2) {
                const castState = serializedState;
                this.applyV2Data(castState, transitionTime);
            }
            else {
                console.warn(`Unsupported version number ${version}. Defaulting`);
                this.applyDefaultState(transitionTime);
            }
        }
        catch (e) {
            this.warn(`Caught when deserializing ${e}. Defaulting`);
            this.applyDefaultState(transitionTime);
        }
    }
    applyDefaultState(transitionDuration) {
        if (!this.sceneRoot || !this.stateMachine) {
            return;
        }
        this.sceneRoot.lerpToDefault(transitionDuration * 1000, this.stateMachine.transitionEaseFunction);
    }
    applyV2Data(data, transitionTime) {
        if (!this.sceneRoot || !this.stateMachine) {
            return;
        }
        const pos = Vector3.FromDTO(data.position);
        const rot = Quaternion.FromDTO(data.rotation);
        const scale = new Vector3(data.scale, data.scale, data.scale);
        const rootTransform = this.sceneRoot.transform;
        const lerpDTO = {
            durationMS: transitionTime * 1000,
            ease: this.stateMachine.transitionEaseFunction,
            desiredPosition: pos,
            desiredRotation: rot,
            desiredScale: scale,
        };
        rootTransform.lerp(lerpDTO);
    }
    applyV1Data(data, transitionTime) {
        if (!this.sceneRoot || !this.stateMachine) {
            return;
        }
        const pos = Vector3.FromArray(data.position);
        const rot = Quaternion.FromArray(data.rotation);
        const scale = Vector3.FromArray(data.scale);
        const rootTransform = this.sceneRoot.transform;
        const lerpDTO = {
            durationMS: transitionTime * 1000,
            ease: this.stateMachine.transitionEaseFunction,
            desiredPosition: pos,
            desiredRotation: rot,
            desiredScale: scale,
        };
        rootTransform.lerp(lerpDTO);
    }
    constructor(appObject) {
        super(appObject, SerializeSceneRoot.type);
        this.seriliziationVersion = 2;
        this.sceneRoot = appObject.getComponent(SceneRootEntity.type);
        if (!this.sceneRoot) {
            this.warn("SerializeSceneRoot UC has been added to an App Object that does not have a SceneRoot Entity. Add one before adding the UC.");
            this.dispose();
            return;
        }
        this.storedView = appObject.getComponent(StoredView.type); // StoreView is optional
        this.stateMachine = StateMachineEntity.get(appObject.appObjectRepo);
        if (this.stateMachine) {
            this.stateMachine.registerSerializedSystem(this);
        }
        else {
            this.warn("Unable to find StateMachineEntity");
        }
    }
}
//# sourceMappingURL=SerializeSceneRoot.js.map