/// <reference path="../World/MachineBegins.ts" />
/// <reference path="../World/TurnSequence.ts" />
module Elements {
    export var animationTime = 1000;
    export var isMobile = /Mobi/i.test(navigator.userAgent); // This correctly lets Mobile users who requested the Desktop app or are on a sufficiently big tablet to get the right desktop version

    export var screenWidth = document.body.clientWidth;
    export var screenHeight = document.body.clientHeight;

    export function getLinkElements (image? : boolean) {
        if (CurrentTurnHandler == undefined) return [];
        if (CurrentTurnHandler.isTurn() || image) {
            return [CurrentTurnHandler.currentTurnTab];
        } else {
            return [HyperlinkHandler.linkedActionsTab, RoomHandler.currentRoomTab, HyperlinkHandler.commonActionsTab, CurrentTurnHandler.currentTurnTab, RoomHandler.currentRoomExits];
        }
    }

    export function updateScreenSize () {
        screenWidth = document.body.clientWidth;
        screenHeight = document.body.clientHeight;
    }

    export var mainPage = document.getElementById("mainPage");

    /**
     * Adds the blur effect to the screen
     */
    export function startTurn () {
        mainPage.classList.add("turn");
    }

    /**
     * Remove the blur effect from the screen
     */
    export function endTurn () {
        mainPage.classList.remove("turn");
    }

    /**
     * Are we currently in a turn?
     * @returns {boolean}
     */
    export function isInTurn () {
        return mainPage.classList.contains("turn");
    }


    let menuOutsideofTurn = false;
    let menus = 0;

    export function startMenu () {
        if (!isInTurn()) {
            menuOutsideofTurn = true;
            startTurn();
        }
        mainPage.classList.add("mainmenu");
        menus++;
        Elements.CurrentTurnHandler.clear();
        Controls.KeyHandler.reset();
    }

    export function endMenu () {
        Elements.CurrentTurnHandler.clear();
        if (--menus == 0) {
            if (menuOutsideofTurn) {
                endTurn();
            }
            resetMenus();
        }
    }

    export function resetMenus () {
        if (menus != 0) {
            console.error("A menu wasn't finished properly. Please correct the dialogue.");
        }
        menus = 0;
        menuOutsideofTurn = false;
        mainPage.classList.remove("mainmenu");
    }

    export function clearMainScreen () {
        Elements.CurrentTurnHandler.clear();
    }

    let loadingScreen = document.getElementById("loadingScreen");

    export function stopLoading () {
        document.body.removeChild(loadingScreen);
    }

    export async function waitForAnyKey () {
        let say = new Say(Say.CENTERED, new SayBold("Press any key to continue."));
        let elements = await say.getHTML("p", ["content"]);
        Elements.CurrentTurnHandler.print(...elements);

        await Controls.KeyHandler.getNextKey();
        Elements.CurrentTurnHandler.unprint(...elements);
    }

    export async function waitForSpaceKey () {
        let say = new Say(Say.CENTERED, new SayBold("Press Space to continue."));
        let elements = await say.getHTML("p", ["content"]);
        Elements.CurrentTurnHandler.print(...elements);

        let lastKey = "notSpace";
        while (lastKey != "Space" && lastKey != "Click") {
            lastKey = (await Controls.KeyHandler.getNextKey()).evCode;
        }

        Elements.CurrentTurnHandler.unprint(...elements);
    }

    export function printObeliskLogo () {
        let say = new Say(Say.CENTERED, new SayImage("introLogo"), Say.LINE_BREAK, new SayItalic("The Obelisk is an adult interactive fiction game set in a post-apocalyptic world ravaged by a magical structure."));
        Elements.CurrentTurnHandler.printAsContent(say);
    }
}

if (Elements.isMobile) {
    Elements.mainPage.classList.add("mobile");
}

window.addEventListener("resize", function () {
    Elements.updateScreenSize();
});


module MachineBegins {
    export let HideLoadingDivRule = MachineBegins.rulebook.createAndAddRule({
        name : "Hide loading div",
        code : runner => {
            Elements.stopLoading();
        }
    });
}

module TurnSequence {
    export let LazilyFixMenus = TurnSequence.rulebook.createAndAddRule({
        name : "Fix menus that were never closed because the dev is stupid",
        firstPriority : Rule.PRIORITY_LOWEST,
        priority : Rule.PRIORITY_LOWEST,
        code : () => {
            Elements.resetMenus();
        }
    })
}