/// <reference path="../Elements.ts" />
/// <reference path="../../World/Classes/Rulebook.ts" />
/// <reference path="../../World/Classes/Rule.ts" />
/// <reference path="../../World/Classes/Action/ActionExamine.ts" />
/// <reference path="../../World/Classes/Things/CoinPouch.ts" />
module Elements.InventoryHandler {
    var container = document.getElementById("inventoryTarget");

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

    function printHeader (header : string) {
        let p = document.createElement("p");
        p.classList.add("inventoryHeader");
        p.appendChild(document.createTextNode(header + ":"));
        container.appendChild(p);
    }

    let currentRow : HTMLElement;

    export let LinkingThing = new Rulebook<Thing>("Inventory - Links for a Thing");

    export function printThingLink (shortcut : string, action : Action) {
        let nameLink = document.createElement("div");
        nameLink.classList.add("inventoryLink");
        nameLink.appendChild(document.createTextNode(shortcut));
        currentRow.appendChild(nameLink);

        Controls.Links.makeLink(nameLink, action);
    }

    async function printThing (thing : Thing) {
        currentRow = document.createElement("div");
        currentRow.classList.add("inventoryRow");

        let nameLink = document.createElement("div");
        nameLink.classList.add("inventoryLink", "name");
        nameLink.appendChild(document.createTextNode(thing.getPrintedName()));
        currentRow.appendChild(nameLink);

        Controls.Links.makeLink(nameLink, new ActionExamine(WorldState.player, thing));

        await LinkingThing.execute({noun : thing});

        container.appendChild(currentRow);
    }

    export function thingSort (a : Thing, b : Thing) {
        let na = a.getPrintedName().toLowerCase();
        let nb = b.getPrintedName().toLowerCase();
        if (na < nb) return -1;
        if (na > nb) return 1;
        return 0;
    }

    export async function updateInventory () {
        let player = WorldState.player;

        empty();

        let wielded = Thing.WieldRelation.getRight(player).sort(thingSort);
        let worn = Thing.WearRelation.getRight(player).sort(thingSort);
        let carried = Thing.CarryRelation.getRight(player).sort(thingSort);

        if (wielded.length > 0) {
            printHeader("Wielded");
            for (let i = 0; i < wielded.length; i++) {
                await printThing(wielded[i]);
            }
        }

        if (worn.length > 0) {
            printHeader("Worn");
            for (let i = 0; i < worn.length; i++) {
                await printThing(worn[i]);
            }
        }

        if (carried.length > 0) {
            printHeader("Carried");
            for (let i = 0; i < carried.length; i++) {
                await printThing(carried[i]);
            }
        }

        let pouch = <Array<CoinPouch>> Thing.CarryRelation.getRightType(WorldState.player, CoinPouch);
        if (pouch.length > 0) {
            let p = document.createElement("p");
            p.classList.add("inventoryGold");
            let s : Say;
            if (pouch[0].getCoins() > 0) {
                s = new Say("There are " + pouch[0].getCoins().toString() + " coins in your ", pouch[0], ".");
            } else {
                s = new Say("There are no coins in your ", pouch[0], ".");
            }
            await s.getPureElements().then(elements => {
                elements.forEach(element => {
                    p.appendChild(element);
                });
            });
            container.appendChild(p);
        }
    }
}