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 Summation2;
(function (_Summation2) {
    var Summation2 = (function (_super) {
        __extends(Summation2, _super);
        function Summation2(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
        }
        Summation2.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();
        };
        Summation2.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);
        };
        Summation2.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();
            this.createInfo();
        };
        Summation2.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        Summation2.prototype.createInfo = function () {
            this.info = this.game.add.text(this.Elements.InfoPos.x, this.Elements.InfoPos.y, "start", this.Elements.Style);
            this.info.anchor.set(0.5, 0.5);
            this.info.visible = false;
            this.addChild(this.info);
            if (this.Elements.WordWrap) {
                this.info.wordWrap = true;
                this.info.wordWrapWidth = this.Elements.WordWrap;
            }
            this.info.inputEnabled = true;
            this.info.input.enableDrag();
            this.info.events.onInputDown.add(this.onInputDownD, this);
        };
        Summation2.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,
                    "Group": this.Elements.DragableElements.el[i].group,
                    "StartPos": {
                        "x": this.Elements.DragableElements.el[i].pos.x,
                        "y": this.Elements.DragableElements.el[i].pos.y,
                    }
                });
                this.addChild(el);
            }
        };
        Summation2.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,
                    "Texts": this.Elements.ContainerElements.el[i].Texts,
                    "Element": el
                });
            }
        };
        Summation2.prototype.onInputOver = function (el, pointer) {
            el.alpha = 0.7;
        };
        Summation2.prototype.onInputOut = function (el, pointer) {
            el.alpha = 1.0;
        };
        Summation2.prototype.onInputDownD = function (el, pointer) {
            EU.Log("Correct!!!");
            EU.setGameCompletion(this.parent, 1);
            _super.prototype.nextTask.call(this);
        };
        Summation2.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();
        };
        Summation2.prototype.getElementIndex = function (element, array) {
            for (var i = 0; i < array.length; i++) {
                if (array[i].Element.name == element.name)
                    return i;
            }
        };
        Summation2.prototype.Overlap = function (a, b) {
            var bA = a.getBounds();
            var bB = b.getBounds();
            return Phaser.Rectangle.intersects(bA, bB);
        };
        Summation2.prototype.checkIfGroupsCorrect = function () {
            var temp = [];
            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)) {
                        var contains = true;
                        if (temp.length == 0)
                            temp.push(this.Dragables[j].Group);
                        for (var k = 0; k < temp.length; k++) {
                            if (temp[k] == this.Dragables[j].Group) {
                                contains = true;
                                break;
                            }
                            contains = false;
                        }
                        if (contains == false)
                            temp.push(this.Dragables[j].Group);
                    }
                }
            }
            EU.Log(temp.length);
            if (temp.length == this.Elements.ForceGroups) {
                EU.Log("Correct Groups");
                return true;
            }
            else {
                EU.Log("NOT cor Groups");
                return false;
            }
        };
        Summation2.prototype.checkIfEverythingIsCorrect = function () {
            var dragged = 0;
            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;
                        dragged++;
                    }
                }
                this.curPoints = points;
                if (this.checkIfGroupsCorrect()) {
                    this.info.visible = true;
                    for (var j = 0; j < this.Containers[i].Value.length; j++) {
                        if (points <= this.Containers[i].Value[j]) {
                            this.info.text = this.Containers[i].Texts[j].pre;
                            if (j != 0) {
                                this.info.text += (points - this.Containers[i].Value[j - 1]) + this.Containers[i].Texts[j].su;
                                if (this.Containers[i].Texts[j].multi) {
                                    this.info.text = this.Containers[i].Texts[j].pre + (this.Containers[i].Texts[j].multi * Math.ceil(points - this.Containers[i].Value[j - 1])) + this.Containers[i].Texts[j].su;
                                }
                            }
                            break;
                        }
                        this.info.text = this.Containers[i].Texts[this.Containers[i].Value.length].pre + (Math.round(10 * (points - this.Containers[i].Value[j - 1])) / 10) + this.Containers[i].Texts[this.Containers[i].Value.length].su;
                    }
                }
                else {
                    this.info.visible = false;
                }
            }
            if (this.checkIfGroupsCorrect()) {
                EU.Log("Correct!!!");
                EU.setGameCompletion(this.parent, 1);
                return true;
            }
            EU.Log("NotCorrect:<");
            EU.setGameCompletion(this.parent, 0);
            return false;
        };
        return Summation2;
    })(Base.BasicGroup);
    _Summation2.Summation2 = Summation2;
})(Summation2 || (Summation2 = {}));
//# sourceMappingURL=Summation2.js.map