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 Syllabify;
(function (_Syllabify) {
    var Syllabify = (function (_super) {
        __extends(Syllabify, _super);
        function Syllabify(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
        }
        Syllabify.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();
        };
        Syllabify.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);
        };
        Syllabify.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            EU.createStaticElements(this);
            EU.createSelectableElements(this);
            EU.createFillElements(this);
            this.createSyllabifyElements();
            this.numberOfChecked = 0;
        };
        Syllabify.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        Syllabify.prototype.createSyllabifyElements = function () {
            this.Words = [];
            for (var i = 0; i < this.Elements.SyllabifyElements.el.length; i++) {
                var posX = this.Elements.SyllabifyElements.el[i].pos.x;
                var posY = this.Elements.SyllabifyElements.el[i].pos.y;
                var Lines = [];
                for (var j = 0; j < this.Elements.SyllabifyElements.el[i].text.length - 1; j++) {
                    var el = this.game.add.text(posX, posY, this.Elements.SyllabifyElements.el[i].text[j], this.Elements.SyllabifyElements.Options.Style);
                    posX += el.width + 10;
                    var line = EU.drawLine("red", posX - 5, posY, posX - 5, posY + el.height, this, el.height / 10);
                    line.alpha = 0.2;
                    line.inputEnabled = true;
                    line.name = "unchecked";
                    line.events.onInputDown.add(this.onLineClicked, this);
                    this.addChild(line);
                    this.addChild(el);
                    Lines.push(line);
                }
                var el = this.game.add.text(posX, posY, this.Elements.SyllabifyElements.el[i].text[this.Elements.SyllabifyElements.el[i].text.length - 1], this.Elements.SyllabifyElements.Options.Style);
                this.addChild(el);
                this.Words.push(Lines);
            }
        };
        Syllabify.prototype.onLineClicked = function (el, pointer) {
            if (el.name == "unchecked") {
                el.name = "checked";
                el.alpha = 1;
                this.numberOfChecked++;
            }
            else {
                el.name = "unchecked";
                el.alpha = 0.2;
                this.numberOfChecked--;
            }
            this.checkIfEverythingIsCorrect();
        };
        Syllabify.prototype.checkIfLinesAreCorrect = function () {
            for (var i = 0; i < this.Elements.SyllabifyElements.el.length; i++) {
                for (var j = 0; j < this.Elements.SyllabifyElements.el[i].answer.length; j++) {
                    var correct = false;
                    for (var k = 0; k < this.Elements.SyllabifyElements.el[i].answer[j].length; k++) {
                        if (this.Words[i][this.Elements.SyllabifyElements.el[i].answer[j][k]].name == "checked") {
                            correct = true;
                            break;
                        }
                    }
                    if (correct == false)
                        return false;
                }
            }
            return true;
        };
        Syllabify.prototype.calculateMaxChecked = function () {
            var points = 0;
            for (var i = 0; i < this.Elements.SyllabifyElements.el.length; i++) {
                points += this.Elements.SyllabifyElements.el[i].answer.length;
            }
            return points;
        };
        Syllabify.prototype.checkIfEverythingIsCorrect = function () {
            EU.Log("Selectables: " + EU.checkIfSelectablesAreCorrect(this));
            EU.Log("Fills: " + EU.checkIfFillsAreCorrect(this));
            if (EU.checkIfSelectablesAreCorrect(this) && EU.checkIfFillsAreCorrect(this) && this.checkIfLinesAreCorrect() && this.numberOfChecked == this.calculateMaxChecked()) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("Correct!!!");
                return true;
            }
            EU.setGameCompletion(this.parent, 0);
            return false;
        };
        return Syllabify;
    })(Base.BasicGroup);
    _Syllabify.Syllabify = Syllabify;
})(Syllabify || (Syllabify = {}));
//# sourceMappingURL=Syllabify.js.map