"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StandardLayoutUC = void 0;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const BaseAppState_1 = require("../../BaseApp/Entity/BaseAppState");
const ChallengeEntity_1 = require("../../Challenge/Entities/ChallengeEntity");
const ZSpaceEntity_1 = require("../../zSpace/Entities/ZSpaceEntity");
const Entities_1 = require("../Entities");
const setupStandardFramedScreen_1 = require("../Factories/setupStandardFramedScreen");
class StandardLayoutUC extends core_1.AppObjectUC {
    get challenge() {
        return this.getCachedSingleton(ChallengeEntity_1.ChallengeEntity.type);
    }
    get consumptionSideBySideBreakWidth() {
        return (this.infoColumnWidth * 4 +
            this.toolbarWidth +
            this.infoOverlayMargin +
            2 * this.padding_x);
    }
    get infoContentLengthOverlayBreak() {
        if (!this.framedScreen) {
            return 0;
        }
        return 0.5 * this.framedScreen.screenHeight;
    }
    updatePositionForPadding() {
        if (!this.slideInfo ||
            !this.renderCanvas ||
            !this.inspector ||
            !this.toolbar ||
            !this.lowerLetterBox ||
            !this.upperLetterBox ||
            !this.leftLetterBox ||
            !this.rightLetterBox)
            return;
        if (this.slideInfo.show) {
            this.slideInfo.position = core_2.Vector2.Add(this.slideInfo.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.renderCanvas.show) {
            this.renderCanvas.position = core_2.Vector2.Add(this.renderCanvas.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.inspector.show) {
            this.inspector.position = core_2.Vector2.Add(this.inspector.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.toolbar.show) {
            this.toolbar.position = core_2.Vector2.Add(this.toolbar.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.lowerLetterBox.show) {
            this.lowerLetterBox.position = core_2.Vector2.Add(this.lowerLetterBox.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.upperLetterBox.show) {
            this.upperLetterBox.position = core_2.Vector2.Add(this.upperLetterBox.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.leftLetterBox.show) {
            this.leftLetterBox.position = core_2.Vector2.Add(this.leftLetterBox.position, new core_2.Vector2(this.padding_x, 0));
        }
        if (this.rightLetterBox.show) {
            this.rightLetterBox.position = core_2.Vector2.Add(this.rightLetterBox.position, new core_2.Vector2(this.padding_x, 0));
        }
    }
    layoutConsumptionOverlay(height, width) {
        if (!this.baseApp ||
            !this.slideInfo ||
            !this.renderCanvas ||
            !this.inspector ||
            !this.toolbar ||
            !this.lowerLetterBox ||
            !this.upperLetterBox ||
            !this.leftLetterBox ||
            !this.rightLetterBox)
            return;
        if (this.challenge?.selectedChallenge === "NONE" &&
            !this.baseApp.slideText) {
            this.disableContainer(this.slideInfo);
        }
        else {
            this.slideInfo.position = new core_2.Vector2(this.toolbarWidth + this.infoOverlayMargin, this.infoOverlayMargin);
            this.slideInfo.size = new core_2.Vector2(this.infoColumnWidth, -1);
            this.slideInfo.show = true;
        }
        this.toolbar.position = new core_2.Vector2(0, 0);
        this.toolbar.size = new core_2.Vector2(this.toolbarWidth, height);
        this.toolbar.show = true;
        const renderCanvasX = this.toolbarWidth;
        this.renderCanvas.position = new core_2.Vector2(renderCanvasX, 0);
        const canvasSize = width - this.toolbarWidth;
        this.renderCanvas.size = new core_2.Vector2(canvasSize, height);
        this.renderCanvas.show = true;
        this.disableContainer(this.inspector);
        this.disableContainer(this.lowerLetterBox);
        this.disableContainer(this.upperLetterBox);
        this.disableContainer(this.leftLetterBox);
        this.disableContainer(this.rightLetterBox);
        this.updatePositionForPadding();
    }
    layoutConsumptionSideBySide(height, width) {
        if (!this.baseApp ||
            !this.slideInfo ||
            !this.renderCanvas ||
            !this.inspector ||
            !this.toolbar ||
            !this.lowerLetterBox ||
            !this.upperLetterBox ||
            !this.leftLetterBox ||
            !this.rightLetterBox)
            return;
        if (this.challenge?.selectedChallenge === "NONE" &&
            !this.baseApp.slideText) {
            this.disableContainer(this.slideInfo);
            this.toolbar.position = new core_2.Vector2(0, 0);
            this.toolbar.size = new core_2.Vector2(this.toolbarWidth, height);
            this.toolbar.show = true;
            const renderCanvasX = this.toolbarWidth;
            this.renderCanvas.position = new core_2.Vector2(renderCanvasX, 0);
            const canvasSize = width - renderCanvasX;
            this.renderCanvas.size = new core_2.Vector2(canvasSize, height);
            this.renderCanvas.show = true;
        }
        else {
            this.slideInfo.position = core_2.Vector2.Zero();
            this.slideInfo.size = new core_2.Vector2(this.infoColumnWidth, height);
            this.slideInfo.show = true;
            this.toolbar.position = new core_2.Vector2(this.infoColumnWidth, 0);
            this.toolbar.size = new core_2.Vector2(this.toolbarWidth, height);
            this.toolbar.show = true;
            const renderCanvasX = this.infoColumnWidth + this.toolbarWidth;
            this.renderCanvas.position = new core_2.Vector2(renderCanvasX, 0);
            const canvasSize = width - renderCanvasX;
            this.renderCanvas.size = new core_2.Vector2(canvasSize, height);
            this.renderCanvas.show = true;
        }
        this.disableContainer(this.inspector);
        this.disableContainer(this.lowerLetterBox);
        this.disableContainer(this.upperLetterBox);
        this.disableContainer(this.leftLetterBox);
        this.disableContainer(this.rightLetterBox);
        this.updatePositionForPadding();
    }
    layoutForAuthoring(height, width) {
        if (!this.baseApp ||
            !this.slideInfo ||
            !this.renderCanvas ||
            !this.inspector ||
            !this.toolbar ||
            !this.lowerLetterBox ||
            !this.upperLetterBox ||
            !this.leftLetterBox ||
            !this.rightLetterBox)
            return;
        this.slideInfo.position = core_2.Vector2.Zero();
        this.slideInfo.size = new core_2.Vector2(this.infoColumnWidth, height);
        this.slideInfo.show = true;
        this.toolbar.position = new core_2.Vector2(this.infoColumnWidth, 0);
        this.toolbar.size = new core_2.Vector2(this.toolbarWidth, height);
        this.toolbar.show = true;
        let inspectorWidth = width - height - this.infoColumnWidth - this.toolbarWidth;
        if (inspectorWidth < this.inspectorColumnWidth_Min) {
            inspectorWidth = this.inspectorColumnWidth_Min;
        }
        else if (inspectorWidth > this.inspectorColumnWidth_Max) {
            inspectorWidth = this.inspectorColumnWidth_Max;
        }
        this.inspector.size = new core_2.Vector2(inspectorWidth, height);
        this.inspector.show = true;
        const renderCanvasX = this.infoColumnWidth + this.toolbarWidth;
        this.renderCanvas.position = new core_2.Vector2(renderCanvasX, 0);
        const canvasWidth = width - inspectorWidth - this.toolbarWidth - this.infoColumnWidth;
        this.renderCanvas.size = new core_2.Vector2(canvasWidth, height);
        this.renderCanvas.show = true;
        const inspectorX = this.infoColumnWidth + this.toolbarWidth + canvasWidth;
        this.inspector.position = new core_2.Vector2(inspectorX, 0);
        if (canvasWidth === height) {
            this.disableContainer(this.lowerLetterBox);
            this.disableContainer(this.upperLetterBox);
            this.disableContainer(this.leftLetterBox);
            this.disableContainer(this.rightLetterBox);
        }
        else if (canvasWidth < height) {
            const halfGap = Math.floor(0.5 * (height - canvasWidth));
            this.upperLetterBox.position = new core_2.Vector2(renderCanvasX, 0);
            this.upperLetterBox.size = new core_2.Vector2(canvasWidth, halfGap);
            this.upperLetterBox.show = true;
            this.lowerLetterBox.position = new core_2.Vector2(renderCanvasX, height - halfGap);
            this.lowerLetterBox.size = new core_2.Vector2(canvasWidth, halfGap);
            this.lowerLetterBox.show = true;
            this.disableContainer(this.leftLetterBox);
            this.disableContainer(this.rightLetterBox);
        }
        else if (canvasWidth > height) {
            const halfGap = Math.floor(0.5 * (canvasWidth - height));
            this.leftLetterBox.position = new core_2.Vector2(renderCanvasX, 0);
            this.leftLetterBox.size = new core_2.Vector2(halfGap, height);
            this.leftLetterBox.show = true;
            this.rightLetterBox.position = new core_2.Vector2(renderCanvasX + canvasWidth - halfGap, 0);
            this.rightLetterBox.size = new core_2.Vector2(halfGap, height);
            this.rightLetterBox.show = true;
            this.disableContainer(this.lowerLetterBox);
            this.disableContainer(this.upperLetterBox);
        }
        this.updatePositionForPadding();
    }
    layoutForZSpaceEmulation(height, width) {
        if (!this.baseApp ||
            !this.slideInfo ||
            !this.renderCanvas ||
            !this.inspector ||
            !this.toolbar ||
            !this.lowerLetterBox ||
            !this.upperLetterBox ||
            !this.leftLetterBox ||
            !this.rightLetterBox)
            return;
        this.renderCanvas.position = new core_2.Vector2(0, 0);
        this.renderCanvas.size = new core_2.Vector2(width, height);
        this.renderCanvas.show = true;
        this.disableContainer(this.slideInfo);
        this.disableContainer(this.toolbar);
        this.disableContainer(this.inspector);
        this.disableContainer(this.lowerLetterBox);
        this.disableContainer(this.upperLetterBox);
        this.disableContainer(this.leftLetterBox);
        this.disableContainer(this.rightLetterBox);
    }
    getContainerByID(id) {
        if (!this.framedScreen) {
            console.warn("No framed screen");
            return;
        }
        const container = this.framedScreen.getContainerByID(id);
        if (!container) {
            console.warn("Unable to find container by ID: " + container);
        }
        return container;
    }
    constructor(appObject) {
        super(appObject, StandardLayoutUC.type);
        this.padding_x = 0;
        this.toolbarWidth = 64;
        this.infoColumnWidth = 264;
        this.infoOverlayMargin = 24;
        this.inspectorColumnWidth_Min = 264;
        this.inspectorColumnWidth_Max = 528;
        this.updateLayout = () => {
            if (!this.framedScreen || !this.baseApp)
                return;
            const height = this.framedScreen.screenHeight;
            const width = this.framedScreen.screenWidth - 2 * this.padding_x;
            if (this.zSpaceEntity && this.zSpaceEntity.emulate) {
                this.layoutForZSpaceEmulation(this.framedScreen.screenHeight, this.framedScreen.screenWidth);
                return;
            }
            if (this.baseApp?.isAuthoring === true) {
                this.layoutForAuthoring(height, width);
                return;
            }
            if (this.framedScreen.screenWidth < this.consumptionSideBySideBreakWidth) {
                this.layoutConsumptionSideBySide(height, width);
                return;
            }
            if (this.baseApp.slideInfoContentHeight >
                this.infoContentLengthOverlayBreak ||
                this.baseApp.slideInfoContentHeight === 0) {
                this.layoutConsumptionSideBySide(height, width);
                return;
            }
            this.layoutConsumptionOverlay(height, width);
        };
        this.disableContainer = (container) => {
            container.show = false;
            container.position = core_2.Vector2.Zero();
            container.size = core_2.Vector2.Zero();
        };
        this.framedScreen = appObject.getComponent(Entities_1.FramedScreenEntity.type);
        if (!this.framedScreen) {
            this.appObjects.submitFatal("FramedScreenSizePM", "FramedScreenSizePM has been added to an App Object that does not have a FramedScreenEntity");
            throw new Error("No FramedScreenEntity");
        }
        this.slideInfo = this.getContainerByID(setupStandardFramedScreen_1.SLIDE_INFO_FRAMED_CONTAINER_ID);
        this.renderCanvas = this.getContainerByID(setupStandardFramedScreen_1.RENDER_CANVAS_FRAMED_CONTAINER_ID);
        this.inspector = this.getContainerByID(setupStandardFramedScreen_1.INSPECTOR_FRAMED_CONTAINER_ID);
        this.toolbar = this.getContainerByID(setupStandardFramedScreen_1.TOOLBAR_FRAMED_CONTAINER_ID);
        this.lowerLetterBox = this.getContainerByID(setupStandardFramedScreen_1.LOWER_LETTERBOX_FRAMED_CONTAINER_ID);
        this.upperLetterBox = this.getContainerByID(setupStandardFramedScreen_1.UPPER_LETTERBOX_FRAMED_CONTAINER_ID);
        this.leftLetterBox = this.getContainerByID(setupStandardFramedScreen_1.LEFT_LETTERBOX_FRAMED_CONTAINER_ID);
        this.rightLetterBox = this.getContainerByID(setupStandardFramedScreen_1.RIGHT_LETTERBOX_FRAMED_CONTAINER_ID);
        this.zSpaceEntity = ZSpaceEntity_1.ZSpaceEntity.get(this.appObjects);
        if (this.zSpaceEntity) {
            this.zSpaceEntity.addChangeObserver(this.updateLayout);
        }
        else {
            console.warn("Unable to find the zSpaceEntity");
            return;
        }
        this.baseApp = BaseAppState_1.BaseAppState.get(this.appObjects);
        if (this.baseApp) {
            this.baseApp.addChangeObserver(this.updateLayout);
        }
        else {
            this.error("Unable to find base app");
            return;
        }
        this.framedScreen.addChangeObserver(this.updateLayout);
    }
}
exports.StandardLayoutUC = StandardLayoutUC;
StandardLayoutUC.type = "StandardLayoutUC";
//# sourceMappingURL=StandardLayoutUC.js.map