module CharacterCreation {
    let ccDiv = document.createElement("div");
    ccDiv.id = "characterCreation";
    let rightSide = document.createElement("div");
    rightSide.id = "ccRight";
    let leftSide = document.createElement("div");
    leftSide.id = "ccLeft";
    ccDiv.appendChild(leftSide);
    ccDiv.appendChild(rightSide);

    let resolver;

    async function printTable () {
        printOrigin();

        while (leftSide.firstChild) leftSide.removeChild(leftSide.firstChild);

        await (new Say(new SayBold("Character Origin:")).getHTMLContent().then(eles => {
            eles.forEach(ele => {
                leftSide.appendChild(ele);
            })
        }));

        let origins = CharacterOrigin.getOrigins();
        for (let i = 0; i < origins.length; i++) {
            let origin = origins[i];
            let say = new Say(origin.name);
            if (origin.id == CharacterCreation.PlayerOrigin.value) {
                say.add(" - SELECTED");
            }
            await (say).getHTML("p", ["choice"]).then(elementArray => {
                Controls.Links.makeCustomLink(elementArray[0], {
                    mouseover: () => {
                        printOrigin(origin);
                    },
                    mouseout: () => {
                        printOrigin();
                    },
                    click: () => {
                        CharacterCreation.PlayerOrigin.value = origin.id;
                        reset();
                    }
                });
                Controls.KeyHandler.applyCode(elementArray[0], Controls.KeyHandler.getSecondKeyCode());
                leftSide.appendChild(elementArray[0]);
            });
        }
    }

    function printConfirm () {
        (new Say("Confirm")).getHTML("p", ["choice"]).then(elementArray => {
            elementArray[0].addEventListener("click", () => {
                resolver();
            });
            Controls.KeyHandler.applyCode(elementArray[0], Controls.KeyHandler.getSecondKeyCode());
            Elements.CurrentTurnHandler.print(elementArray[0]);
        });
    }

    function printOrigin (origin? : CharacterOrigin) {
        if (origin == undefined) {
            origin = CharacterOrigin.getOrigin(CharacterCreation.PlayerOrigin.value);
        }
        let say = new Say(origin.description);
        if (origin.bonusStats != undefined) {
            say.add(Say.PARAGRAPH_BREAK, origin.bonusStats);
        }
        say.getHTMLContent().then((eles => {
            while (rightSide.firstChild) rightSide.removeChild(rightSide.firstChild);

            for (let i = 0; i < eles.length; i++) {
                rightSide.appendChild(eles[i]);
            }
        }));
    }

    function reset () {
        Elements.CurrentTurnHandler.clear();
        Elements.CurrentTurnHandler.print(ccDiv);
        printTable();
        Elements.CurrentTurnHandler.print(document.createElement("br"));
        Elements.CurrentTurnHandler.print(document.createElement("br"));
        printConfirm();
    }

    export let CCOrigin = CharacterCreation.rulebook.createAndAddRule({
        name: "Character Creation - Origin",
        firstPriority: Rule.PRIORITY_MEDIUM,
        priority: Rule.PRIORITY_HIGH,
        code: async () => {
            Elements.startMenu();

            let promise = new Promise(resolve => {
                resolver = resolve;
            });

            reset();

            await promise;

            CharacterCreation.getOrigin().confirmPicked();

            Elements.endMenu();
        }
    });
}