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 DragDrop;
(function (_DragDrop) {
    var Types;
    (function (Types) {
        Types[Types["OneToMany"] = 0] = "OneToMany";
        Types[Types["ManyToOne"] = 1] = "ManyToOne";
        Types[Types["OneToOne"] = 2] = "OneToOne";
        Types[Types["BigContainer"] = 3] = "BigContainer";
    })(Types || (Types = {}));
    var DragDrop = (function (_super) {
        __extends(DragDrop, _super);
        function DragDrop(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
            this.isExam = false;
            this.allPaired = true;
            this.groupsEnabled = true;
        }
        DragDrop.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.GameData = json.GameData;
            this.Elements = json.GameData;
        };
        DragDrop.prototype.preload = function () {
            _super.prototype.preload.call(this);
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.loadGraphics();
            this.game.load.start();
        };
        DragDrop.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            this.createStaticElements();
            this.createContainerElements();
            this.createDragableElements();
            this.loadGroups();
            this.initOptions();
            EU.createFillElements(this);
            EU.createSelectableElements(this);
        };
        DragDrop.prototype.initOptions = function () {
            if (this.GameData.AllPaired === undefined || this.GameData.AllPaired === null)
                this.allPaired = true;
            else
                this.allPaired = this.GameData.AllPaired;
        };
        DragDrop.prototype.loadGraphics = function () {
            for (var i = 0; i < this.GameData.Graphics.length; i++)
                this.game.load.image(this.GameData.Prefix + this.GameData.Graphics[i].key, this.GameData.Graphics[i].src);
        };
        DragDrop.prototype.createStaticElements = function () {
            var opt = this.GameData.StaticElements.Options;
            var orientation;
            if (opt.Orientation === undefined || opt.Orientation === null) {
                orientation = true;
            }
            else {
                orientation = opt.Orientation;
            }
            var positions = EU.generateGridPositions(opt.Columns, opt.Rows, opt.MaxWidth, opt.MaxHeight, opt.Spacing, opt.Spacing, opt.StartingPos.x, opt.StartingPos.y, orientation);
            var el;
            var posX;
            var posY;
            for (var i = 0; i < this.GameData.StaticElements.el.length; i++) {
                if (this.GameData.StaticElements.Options.isGrid) {
                    posX = positions[i].x;
                    posY = positions[i].y;
                }
                else {
                    posX = this.GameData.StaticElements.el[i].pos.x;
                    posY = this.GameData.StaticElements.el[i].pos.y;
                }
                if (this.GameData.StaticElements.el.type) {
                    switch (this.GameData.StaticElements.el[i].type) {
                        case "image":
                            el = this.game.add.image(posX, posY, this.GameData.Prefix + this.GameData.StaticElements.el[i].key);
                            break;
                        case "text":
                            el = this.game.add.text(posX, posY, this.GameData.StaticElements.el[i].text, this.GameData.StaticElements.Options.Style);
                            break;
                        case "sound":
                            el = game.game.add.sprite(posX, posY, "speaker");
                            el.inputEnabled = true;
                            el.name = game.Elements.StaticElements.el[i].sound;
                            el.events.onInputUp.add(EU.soundButtonDown, game);
                            break;
                    }
                }
                else
                    el = this.game.add.image(posX, posY, this.GameData.Prefix + this.GameData.StaticElements.el[i].key);
                this.addChild(el);
            }
        };
        DragDrop.prototype.createContainerElements = function () {
            this.containers = [];
            var el;
            var opt = this.GameData.ContainerElements.Options;
            var positions = EU.generateGridPositions(opt.Columns, opt.Rows, opt.MaxWidth, opt.MaxHeight, opt.Spacing, opt.Spacing, opt.StartingPos.x, opt.StartingPos.y, true);
            for (var i = 0; i < this.GameData.ContainerElements.el.length; i++) {
                if (this.GameData.ContainerElements.Options.isGrid)
                    el = this.game.add.image(positions[i].x, positions[i].y, this.GameData.Prefix + this.GameData.ContainerElements.el[i].key);
                else
                    el = this.game.add.image(this.GameData.ContainerElements.el[i].pos.x, this.GameData.ContainerElements.el[i].pos.y, this.GameData.Prefix + this.GameData.ContainerElements.el[i].key);
                el.name = this.GameData.ContainerElements.el[i].name;
                this.containers.push({
                    "el": el,
                    "startPos": {
                        "x": el.position.x,
                        "y": el.position.y
                    },
                    "name": el.name,
                    "dragElementName": ""
                });
                this.addChild(el);
            }
        };
        DragDrop.prototype.createDragableElements = function () {
            this.dragElements = [];
            var opt = this.GameData.DragableElements.Options;
            var positions = EU.shuffleArray(EU.generateGridPositions(opt.Columns, opt.Rows, opt.MaxWidth, opt.MaxHeight, opt.Spacing, opt.Spacing, opt.StartingPos.x, opt.StartingPos.y, true));
            for (var i = 0; i < this.GameData.DragableElements.el.length; i++) {
                if (this.GameData.DragableElements.Options.isGrid)
                    var el = this.game.add.image(positions[i].x, positions[i].y, this.GameData.Prefix + this.GameData.DragableElements.el[i].key);
                else
                    var el = this.game.add.image(this.GameData.DragableElements.el[i].pos.x, this.GameData.DragableElements.el[i].pos.y, this.GameData.Prefix + this.GameData.DragableElements.el[i].key);
                el.inputEnabled = true;
                el.name = this.GameData.DragableElements.el[i].name;
                el.input.enableDrag();
                el.events.onInputUp.add(this.onInputUp, this);
                el.events.onInputDown.add(this.onInputDown, this);
                this.dragElements.push({
                    "el": el,
                    "startPos": {
                        "x": el.position.x,
                        "y": el.position.y
                    },
                    "paired": false,
                    "correctContainer": this.GameData.DragableElements.el[i].correctContainer,
                    "correctGroup": this.GameData.DragableElements.el[i].correctGroup,
                    "pairedWith": ""
                });
                this.addChild(el);
            }
        };
        DragDrop.prototype.loadGroups = function () {
            this.groupsEnabled = this.GameData.GroupsEnabled;
            if (this.groupsEnabled) {
                this.containersGroups = [];
                if (this.GameData.ContainerGroups != undefined)
                    ;
                for (var i = 0; i < this.GameData.ContainerGroups.length; i++) {
                    var tmp = [];
                    for (var x = 0; x < this.GameData.ContainerGroups[i].el.length; x++) {
                        tmp.push(this.getContainerByName(this.GameData.ContainerGroups[i].el[x].name));
                    }
                    var z = { "name": this.GameData.ContainerGroups[i].name, el: tmp };
                    this.containersGroups.push(z);
                }
            }
        };
        DragDrop.prototype.onInputUp = function (el, pointer) {
            var index = this.getCorrespondingContainerIndex(el);
            for (var i = 0; i < this.containers.length; i++) {
                if (this.overlap(el, this.containers[i].el)) {
                    if (this.GameData.DragType != "BigContainer") {
                        if (this.containers[i].dragElementName === "") {
                            this.containers[i].dragElementName = el.name;
                        }
                        else {
                            var oldDrag = this.getDragElementByName(this.containers[i].dragElementName);
                            oldDrag.pairedWith = "";
                            oldDrag.el.bringToTop();
                            var returnTween = this.game.add.tween(oldDrag.el);
                            returnTween.to({ x: oldDrag.startPos.x, y: oldDrag.startPos.y }, 500, Phaser.Easing.Circular.InOut);
                            returnTween.start();
                            this.containers[i].dragElementName = el.name;
                        }
                        el.x = this.containers[i].startPos.x;
                        el.y = this.containers[i].startPos.y;
                    }
                    this.dragElements[index].paired = true;
                    this.dragElements[index].pairedWith = this.containers[i].name;
                    this.checkIfEverythingIsCorrect();
                    return;
                }
            }
            this.dragElements[index].paired = false;
            this.dragElements[index].pairedWith = "";
            EU.Log("game completion set to 0");
            EU.setGameCompletion(this.parent, 0);
            var tw = this.game.add.tween(el);
            tw.to({ x: this.dragElements[index].startPos.x, y: this.dragElements[index].startPos.y }, 500, Phaser.Easing.Circular.InOut);
            tw.start();
        };
        DragDrop.prototype.findCorrectOrders = function (el) {
            var arr = [];
            for (var i = 0; i < this.GameData.DragCorrectOrder.length; i++) {
                for (var j = 0; j < this.GameData.DragCorrectOrder[i].el.length; j++) {
                    if (el === this.GameData.DragCorrectOrder[i].el[j].name) {
                        arr.push(j);
                    }
                }
            }
            return arr;
        };
        DragDrop.prototype.findContainerCorrectOrder = function (el) {
            var arr = [];
            for (var i = 0; i < this.GameData.ContainerGroups.length; i++) {
                for (var j = 0; j < this.GameData.ContainerGroups[i].el.length; j++) {
                    if (el === this.GameData.ContainerGroups[i].el[j].name) {
                        arr.push(j);
                    }
                }
            }
            return arr;
        };
        DragDrop.prototype.onInputDown = function (el, pointer) {
            if (this.GameData.DragType != "BigContainer")
                this.getDragElementByName(el.name).pairedWith = "";
            for (var i = 0; i < this.containers.length; i++) {
                if (this.overlap(el, this.containers[i].el)) {
                    this.containers[i].dragElementName = "";
                }
            }
            el.bringToTop();
        };
        DragDrop.prototype.checkIfEverythingIsCorrect = function () {
            switch (this.GameData.DragType) {
                case "GroupsWithOrder":
                    this.checkCompletedGroupsWithOrder(this.allPaired);
                    break;
                case "NotAllPaired":
                    this.checkCompletedFilledContainers();
                    break;
                default:
                    this.checkCompleted(this.allPaired);
                    break;
            }
        };
        DragDrop.prototype.checkCompleted = function (allPaired) {
            EU.Log("game completion set to 0");
            EU.setGameCompletion(this.parent, 0);
            var i = 0;
            if (allPaired)
                for (i = 0; i < this.dragElements.length; i++) {
                    if (!this.dragElements[i].paired)
                        return false;
                }
            if (this.groupsEnabled) {
                for (i = 0; i < this.dragElements.length; i++) {
                    if (!this.isElementInCorrectGroup(this.dragElements[i]))
                        return false;
                }
            }
            else {
                for (i = 0; i < this.dragElements.length; i++) {
                    if (this.dragElements[i].correctContainer !== this.dragElements[i].pairedWith)
                        return false;
                }
            }
            if (EU.checkIfFillsAreCorrect(this)) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("completed");
            }
        };
        DragDrop.prototype.checkCompletedGroupsWithOrder = function (allPaired) {
            EU.Log("game completion set to 0");
            EU.setGameCompletion(this.parent, 0);
            var i = 0;
            if (allPaired)
                for (i = 0; i < this.dragElements.length; i++) {
                    if (!this.dragElements[i].paired)
                        return false;
                }
            for (var i = 0; i < this.dragElements.length; i++) {
                var x = this.findCorrectOrders(this.dragElements[i].el.name);
                var y = this.findContainerCorrectOrder(this.dragElements[i].pairedWith);
                for (var j = 0; j < x.length; j++)
                    if (x[j] !== y[j])
                        return false;
            }
            if (EU.checkIfFillsAreCorrect(this)) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("completed");
            }
        };
        DragDrop.prototype.checkCompletedFilledContainers = function () {
            EU.Log(this.containers);
            for (var i = 0; i < this.containers.length; i++) {
                if (this.containers[i].dragElementName === "")
                    return false;
            }
            for (var i = 0; i < this.containers.length; i++) {
                var temp = this.getDragElementByName(this.containers[i].dragElementName).correctContainer;
                EU.Log(temp);
                if (temp !== this.containers[i].name)
                    return false;
            }
            if (EU.checkIfFillsAreCorrect(this)) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("completed");
            }
            return true;
        };
        DragDrop.prototype.overlap = function (a, b) {
            var bA = a.getBounds();
            var bB = b.getBounds();
            return Phaser.Rectangle.intersects(bA, bB);
        };
        DragDrop.prototype.isElementInCorrectGroup = function (el) {
            for (var i = 0; i < this.containersGroups.length; i++) {
                if (el.correctGroup === "")
                    return true;
                if (this.containersGroups[i].name === el.correctGroup) {
                    for (var j = 0; j < this.containersGroups[i].el.length; j++) {
                        if (this.containersGroups[i].el[j].name === el.pairedWith)
                            return true;
                    }
                }
            }
            return false;
        };
        DragDrop.prototype.getContainerByName = function (name) {
            for (var i = 0; i < this.containers.length; i++)
                if (this.containers[i].el.name === name)
                    return this.containers[i].el;
        };
        DragDrop.prototype.getDragElementByName = function (name) {
            for (var i = 0; i < this.dragElements.length; i++)
                if (this.dragElements[i].el.name === name)
                    return this.dragElements[i];
        };
        DragDrop.prototype.getCorrespondingContainerIndex = function (el) {
            for (var i = 0; i < this.dragElements.length; i++) {
                if (el === this.dragElements[i].el)
                    return i;
            }
        };
        return DragDrop;
    })(Base.BasicGroup);
    _DragDrop.DragDrop = DragDrop;
})(DragDrop || (DragDrop = {}));
//# sourceMappingURL=DragDrop.js.map