/// <reference path="../Say.ts" />
/// <reference path="../../../Controls/Modules/KeyHandler.ts" />
/// <reference path="../../../World/MachineBegins.ts" />
class SayImage implements PrintableElement {
    public static imageNames : Array<SayImage> = [];
    public static loadTimeCheck : boolean = false;
    public static imageViewer : HTMLElement = <HTMLElement> document.getElementById("imageViewer");
    private imgName : string;

    private isImageDefined () {
        try {
            for(var i = 0; i < document.styleSheets.length; i++) {
                var rules = document.styleSheets[i]['rules'] || document.styleSheets[i]['cssRules'];
                for(var x in rules) {
                    if(typeof rules[x].selectorText == 'string' && rules[x].selectorText == "." + this.imgName) {
                        return true;
                    }
                }
            }
            return false;
        } catch (e) {
            console.warn("Can't check for undefined images.");
            return true;
        }
    }

    public constructor (imgName : string) {
        this.imgName = imgName;

        if (!SayImage.loadTimeCheck && SayImage.imageNames.indexOf(this) == -1) {
            SayImage.imageNames.push(this);
        }
    }

    public getImageElement () : Element {
        let img = document.createElement("div");
        if (this.isImageDefined()) {
            img.classList.add(this.imgName);
            img.classList.add("contentImage");
        } else {
            img.classList.add("error");
            img.appendChild(document.createTextNode("Image \"" + this.imgName + "\" not found."));
        }

        img.addEventListener("click", () => {
            SayImage.showInViewer(this);
        });

        Controls.KeyHandler.applyCode(img, Controls.KeyHandler.imageKeyCode.getValue());

        return img;
    }

    public getPrintedElement () {
        return [this.getImageElement()];
    }

    public static doLoadTimeCheck() {
        for (let i = 0; i < SayImage.imageNames.length; i++) {
            let image = SayImage.imageNames[i];
            if (!image.isImageDefined()) {
                if (Settings.hardDebug) {
                    Elements.CurrentTurnHandler.printAsError("Image \"" + image.imgName + "\" was not found.");
                }
                console.error("Image \"" + image.imgName + "\" was not found.");
            }
        }
        SayImage.loadTimeCheck = true;
    }

    public static showInViewer (image : SayImage) {
        if (!(SayImage.imageViewer.style.display == 'block')) {
            SayImage.imageViewer.addEventListener("click", () => {
                SayImage.imageViewer.style.display = "none";
            });
            SayImage.imageViewer.className = image.imgName;
            SayImage.imageViewer.style.display = "block";
        } else {
            SayImage.imageViewer.style.display = "none";
        }

    }
}

module MachineBegins {
    export let ImageLoadTimeCheck = MachineBegins.rulebook.createAndAddRule({
        firstPriority : Rule.PRIORITY_HIGHEST, // We want this to print to the top of the console
        name : "Check for undefined images",
        code : () => {
            try {
                SayImage.doLoadTimeCheck();
            } catch (e) {
                console.warn("Can't check for undefined images.");
            }
        }
    });
}