var EHDI = EHDI || Object.create(nul);

EHDI.components = EHDI.components || Object.create(null);

EHDI.components.Timer = function() {
    EHDI.aka.Container.call(this);
    this.timeLeft = 120;

    this.containerSprite = new EHDI.aka.Sprite(EHDI.Assets.images["gfx_container"]);

    this.addChild(this.containerSprite);

    var timerHeader = new EHDI.displays.TextSprite(EHDI.GAME.JSONMgr.getLocale("LBL_TIMER_HEADER"));

    timerHeader.anchor.x = 0.5;
    timerHeader.anchor.y = 0.5;
    timerHeader.position.set(this.containerSprite.width * 0.169, this.containerSprite.height * 0.4);
    this.addChild(timerHeader);

    this.timerTxt = new EHDI.displays.TextSprite(EHDI.GAME.JSONMgr.getLocale("SCORE_FORMAT"));

    this.timerTxt.anchor.x = 0.5;
    this.timerTxt.anchor.y = 0.5;

    this.timerUpdate = this.updateTime.bind(this);

    EHDI.GAME.updateManager.addFrameListener(this.timerUpdate);

    this.timerTxt.position.set(this.containerSprite.width * 0.675, this.containerSprite.height * 0.45);

    this.addChild(this.timerTxt);

    this.flash = new EHDI.aka.Sprite(EHDI.Assets.images["gfx_container_white"]);
    this.addChild(this.flash);
    this.flash.alpha = 0;
    this.flash.visible = false;

    this.sparkle = new EHDI.aka.Sprite(EHDI.Assets.images["gfx_flare_star"]);
    this.sparkle.anchor.set(0.5, 0.5);
    this.sparkle.position.set(timerHeader.x, timerHeader.y);
    this.sparkle.scale.set(0, 0);

    this.addChild(this.sparkle);

    this.line = new EHDI.aka.Sprite(EHDI.Assets.images["gfx_flare_long"]);
    this.line.anchor.set(0.5, 0.5);
    this.line.position.set(this.containerSprite.width * 0.5, this.containerSprite.height * 0.4);
    this.line.scale.set(0, 1);
    this.addChild(this.line);

    this.onTimeTimeline = new TimelineMax();
    this.onTimeTimeline.to(this.timerTxt.scale, 0.1, {x: 1.5, y : 1.5});
    this.onTimeTimeline.to(this.timerTxt.scale, 0.1, {x: 1, y : 1});
    this.onTimeTimeline.addCallback(this.toggleEffects.bind(this), "+=0", [true]);
    this.onTimeTimeline.addCallback(this.toggleFlash.bind(this), "+=0", [true]);
    this.onTimeTimeline.to(this.sparkle.scale, 0.1, {x: 1, y : 1, ease: Power0.easeNone});
    this.onTimeTimeline.to(this.line.scale, 0.2, {x: 1, y : 1, ease: Power0.easeNone}, "-=0.1");
    this.onTimeTimeline.to(this.flash, 0.05, {alpha : 0.75});
    this.onTimeTimeline.to(this.sparkle, 0.15, {x: this.containerSprite.width * 0.9, rotation: 5, ease: Power0.easeNone}, "-=0.15");
    this.onTimeTimeline.to(this.flash, 0.1, {alpha : 0}, "-=0.025");
    this.onTimeTimeline.addCallback(this.toggleFlash.bind(this), "-=0.05", [false]);
    this.onTimeTimeline.addCallback(this.toggleEffects.bind(this), "+=0.025", [false]);
    this.onTimeTimeline.pause();
};

EHDI.components.Timer.prototype = Object.create(EHDI.aka.Container.prototype);

EHDI.components.Timer.prototype.updateTime = function(dt) {
    if(EHDI.GAME.pauseButton.isPaused || EHDI.GAME.pauseButton.isEndGame) {
        if(this.shakeTimeline)
            this.shakeTimeline.pause();
        return;
    } else {
        if(this.shakeTimeline && this.shakeTimeline.paused)
            this.shakeTimeline.play();

    }
    this.timeLeft -= dt / 1000;
    if(this.timeLeft < 0) {
        this.timeLeft = 0;
    }
    this.timerTxt.text = Math.floor(this.timeLeft/60) + ":"
    if(Math.floor(this.timeLeft % 60) < 10) {
        this.timerTxt.text += "0";
    }
    this.timerTxt.text += Math.floor(this.timeLeft % 60);

    if(this.timeLeft < 11 && !this.isRed) {
        this.isRed = true;
        this.timerTxt.style = {fontFamily:'proximanova-black', fill: 0xED1C24, fontSize: 36};
        this.shakeTimeline = new TimelineMax({repeat : -1});
        this.shakeTimeline.to(this.timerTxt, 0.05, {x : this.containerSprite.width * 0.69, ease : Power0.easeNone});
        this.shakeTimeline.to(this.timerTxt, 0.1, {x : this.containerSprite.width * 0.66, ease : Power0.easeNone});
        this.shakeTimeline.to(this.timerTxt, 0.05, {x : this.containerSprite.width * 0.675, ease : Power0.easeNone});
    } else if(this.timeLeft <= 0) {
        if(this.shakeTimeline)
            this.shakeTimeline.kill();
        EHDI.GAME.updateManager.removeFrameListener(this.timerUpdate);
        EHDI.GAME.matchManager.endGame();
    }
};

EHDI.components.Timer.prototype.addTime = function(timeToAdd) {
    this.timeLeft += timeToAdd;
    this.onTimeTimeline.restart();
    this.onTimeTimeline.play();
};

EHDI.components.Timer.prototype.dispose = function() {
    if(this.shakeTimeline)
        this.shakeTimeline.kill();
    EHDI.GAME.updateManager.removeFrameListener(this.timerUpdate);
};

EHDI.components.Timer.prototype.toggleEffects = function(bool) {
    this.line.visible = bool;
    this.sparkle.visible = bool;
};

EHDI.components.Timer.prototype.toggleFlash  = function(bool) {
    this.flash.visible = bool;
};