var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var SearchText;
(function (_SearchText) {
    var SearchText = (function (_super) {
        __extends(SearchText, _super);
        function SearchText(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
        }
        SearchText.prototype.preload = function () {
            _super.prototype.preload.call(this);
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.PREFIX = this.Elements.TaskPrefix;
            this.loadGraphics();
            this.game.load.start();
        };
        SearchText.prototype.loadGraphics = function () {
            for (var i = 0; i < this.Elements.Graphics.length; i++)
                this.game.load.image(this.PREFIX + this.Elements.Graphics[i].key, this.Elements.Graphics[i].src);
        };
        SearchText.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            this.createStaticElements();
            this.numberOfChecked = 0;
            this.createSourceText();
            this.createTextToFind();
        };
        SearchText.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        SearchText.prototype.createStaticElements = function () {
            for (var i = 0; i < this.Elements.StaticElements.length; i++) {
                var el = this.game.add.image(this.Elements.StaticElements[i].pos.x, this.Elements.StaticElements[i].pos.y, this.PREFIX + this.Elements.StaticElements[i].key);
                this.addChild(el);
            }
        };
        SearchText.prototype.createTextToFind = function () {
            for (var k = 0; k < this.Elements.SourceText.length; k++) {
                this.SourceText[k].toFind = this.Elements.SourceText[k].TextToFind;
            }
        };
        SearchText.prototype.createSourceText = function () {
            this.SourceText = [];
            for (var k = 0; k < this.Elements.SourceText.length; k++) {
                var Text = [];
                var verses = this.Elements.SourceText[k].Text.split("@");
                var posX = this.Elements.SourceText[k].Position.x;
                var posY = this.Elements.SourceText[k].Position.y;
                var style = this.Elements.SourceText[k].Style;
                var counter = 0;
                for (var i = 0; i < verses.length; i++) {
                    var words = verses[i].split("");
                    words.push(' ');
                    counter++;
                    var el = this.game.add.text(0, 0, words[0], style);
                    var versePosY = posY + i * el.height;
                    el.position.set(posX, versePosY);
                    el.inputEnabled = true;
                    el.events.onInputOver.add(this.onInputOver, this);
                    el.events.onInputOut.add(this.onInputOut, this);
                    el.events.onInputDown.add(this.onWordClicked, this);
                    Text.push(el);
                    this.addChild(el);
                    for (var j = 1; j < words.length; j++) {
                        el = this.game.add.text(0, 0, words[j], style);
                        el.position.set(Text[counter - 1].position.x + Text[counter - 1].width, versePosY);
                        el.inputEnabled = true;
                        el.events.onInputOver.add(this.onInputOver, this);
                        el.events.onInputOut.add(this.onInputOut, this);
                        el.events.onInputDown.add(this.onWordClicked, this);
                        Text.push(el);
                        this.addChild(el);
                        counter++;
                    }
                }
                this.SourceText.push({ "Text": Text, "toFind": null });
            }
        };
        SearchText.prototype.onInputOver = function (el, pointer) {
            el.alpha = 0.5;
        };
        SearchText.prototype.onInputOut = function (el, pointer) {
            el.alpha = 1.0;
        };
        SearchText.prototype.onWordClicked = function (el, pointer) {
            if (el.text != " ") {
                if (el.name == "checked") {
                    el.setShadow(0, 0, "black");
                    this.numberOfChecked--;
                    el.name = "unchecked";
                }
                else {
                    el.setShadow(el.height / 25, el.height / 25, "red");
                    this.numberOfChecked++;
                    el.name = "checked";
                }
                this.checkIfEverythingIsCorrect();
            }
        };
        SearchText.prototype.findIndexes = function (source, toFind) {
            var result = [];
            for (var i = 0; i < source.length; i++) {
                if (source.substring(i, i + toFind.length) == toFind) {
                    result.push(i);
                }
            }
            return result;
        };
        SearchText.prototype.markCorrectChars = function () {
            for (var i = 0; i < this.Elements.SourceText.length; i++) {
                for (var j = 0; j < this.SourceText[i].toFind.length; j++) {
                    var indexes = this.findIndexes(this.Elements.SourceText[i].Text, this.SourceText[i].toFind[j]);
                    for (var k = 0; k < indexes.length; k++) {
                        for (var l = 0; l < this.SourceText[i].toFind[j].length; l++) {
                            this.SourceText[i].Text[indexes[k] + l].setShadow(1, 1, "red");
                        }
                    }
                }
            }
        };
        SearchText.prototype.checkIfEverythingIsCorrect = function () {
            var srt = "Points: " + this.calculatePlayerPoints() + "/" + this.calculateMaxPoints();
            EU.Log(srt);
            EU.Log("Checked: " + this.numberOfChecked + "/" + this.numberOfCorrectChars);
            if (this.calculatePlayerPoints() == this.calculateMaxPoints() && this.numberOfChecked == this.numberOfCorrectChars) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("Correct!!!");
                return true;
            }
            EU.setGameCompletion(this.parent, 0);
            return false;
        };
        SearchText.prototype.calculateMaxPoints = function () {
            this.numberOfCorrectChars = 0;
            var points = 0;
            for (var i = 0; i < this.Elements.SourceText.length; i++) {
                for (var j = 0; j < this.SourceText[i].toFind.length; j++) {
                    var indexes = this.findIndexes(this.Elements.SourceText[i].Text, this.SourceText[i].toFind[j]).length;
                    points += indexes;
                    this.numberOfCorrectChars += indexes * this.SourceText[i].toFind[j].length;
                }
            }
            return points;
        };
        SearchText.prototype.calculatePlayerPoints = function () {
            var points = 0;
            for (var i = 0; i < this.Elements.SourceText.length; i++) {
                for (var j = 0; j < this.SourceText[i].toFind.length; j++) {
                    var indexes = this.findIndexes(this.Elements.SourceText[i].Text, this.SourceText[i].toFind[j]);
                    for (var k = 0; k < indexes.length; k++) {
                        var allCharChecked = true;
                        for (var l = 0; l < this.SourceText[i].toFind[j].length; l++) {
                            if (this.SourceText[i].Text[indexes[k] + l].name != "checked") {
                                allCharChecked = false;
                            }
                        }
                        if (allCharChecked)
                            points++;
                    }
                }
            }
            return points;
        };
        return SearchText;
    })(Base.BasicGroup);
    _SearchText.SearchText = SearchText;
})(SearchText || (SearchText = {}));
//# sourceMappingURL=SearchText.js.map