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

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

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

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

    this.addChild(containerSprite);

    // var timerHeader = new EHDI.aka.PixiText("TIMER: ", {fontFamily: 'proximanova-black', fill: 0xFFFFFF, fontSize : 20});
    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(containerSprite.width * 0.19, containerSprite.height * 0.4);
    this.addChild(timerHeader);

    // this.timerTxt = new EHDI.aka.PixiText("2:00", {fontFamily:'proximanova-black', fill: 0xFFFFFF, fontSize: 36});
    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(containerSprite.width * 0.675, 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(containerSprite.width * 0.5, 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: 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)
        return;
    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 <= 0) {
        EHDI.GAME.updateManager.removeFrameListener(this.timerUpdate);
        EHDI.GAME.gameScene.endGame();
    }
};

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

EHDI.components.Timer.prototype.dispose = function() {
    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;
};