/// <reference path="../World/Classes/Save/StoredVariable.ts" />
class Perk extends StoredVariable<boolean> {
    public forcedStatus : ((e : Perk) => boolean | void) = () => { return undefined };
    public description : Say | string | ((perk : Perk) => Say | string) = "Undefined";
    public name : string;
    public confirmPicked : (() => void) = () => {};

    constructor (id : string) {
        super({
            id : "Perk_" + id,
            value : false
        });
        this.name = id;

        Perk.storePerk(this);
    }

    public isEnabled (valueOnly? : boolean) : boolean {
        if (valueOnly != true) {
            let forced = this.forcedStatus(this);
            if (forced != undefined) {
                return <boolean> forced;
            }
        }
        return this.value;
    }

    public isForced () {
        return this.forcedStatus(this) != undefined;
    }

    public getDescription () {
        if (typeof this.description == "function") {
            return this.description(this);
        } else {
            return this.description;
        }
    }

    public static perks : {[id : string] : Perk} = {};

    public static storePerk (perk : Perk) {
        Perk.perks[perk.id] = perk;
    }

    public static getPerk (id : string) {
        return Perk.perks[id];
    }

    public static getPerks () : Array<Perk> {
        let perks = [];
        for (let id in Perk.perks) {
            perks.push(Perk.perks[id]);
        }
        perks.sort((a : Perk, b : Perk) => {
            let na = a.name.toUpperCase();
            let nb = b.name.toUpperCase();
            if (na < nb) return -1;
            if (na > nb) return 1;
            return 0;
        });
        return perks;
    }

    public static updatePerks() {
        for (let id in Perk.perks) {
            let perk = Perk.perks[id];
            if (perk.isForced()) {
                perk.value = <boolean> perk.forcedStatus(perk);
            }
        }
    }
}