/// <reference path="../Elements.ts" />
module Elements.AppearanceHandler {
    var target = document.getElementById("appearanceTarget");

    function empty () {
        while (target.firstChild) {
            target.removeChild(target.firstChild);
        }
    }

    export async function print (say : Say) {
        await say.getHTML("p", ["appearanceDescription"]).then(value => {
            for (let i = 0; i < value.length; i++) {
                target.appendChild(value[i]);
            }
        });
    }

    export async function updateAppearance () {
        empty();
        // TODO: maybe use multiple names for "dick"
        let player: Humanoid = <Humanoid> WorldState.player;

        let playerGender = player.getGenderValue();
        let playerSluttiness = player.getSluttiness();
        // let playerTop = player.getTopClothOn(Humanoid.SLOT_BREASTS);
        // let playerBottom = player.getTopClothOn(Humanoid.SLOT_LEG_UPPER);
        // let playerShoes = player.getTopClothOn(Humanoid.SLOT_FEET);

        let you = new SayLink("You");
        you.setAction(new ActionExamine(WorldState.player, WorldState.player));
        let presentation = new Say(you, " are presenting as ", new SayAn(), player.getShortestDescription(), ".");

        if (playerSluttiness.naked) {
            presentation.add(" You are naked.");
        } else if (playerSluttiness.halfNaked) {
            presentation.add(" You are almost naked.");
        }

        let tits = <HumanoidBreasts> Thing.PartRelation.getRightTypeOne(player, HumanoidBreasts);
        let penis = <HumanoidPenis> Thing.PartRelation.getRightTypeOne(player, HumanoidPenis);
        let bulges = player.getBulges();



        if (bulges.breasts > 0 || playerGender.genderValueCorrected > 60) {
            let realTits = tits.getSizeText();
            let fakeTits = HumanoidBreasts.getSizeText(bulges.breasts);
            presentation.add(" You have " + realTits + " breasts");
            if (bulges.breasts != tits.getSize() && realTits != fakeTits) {
                presentation.add(", padded to appear as " + fakeTits + " through your clothing.");
            } else {
                if (!tits.isUncovered()) {
                    presentation.add(", safely covered.")
                } else {
                    presentation.add(".");
                }
            }
        }


        // presentation.add(Say.PARAGRAPH_BREAK, new SayBold("Gender Value: "), playerGender.genderValueCorrected);
        // presentation.add(Say.PARAGRAPH_BREAK, new SayBold("Sluttiness Value: "), playerSluttiness.sluttinessCorrected);
        //presentation.add(Say.PARAGRAPH_BREAK, new SayBold("Breasts: "), tits.getSizeText(), " (Visible as " + HumanoidBreasts.getSizeText(bulges.breasts) + ")");

        if (penis == undefined) {
            presentation.add(" You have ", new SayAn(), HumanoidPenis.getSizeText(bulges.crotch) + " crotch bulge visible through your clothing.");
        } else {
            let realDick = penis.getSizeText();
            let fakeDick = HumanoidPenis.getSizeText(bulges.crotch);
            presentation.add(" You have ", new SayAn(), realDick + " dick");
            if (penis.getActualSize() != bulges.crotch && realDick != fakeDick) {
                presentation.add(", which looks like it is " + fakeDick + " due to your clothing.")
            } else {
                if (!penis.isUncovered()) {
                    presentation.add(", which is covered.");
                } else {
                    presentation.add(".");
                }
            }
        }

        // if (penis == undefined) {
        //     if (bulges.crotch > 0) {
        //         presentation.add(Say.PARAGRAPH_BREAK, "You have ", new SayAn(), HumanoidPenis.getSizeText(bulges.crotch) + " bulge visible on your crotch.");
        //     }
        // } else {
        //     presentation.add(Say.PARAGRAPH_BREAK,
        //         new SayBold("Dick: "),
        //         penis.getSizeText(),
        //         " (Visible as ",
        //         new SayAn(),
        //         HumanoidPenis.getSizeText(bulges.crotch),
        //             playerGender.hasPenis ?
        //                 (penis.isFlaccid ? " flaccid" : " erect") +
        //                 (penis.getActualSize() < 9 ? " microdick" : " cock")
        //             : " bulge",
        //         ")"
        //     );
        // }

        // TODO: Check if has a specific look to it. Optional. Hard to do.
        // Examples: "You are dressed casually like a woman.", "You are wearing formal women's clothing." etc.

        await print(presentation);
    }
}