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 Fill_five;
var game_fill = 0;
var game_fill_ten = 0;
(function (_Fill_five) {
    var Fill_five = (function (_super) {
        __extends(Fill_five, _super);
        function Fill_five(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
            this.HorizontalPlacementArray = [];
            this.VerticalPlacementArray = [];
            this.CaseSensitivity = true;
            this.isGrid = false;
            this.StartingPosX = 0;
            this.StartingPosY = 0;
            this.Columns = 0;
            this.Rows = 0;
            this.Spacing = 0;
            this.initializationKeys = "";
            this.maxCharacters = 0;
            this.FillList = [];
            this.AlwaysGood = false;
            this.inputBoxStyle = { font: "normal 80pt Arial", fill: "black" };
        }
        Fill_five.prototype.preload = function () {
            _super.prototype.preload.call(this);
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.loadGraphics();
            this.game.load.start();
        };
        Fill_five.prototype.killKeyborad = function () {
            if (this.keyboard != undefined) {
                this.keyboard.destroy(false, false);
                this.keyboard = undefined;
            }
        };
        Fill_five.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            this.loadInit();
            this.createStaticElements();
            this.createActiveElments();
            EU.createStaticSounds(this);
        };
        Fill_five.prototype.loadInit = function () {
            this.initializationKeys = this.GameData.AcceptedCharacters;
            this.maxCharacters = this.GameData.MaxCharacters;
            if (this.GameData.CaseSensitivity === true || this.GameData.CaseSensitivity === false) {
                this.CaseSensitivity = this.GameData.CaseSensitivity;
            }
            this.AlwaysGood = this.GameData.AlwaysGood;
            if (this.GameData.isGrid == true) {
                this.isGrid = this.GameData.isGrid;
                this.StartingPosX = this.GameData.StartingPos.x;
                this.StartingPosY = this.GameData.StartingPos.y;
                this.Columns = this.GameData.Columns;
                this.Rows = this.GameData.Rows;
                this.Spacing = this.GameData.Spacing;
            }
        };
        Fill_five.prototype.loadGraphics = function () {
            for (var i = 0; i < this.GameData.Graphics.length; i++)
                this.game.load.image(this.GameData.Graphics[i].key, this.GameData.Graphics[i].src);
        };
        Fill_five.prototype.createStaticElements = function () {
            if (this.GameData.CustomFontSize != -1) {
                this.fontSize = this.GameData.CustomFontSize;
                var LetterAmountmultipler = 70 * (this.maxCharacters / 3);
            }
            else {
                this.fontSize = 50 - (this.maxCharacters);
                var LetterAmountmultipler = 70 * (this.maxCharacters / 3);
            }
            this.inputBoxStyle.font = "normal " + this.fontSize + "pt Arial";
            if (this.isGrid) {
                var currHeight = this.StartingPosY;
                for (var i = 0; i < this.GameData.StaticElements.length; i++) {
                    var currWidth = this.StartingPosX;
                    if (this.GameData.StaticElements[i].HasImage) {
                        var el = this.game.add.image(currWidth, currHeight + (100 * i), this.GameData.StaticElements[i].key);
                        currWidth += el.width;
                        currHeight += el.height;
                        this.HorizontalPlacementArray.push(currWidth + this.Spacing);
                        this.VerticalPlacementArray.push(el.height / 2 + this.Spacing);
                        this.addChild(el);
                    }
                    if (this.GameData.StaticElements[i].HasText) {
                        var el_text = this.game.add.text(currWidth, currHeight + (100 * i), this.GameData.StaticElements[i].prefix, this.inputBoxStyle);
                        el_text.anchor.set(0, 0.5);
                        this.HorizontalPlacementArray.push(el_text.width + (LetterAmountmultipler / 2) + currWidth);
                        this.addChild(el_text);
                        el_text = this.game.add.text(this.HorizontalPlacementArray[i] + LetterAmountmultipler / 2, currHeight + (100 * i), this.GameData.StaticElements[i].sufix, this.inputBoxStyle);
                        el_text.anchor.set(0, 0.5);
                        currWidth += el_text.width;
                        this.VerticalPlacementArray.push(currHeight + (100 * i));
                        this.addChild(el_text);
                    }
                }
            }
            else {
                for (var i = 0; i < this.GameData.StaticElements.length; i++) {
                    if (this.GameData.StaticElements[i].HasImage) {
                        var tmptext = this.game.cache.getImage(this.GameData.StaticElements[i].key);
                        var el = this.game.add.image(this.GameData.StaticElements[i].pos.x + (tmptext.width / 2), this.GameData.StaticElements[i].pos.y + (tmptext.height / 2), this.GameData.StaticElements[i].key);
                        el.anchor.set(0.5, 0.5);
                        if (this.GameData.StaticElements[i].rotation) {
                            el.angle = this.GameData.StaticElements[i].rotation;
                        }
                        this.addChild(el);
                    }
                    if (this.GameData.StaticElements[i].HasText) {
                        var el_text = this.game.add.text(this.GameData.StaticElements[i].pos.x, this.GameData.StaticElements[i].pos.y, this.GameData.StaticElements[i].prefix, this.inputBoxStyle);
                        el_text.anchor.set(0, 0.5);
                        this.addChild(el_text);
                        el_text = this.game.add.text(this.GameData.StaticElements[i].pos.x, this.GameData.StaticElements[i].pos.y, this.GameData.StaticElements[i].sufix, this.inputBoxStyle);
                        el_text.anchor.set(0, 0.5);
                        this.addChild(el_text);
                    }
                }
            }
        };
        Fill_five.prototype.createActiveElments = function () {
            if (this.GameData.CustomFontSize != -1) {
                this.fontSize = this.GameData.CustomFontSize;
            }
            else {
                this.fontSize = 50 - (this.maxCharacters);
            }
            var BoxSize = 8 / 5 * this.fontSize;
            var LetterAmountmultipler = 70 * (this.maxCharacters / 3);
            if (this.GameData.SmallBox && this.GameData.SmallBox == true)
                LetterAmountmultipler = 60;
            this.inputBoxStyle.font = "normal " + this.fontSize + "pt Arial";
            var bmd = this.game.add.bitmapData(LetterAmountmultipler, BoxSize);
            bmd.ctx.rect(0, 0, LetterAmountmultipler, BoxSize);
            bmd.ctx.fillStyle = '#cdcdcd';
            bmd.ctx.fill();
            var currHeight = this.StartingPosY;
            
			if (this.isGrid){
                this.box_1 = this.game.add.sprite(this.HorizontalPlacementArray[0], this.VerticalPlacementArray[0], bmd);
			}
            else {
                this.box_1 = this.game.add.sprite(this.GameData.TaskList[0].pos.x, this.GameData.TaskList[0].pos.y, bmd);
			}
			this.box_1.anchor.set(0.5, 0.5);
            this.box_1.alpha = 0.5;
            this.box_1.width = LetterAmountmultipler;
            this.item_1 = this.game.add.text(0, 0, "?", this.inputBoxStyle);
            this.item_1.anchor.set(0.5, 0.5);
            this.item_1.father = this.box_1;
            this.item_1.x = this.box_1.x;
            this.item_1.y = this.box_1.y;
            this.addChild(this.item_1);
            this.box_1.text = this.item_1;
            this.item_1.answer = this.GameData.TaskList[0].Answer;
            this.box_1.inputEnabled = true;
            this.box_1.events.onInputUp.add(this.showKeyboard, this);
            this.addChild(this.box_1);
            this.FillList.push(this.item_1);
			
			if (this.isGrid){
                this.box_2 = this.game.add.sprite(this.HorizontalPlacementArray[1], this.VerticalPlacementArray[1], bmd);
			}
            else {
                this.box_2 = this.game.add.sprite(this.GameData.TaskList[1].pos.x, this.GameData.TaskList[1].pos.y, bmd);
			}
			this.box_2.anchor.set(0.5, 0.5);
            this.box_2.alpha = 0.5;
            this.box_2.width = LetterAmountmultipler;
            this.item_2 = this.game.add.text(0, 0, "?", this.inputBoxStyle);
            this.item_2.anchor.set(0.5, 0.5);
            this.item_2.father = this.box_2;
            this.item_2.x = this.box_2.x;
            this.item_2.y = this.box_2.y;
            this.addChild(this.item_2);
            this.box_2.text = this.item_2;
            this.item_2.answer = this.GameData.TaskList[1].Answer;
            this.box_2.inputEnabled = true;
            this.box_2.events.onInputUp.add(this.showKeyboard, this);
            this.addChild(this.box_2);
            this.FillList.push(this.item_2);
			
			if (this.isGrid){
                this.box_3 = this.game.add.sprite(this.HorizontalPlacementArray[2], this.VerticalPlacementArray[2], bmd);
			}
            else {
                this.box_3 = this.game.add.sprite(this.GameData.TaskList[2].pos.x, this.GameData.TaskList[2].pos.y, bmd);
			}
			this.box_3.anchor.set(0.5, 0.5);
            this.box_3.alpha = 0.5;
            this.box_3.width = LetterAmountmultipler;
            this.item_3 = this.game.add.text(0, 0, "?", this.inputBoxStyle);
            this.item_3.anchor.set(0.5, 0.5);
            this.item_3.father = this.box_3;
            this.item_3.x = this.box_3.x;
            this.item_3.y = this.box_3.y;
            this.addChild(this.item_3);
            this.box_3.text = this.item_3;
            this.item_3.answer = this.GameData.TaskList[2].Answer;
            this.box_3.inputEnabled = true;
            this.box_3.events.onInputUp.add(this.showKeyboard, this);
            this.addChild(this.box_3);
            this.FillList.push(this.item_3);
			
			if (this.isGrid){
                this.box_4 = this.game.add.sprite(this.HorizontalPlacementArray[3], this.VerticalPlacementArray[3], bmd);
			}
            else {
                this.box_4 = this.game.add.sprite(this.GameData.TaskList[3].pos.x, this.GameData.TaskList[3].pos.y, bmd);
			}
			this.box_4.anchor.set(0.5, 0.5);
            this.box_4.alpha = 0.5;
            this.box_4.width = LetterAmountmultipler;
            this.item_4 = this.game.add.text(0, 0, "?", this.inputBoxStyle);
            this.item_4.anchor.set(0.5, 0.5);
            this.item_4.father = this.box_4;
            this.item_4.x = this.box_4.x;
            this.item_4.y = this.box_4.y;
            this.addChild(this.item_4);
            this.box_4.text = this.item_4;
            this.item_4.answer = this.GameData.TaskList[3].Answer;
            this.box_4.inputEnabled = true;
            this.box_4.events.onInputUp.add(this.showKeyboard, this);
            this.addChild(this.box_4);
            this.FillList.push(this.item_4);
			
			if (this.isGrid){
                this.box_5 = this.game.add.sprite(this.HorizontalPlacementArray[4], this.VerticalPlacementArray[4], bmd);
			}
            else {
                this.box_5 = this.game.add.sprite(this.GameData.TaskList[4].pos.x, this.GameData.TaskList[4].pos.y, bmd);
			}
			this.box_5.anchor.set(0.5, 0.5);
            this.box_5.alpha = 0.5;
            this.box_5.width = LetterAmountmultipler;
            this.item_5 = this.game.add.text(0, 0, "?", this.inputBoxStyle);
            this.item_5.anchor.set(0.5, 0.5);
            this.item_5.father = this.box_5;
            this.item_5.x = this.box_5.x;
            this.item_5.y = this.box_5.y;
            this.addChild(this.item_5);
            this.box_5.text = this.item_5;
            this.item_5.answer = this.GameData.TaskList[4].Answer;
            this.box_5.inputEnabled = true;
            this.box_5.events.onInputUp.add(this.showKeyboard, this);
            this.addChild(this.box_5);
            this.FillList.push(this.item_5);
        };
        Fill_five.prototype.keyboardInit = function () {
            this.keyboard = new KB.Keyboard(this.game, this.initializationKeys, 500, 0, true);
            if (this.initializationKeys.length >= 2) {
                EU.Log(this.keyboard);
                if (this.initializationKeys == "numbersAndChars") {
                    this.keyboard.initialize("1234567890" + this.keyboard.arrSeparator + "qwertyuiop+-" + this.keyboard.arrSeparator + "asdfghjkl:;" + this.keyboard.arrSeparator + "zxcvbnm^& .,", "key", "key");
                }
                else {
                    this.keyboard.initialize(this.initializationKeys, "key", "key");
                }
            }
            else {
                this.keyboard.initialize("qwertyuiop+-" + this.keyboard.arrSeparator + "asdfghjkl" + this.keyboard.arrSeparator + "zxcvbnm^& .,", "key", "key");
            }
            this.keyboard.scale.setTo(0.5, 0.5);
            this.keyboard.alive = false;
            this.keyboard.fixedToCamera = true;
            this.keyboard.maxLetters = this.maxCharacters;
            this.game.add.existing(this.keyboard);
        };
        Fill_five.prototype.showKeyboard = function (button) {
			game_fill_ten = 0;
			game_fill = 1;
            if (this.keyboard == undefined)
                this.keyboardInit();
            this.keyboard.controller = button.text;
            this.keyboard.RootController = this;
            this.keyboard.navClick(this.keyboard);
        };
        Fill_five.prototype.checkComplition = function () {
            var TotalAnswers = this.GameData.TaskList.length;
            var GoodAnswers = 0;
            var BadAnswers = 0;
            for (var i = 0; i < this.GameData.TaskList.length; i++) {
                if (this.AlwaysGood) {
                    if (this.FillList[i].text.toUpperCase() != "?") {
                        GoodAnswers++;
                    }
                }
                else {
                    if (this.CaseSensitivity) {
                        if (this.GameData.TaskList[i].MultipleAnswers) {
                            var istrue = false;
                            for (var j = 0; j < this.GameData.TaskList[i].Answer.length; j++) {
                                if (this.FillList[i].text === this.GameData.TaskList[i].Answer[j]) {
                                    istrue = true;
                                }
                                else {
                                    if (this.FillList[i].text.toUpperCase() != "?" && !istrue)
                                        istrue = false;
                                }
                            }
                            if (istrue) {
                                GoodAnswers++;
                            }
                            else {
                                BadAnswers++;
                            }
                        }
                        else {
                            if (this.FillList[i].text === this.GameData.TaskList[i].Answer || this.GameData.TaskList[i].Answer === "") {
                                if (this.GameData.CorrectSound) {
                                    Global.Main.PlayMusic(new Audio('Sounds/win.mp3'));
                                }
                                GoodAnswers++;
                            }
                            else {
                                if (this.FillList[i].text.toUpperCase() != "?")
                                    if (this.GameData.CorrectSound) {
                                        Global.Main.PlayMusic(new Audio('Sounds/err.mp3'));
                                    }
                                BadAnswers++;
                            }
                        }
                    }
                    else {
                        if (this.GameData.TaskList[i].MultipleAnswers) {
                            var istrue = false;
                            for (var j = 0; j < this.GameData.TaskList[i].Answer.length; j++) {
                                if (this.FillList[i].text.toUpperCase() === this.GameData.TaskList[i].Answer[j].toUpperCase()) {
                                    istrue = true;
                                }
                                else {
                                    if (this.FillList[i].text.toUpperCase() != "?" && !istrue)
                                        istrue = false;
                                }
                            }
                            if (istrue) {
                                if (this.GameData.CorrectSound) {
                                    Global.Main.PlayMusic(new Audio('Sounds/win.mp3'));
                                }
                                GoodAnswers++;
                            }
                            else {
                                if (this.GameData.CorrectSound) {
                                    Global.Main.PlayMusic(new Audio('Sounds/err.mp3'));
                                }
                                BadAnswers++;
                            }
                        }
                        else {
                            if (this.FillList[i].text.toUpperCase() === this.GameData.TaskList[i].Answer.toUpperCase() || this.GameData.TaskList[i].Answer === "") {
                                if (this.GameData.CorrectSound) {
                                    Global.Main.PlayMusic(new Audio('Sounds/win.mp3'));
                                }
                                GoodAnswers++;
                            }
                            else {
                                if (this.FillList[i].text.toUpperCase() != "?")
                                    if (this.GameData.CorrectSound) {
                                        Global.Main.PlayMusic(new Audio('Sounds/err.mp3'));
                                    }
                                BadAnswers++;
                            }
                        }
                    }
                }
            }
            if (TotalAnswers == GoodAnswers && (this.FillList[1].text !== this.FillList[2].text)) {
                EU.setGameCompletion(this.parent, 1);
                if (TYPE === "gm") {
                    _super.prototype.nextTask.call(this);
                }
            }
            else {
                var complition = GoodAnswers / TotalAnswers;
                var tmpCheck = BadAnswers / TotalAnswers;
                EU.Log(complition + " + " + tmpCheck + " = " + (tmpCheck + complition));
                if (tmpCheck + complition != 1) {
                    EU.Log("zaznacz wszystkie ocje");
                }
                else {
                    EU.setGameCompletion(this.parent, 0);
                }
            }
            this.killKeyborad();
        };
        Fill_five.prototype.update = function () {
        };
        Fill_five.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.GameData = json.GameData;
        };
        return Fill_five;
    })(Base.BasicGroup);
    _Fill_five.Fill_five = Fill_five;
})(Fill_five || (Fill_five = {}));
//# sourceMappingURL=Fill_five.js.map