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 TrueOrFalse;
(function (_TrueOrFalse) {
    var TrueOrFalse = (function (_super) {
        __extends(TrueOrFalse, _super);
        function TrueOrFalse(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
        }
        TrueOrFalse.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();
        };
        TrueOrFalse.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);
        };
        TrueOrFalse.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            if (this.json.isFirst == true) {
                EU.loadVocabulary(this);
            }
            ;
            EU.createStaticElements(this);
            EU.createSelectableElements(this);
            EU.createFillElements(this);
            this.createQuestion();
            this.createAnswers();
            this.isCorrect = "isFalse";
        };
        TrueOrFalse.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        TrueOrFalse.prototype.createQuestion = function () {
            var question;
            var flag = false;
            while (flag == false) {
                flag = true;
                this.categoryIndex = Math.floor((Math.random() * EU.groups.length));
                this.wordIndex = Math.floor((Math.random() * EU.groups[this.categoryIndex].vocabulary.length));
                EU.Log(this.categoryIndex + " " + this.wordIndex);
                question = EU.groups[this.categoryIndex].vocabulary[this.wordIndex].en;
                for (var i = 0; i < EU.usedWords.length; i++) {
                    if (EU.usedWords[i] == question) {
                        flag = false;
                        break;
                    }
                    flag = true;
                }
            }
            EU.usedWords.push(question);
            this.Qtext = this.game.add.text(0, 0, question, this.Elements.Style);
            var posX = this.Elements.Position.x;
            var posY = this.Elements.Position.y;
            if (this.Elements.Just) {
                switch (this.Elements.Just.x) {
                    case "center":
                        posX += (Global.Main.mainContainerDims.wd - 120) / 2;
                        this.Qtext.anchor.x = 0.5;
                        break;
                    case "right":
                        posX += Global.Main.mainContainerDims.wd - 120;
                        this.Qtext.anchor.x = 1;
                        break;
                }
                switch (this.Elements.Just.y) {
                    case "center":
                        posY += (Global.Main.mainContainerDims.hg - 65) / 2;
                        this.Qtext.anchor.y = 0.5;
                        break;
                    case "bottom":
                        posY += Global.Main.mainContainerDims.hg - 65;
                        this.Qtext.anchor.y = 1;
                        break;
                }
            }
            this.Qtext.position.set(posX, posY);
            this.addChild(this.Qtext);
        };
        TrueOrFalse.prototype.createAnswers = function () {
            this.usedAnswers = [];
            this.usedAnswers.push({
                "answer": EU.groups[this.categoryIndex].vocabulary[this.wordIndex].pl,
                "name": null
            });
            var answer = { "answer": null, "name": null };
            var flag = false;
            while (flag == false) {
                var answerIndex = this.wordIndex = Math.floor((Math.random() * EU.groups[this.categoryIndex].vocabulary.length));
                answer.answer = EU.groups[this.categoryIndex].vocabulary[answerIndex].pl;
                for (var j = 0; j < this.usedAnswers.length; j++) {
                    if (this.usedAnswers[j].answer == answer.answer) {
                        flag = false;
                        break;
                    }
                    flag = true;
                }
            }
            answer.name = "isFalse";
            this.usedAnswers.push(answer);
            this.usedAnswers[0].name = "isTrue";
            this.usedAnswers = EU.shuffleArray(this.usedAnswers);
            this.textAnswers = [];
            var el = this.game.add.text(0, 0, this.usedAnswers[0].answer, this.Elements.AnswerStyle);
            el.position.set(this.Qtext.position.x, this.Qtext.position.y + 1.5 * this.Qtext.height);
            el.anchor = this.Qtext.anchor;
            el.name = this.usedAnswers[0].name;
            var True = this.game.add.text(this.Qtext.position.x - (this.Qtext.anchor.x * this.Qtext.height * 3), this.Qtext.position.y + 3 * this.Qtext.height, "True", this.Elements.AnswerStyle);
            True.anchor = this.Qtext.anchor;
            True.setStyle({ fill: "green", font: this.Elements.AnswerStyle.font });
            this.addChild(True);
            var False = this.game.add.text(this.Qtext.position.x + ((1 - this.Qtext.anchor.x) * this.Qtext.height * 3), this.Qtext.position.y + 3 * this.Qtext.height, "False", this.Elements.AnswerStyle);
            False.anchor = this.Qtext.anchor;
            False.setStyle({ fill: "red", font: this.Elements.AnswerStyle.font });
            this.addChild(False);
            True.inputEnabled = true;
            True.events.onInputOver.add(this.onInputOver, this);
            True.events.onInputOut.add(this.onInputOut, this);
            True.events.onInputDown.add(this.onInputDown, this);
            False.inputEnabled = true;
            False.events.onInputOver.add(this.onInputOver, this);
            False.events.onInputOut.add(this.onInputOut, this);
            False.events.onInputDown.add(this.onInputDown, this);
            if (el.name == "isTrue") {
                True.name = "isTrue";
                False.name = "isFalse";
            }
            else {
                True.name = "isFalse";
                False.name = "isTrue";
            }
            this.textAnswers.push(True);
            this.textAnswers.push(False);
            this.addChild(el);
        };
        TrueOrFalse.prototype.onInputOver = function (el, pointer) {
            el.alpha = 0.5;
        };
        TrueOrFalse.prototype.onInputOut = function (el, pointer) {
            el.alpha = 1.0;
        };
        TrueOrFalse.prototype.clearAnswers = function () {
            for (var i = 0; i < this.textAnswers.length; i++) {
                this.textAnswers[i].removeChildren();
            }
        };
        TrueOrFalse.prototype.onInputDown = function (el, pointer) {
            this.clearAnswers();
            var rect = EU.drawRect("blue", el.width + 10, el.height, el.height / 10, el.height / 10, this);
            rect.position.x = -el.width / 2 - 5;
            el.addChild(rect);
            this.isCorrect = el.name;
            this.checkIfEverythingIsCorrect();
        };
        TrueOrFalse.prototype.checkIfEverythingIsCorrect = function () {
            if (this.isCorrect == "isTrue") {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("Correct!!!");
                return true;
            }
            ;
            EU.setGameCompletion(this.parent, 0);
            EU.Log("NotCorrect!!! :<");
            return false;
        };
        return TrueOrFalse;
    })(Base.BasicGroup);
    _TrueOrFalse.TrueOrFalse = TrueOrFalse;
})(TrueOrFalse || (TrueOrFalse = {}));
//# sourceMappingURL=TrueOrFalse.js.map