module CharacterCreation.ContentTypes {
    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("Fetishes:")).getHTMLContent().then(eles => {
            eles.forEach(ele => {
                leftSide.appendChild(ele);
            })
        }));

        let perks = ContentHandler.getContentTypes().sort((a : ContentType, b : ContentType) => {
            let na = a.getName().toUpperCase();
            let nb = b.getName().toUpperCase();
            if (na < nb) return -1;
            if (na > nb) return 1;
            return 0;
        });

        for (let i = 0; i < perks.length; i++) {
            let perk = perks[i];
            let say = new Say(perk.getName());
            if (perk.isAllowed()) {
                if (!perk.isChangeable()) {
                    say.add (" (Mandatory)");
                } else {
                    say.add(" - Into it");
                }
            } else if (!perk.isChangeable()) {
                say.add(" (Forbidden)");
            }
            await (say).getHTML("p", ["choice"]).then(elementArray => {
                Controls.Links.makeCustomLink(elementArray[0], {
                    mouseover: () => {
                        printPerk(perk);
                    },
                    mouseout: () => {
                        emptyPerk();
                    },
                    click: () => {
                        perk.toggle();
                        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);
        printRightSideInfo();
    }

    function printRightSideInfo() {
        let p = document.createElement("p");
        p.classList.add("content");
        p.appendChild(document.createTextNode("The fetishes the player character has will tailor the world the Obelisk will create for them."));

        rightSide.appendChild(p);
    }

    function printPerk (perk : ContentType) {
        let say = new Say(perk.getDescription());
        say.getHTMLContent().then((eles => {
            emptyPerk();
            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 async function selectContentTypes () {
        Elements.startMenu();

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

        reset();

        await promise;

        Elements.endMenu();
    }
}