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 () {
        while (leftSide.firstChild) leftSide.removeChild(leftSide.firstChild);

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

        let perks = Perk.getPerks();
        for (let i = 0; i < perks.length; i++) {
            let perk = perks[i];
            let say = new Say(perk.name);
            if (perk.isEnabled()) {
                if (perk.isForced()) {
                    say.add (" (Mandatory)");
                } else {
                    say.add(" - Selected");
                }
            } else if (perk.isForced()) {
                say.add(" (Forbidden)");
            }
            await (say).getHTML("p", ["choice"]).then(elementArray => {
                Controls.Links.makeCustomLink(elementArray[0], {
                    mouseover: () => {
                        printPerk(perk);
                    },
                    mouseout: () => {
                        emptyPerk();
                    },
                    click: () => {
                        if (!perk.isForced()) {
                            perk.value = !perk.value;
                        }
                        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 emptyPerk () {
        while (rightSide.firstChild) rightSide.removeChild(rightSide.firstChild);
    }

    function printPerk (perk : Perk) {
        let say = new Say(perk.description);
        say.getHTMLContent().then((eles => {
            emptyPerk();
            for (let i = 0; i < eles.length; i++) {
                rightSide.appendChild(eles[i]);
            }
        }));
    }

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

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

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

            reset();

            await promise;

            let perks = Perk.getPerks();
            perks.forEach(perk => {
                if (perk.isEnabled()) {
                    perk.confirmPicked();
                }
            });

            Elements.endMenu();
        }
    });
}