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 Summation_mod;
var zad_zaliczone = 0;
(function (_Summation_mod) {
    var Summation_mod = (function (_super) {
        __extends(Summation_mod, _super);
        function Summation_mod(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
        }
        Summation_mod.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();
        };
        Summation_mod.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);
        };
        Summation_mod.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            EU.createFillElements(this);
            EU.createSelectableElements(this);
            EU.createStaticElements(this);
            this.createContainerElements();
            this.createDragableElements();
        };
        Summation_mod.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        Summation_mod.prototype.createDragableElements = function () {
            this.Dragables = [];
            for (var i = 0; i < this.Elements.DragableElements.el.length; i++) {
                var el = this.game.add.image(this.Elements.DragableElements.el[i].pos.x, this.Elements.DragableElements.el[i].pos.y, this.PREFIX + this.Elements.DragableElements.el[i].key);
                el.inputEnabled = true;
                el.input.enableDrag();
                el.events.onInputOver.add(this.onInputOver, this);
                el.events.onInputOut.add(this.onInputOut, this);
                el.events.onInputUp.add(this.onInputUp, this);
                el.name = this.PREFIX + this.Elements.DragableElements.el[i].key + i;
                this.Dragables.push({
                    "Element": el,
                    "Value": this.Elements.DragableElements.el[i].Value,
                    "StartPos": {
                        "x": this.Elements.DragableElements.el[i].pos.x,
                        "y": this.Elements.DragableElements.el[i].pos.y,
                    }
                });
                this.addChild(el);
            }
        };
        Summation_mod.prototype.createContainerElements = function () {
            this.Containers = [];
            for (var i = 0; i < this.Elements.ContainerElements.el.length; i++) {
                var el = this.game.add.image(this.Elements.ContainerElements.el[i].pos.x, this.Elements.ContainerElements.el[i].pos.y, this.PREFIX + this.Elements.ContainerElements.el[i].key);
                this.addChild(el);
                this.Containers.push({
                    "Value": this.Elements.ContainerElements.el[i].Value,
                    "Element": el
                });
            }
        };
        Summation_mod.prototype.onInputOver = function (el, pointer) {
            el.alpha = 0.7;
        };
        Summation_mod.prototype.onInputOut = function (el, pointer) {
            el.alpha = 1.0;
        };
        Summation_mod.prototype.onInputUp = function (el, pointer) {
            for (var i = 0; i < this.Containers.length; i++) {
                if (this.Overlap(el, this.Containers[i].Element)) {
                    this.checkIfEverythingIsCorrect();
                    return true;
                }
            }
            var tw = this.game.add.tween(el);
            tw.to({
                x: this.Dragables[this.getElementIndex(el, this.Dragables)].StartPos.x,
                y: this.Dragables[this.getElementIndex(el, this.Dragables)].StartPos.y
            }, 500, Phaser.Easing.Circular.InOut);
            tw.start();
            this.checkIfEverythingIsCorrect();
        };
        Summation_mod.prototype.getElementIndex = function (element, array) {
            for (var i = 0; i < array.length; i++) {
                if (array[i].Element.name == element.name)
                    return i;
            }
        };
        Summation_mod.prototype.Overlap = function (a, b) {
            var bA = a.getBounds();
            var bB = b.getBounds();
            return Phaser.Rectangle.intersects(bA, bB);
        };
		Summation_mod.prototype.nextScreen = function () {
			if (zad_zaliczone == 1){
				this.game.input.enabled = false;
				EU.killKeyboard();
				var tw = this.game.add.tween(this.game.camera);
				tw.to({ x: this.game.camera.x + Global.Main.dims.x }, 1500, Phaser.Easing.Exponential.Out);
				tw.onComplete.add(function () {
					this.game.input.enabled = true;
				}, this);
				tw.start();
				Global.Main.currentPageNumber++;
				Global.Main.returnPage(Global.Main.currentPageNumber).playLector();
				console.log(Global.Main.currentPageNumber);
			}
			else {
				//nie rób nic
			}
        };
        Summation_mod.prototype.checkIfEverythingIsCorrect = function () {
            for (var i = 0; i < this.Containers.length; i++) {
                var points = 0;
                for (var j = 0; j < this.Dragables.length; j++) {
                    if (this.Overlap(this.Dragables[j].Element, this.Containers[i].Element)) {
                        points += this.Dragables[j].Value;
                    }
                }
                if (this.Containers[i].Value != points) {
					zad_zaliczone = 0;
					console.log("zad_zaiczone = "+zad_zaliczone);
                    return false;
                }
            }
            if (EU.checkIfFillsAreCorrect(this) && EU.checkIfSelectablesAreCorrect(this)) {
                zad_zaliczone = 1;
				console.log("zad_zaliczone = "+zad_zaliczone);
				if (TYPE === "gm") {
					var next = this.game.add.button(1825 - 120 - 76, 730, "next", this.nextScreen, this, 0, 0, 1, 0);
					next.scale.setTo(0.8,0.8);
					this.addChild(next);
                    _super.prototype.nextTask.call(this);
                }
                return true;
            }
			zad_zaliczone = 0;
			console.log("zad_zaliczone = "+zad_zaliczone);
            return false;
        };
        return Summation_mod;
    })(Base.BasicGroup);
    _Summation_mod.Summation_mod = Summation_mod;
})(Summation_mod || (Summation_mod = {}));
//# sourceMappingURL=Summation_mod.js.map