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 Sequence;
(function (_Sequence) {
    var Sequence = (function (_super) {
        __extends(Sequence, _super);
        function Sequence(game, name, x, y, json) {
            _super.call(this, game, name, x, y, json);
            this.topSeq = [];
            this.bottomSeq = [];
            this.correctSeq = [];
            this.icons = [];
            this.slots = [];
            this.xPos = 0;
        }
        Sequence.prototype.init = function (json) {
            _super.prototype.init.call(this, json);
            this.Elements = json.GameData;
        };
        Sequence.prototype.preload = function () {
            _super.prototype.preload.call(this);
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.loadGraphics();
            this.game.load.start();
        };
        Sequence.prototype.loadGraphics = function () {
            for (var i = 0; i < this.Elements.Graphics.length; i++)
                this.game.load.image(this.Elements.Graphics[i].key, this.Elements.Graphics[i].src);
        };
        Sequence.prototype.create = function () {
            if (this.json.isStandalone) {
                _super.prototype.create.call(this);
            }
            this.frames = this.Elements.Icons;
            this.prepare();
            this.game.time.events.add(Phaser.Timer.SECOND * 5, this.start, this);
        };
        Sequence.prototype.prepare = function () {
            this.xPos = (1920 / 2 - (this.frames * 300 + (this.frames - 1) * 20) / 2) - 120;
            for (var i = 0; i < this.frames; i++)
                this.topSeq.push(0);
            for (var i = 1; i < 37; i++)
                this.correctSeq.push(i);
            this.correctSeq = this.shuffle(this.correctSeq);
            this.correctSeq = this.correctSeq.slice(0, this.frames);
            for (var i = 0; i < this.frames; i++)
                this.addChild(this.slots[i] = new Icon(this.game, this.xPos + i * 300 + i * 20, 40, 'OS_g_II_12_empty', i * -1));
            for (var i = 0; i < this.frames; i++) {
                this.icons[i] = new Icon(this.game, this.xPos + i * 300 + i * 20, 40, 'OS_g_II_12_' + this.correctSeq[i].toString(), this.correctSeq[i]);
                this.addChild(this.icons[i]);
            }
        };
        Sequence.prototype.start = function () {
            for (var i = 0; i < this.frames; i++)
                this.icons[i].kill();
            this.bottomSeq = this.correctSeq.slice();
            this.bottomSeq = this.shuffle(this.bottomSeq);
            for (var i = 0; i < this.frames; i++) {
                this.icons[i] = new Icon(this.game, this.xPos + i * 300 + i * 20, 400, 'OS_g_II_12_' + this.bottomSeq[i].toString(), this.bottomSeq[i]);
                this.icons[i].name = this.bottomSeq[i];
                this.icons[i].inputEnabled = true;
                this.icons[i].input.enableDrag(false, true);
                this.icons[i].events.onDragStop.add(this.stopDrag, this);
                this.addChild(this.icons[i]);
            }
        };
        Sequence.prototype.stopDrag = function (item) {
            var out = true;
            for (var slot = 0; slot < this.frames; slot++) {
                if (Phaser.Rectangle.intersects(item.rect, this.slots[slot].rect)) {
                    if (this.topSeq[slot] == 0) {
                        for (var i = 0; i < this.frames; i++)
                            if (this.topSeq[i] == item.id)
                                this.topSeq[i] = 0;
                        this.topSeq[slot] = item.id;
                    }
                    else
                        break;
                    this.game.add.tween(item).to({ x: this.slots[slot].x }, 500, Phaser.Easing.Cubic.Out, true);
                    this.game.add.tween(item).to({ y: this.slots[slot].y }, 500, Phaser.Easing.Cubic.Out, true);
                    out = false;
                    break;
                }
            }
            if (out) {
                for (var i = 0; i < this.frames; i++)
                    if (this.topSeq[i] == item.id) {
                        this.topSeq[i] = 0;
                        break;
                    }
                this.game.add.tween(item).to({ x: item.originX }, 500, Phaser.Easing.Cubic.Out, true);
                this.game.add.tween(item).to({ y: item.originY }, 500, Phaser.Easing.Cubic.Out, true);
            }
            if (this.topSeq.toString() == this.correctSeq.toString()) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("Sekwencja poprawna.");
            }
            else {
                EU.Log("      --- ");
                EU.Log(this.correctSeq);
                EU.Log(this.topSeq);
            }
        };
        Sequence.prototype.shuffle = function (o) {
            for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x)
                ;
            return o;
        };
        return Sequence;
    })(Base.BasicGroup);
    _Sequence.Sequence = Sequence;
    var Icon = (function (_super) {
        __extends(Icon, _super);
        function Icon(game, x, y, key, id) {
            this.originX = x;
            this.originY = y;
            this.id = id;
            this.rect = new Phaser.Rectangle(x, y, 300, 300);
            _super.call(this, game, x, y, key);
            this.events.onDragStop.add(this.updateCoord, this);
        }
        Icon.prototype.updateCoord = function () {
            this.rect = new Phaser.Rectangle(this.x, this.y, 300, 300);
        };
        return Icon;
    })(Phaser.Sprite);
})(Sequence || (Sequence = {}));
//# sourceMappingURL=Sequence.js.map