class SceneButtonCounter extends Phaser.Scene
{
    create (data)
    {
        this.parent = data.scene;

        this.frameTime = 250;
        this.frameTimer = -1;
        this.defaultFrame = 0;
        this.frame = 0;

        this.canSleep = true;
        this.canWake = true;
        this.disabled = false;

        this.button = new Button({
            'scene': this,
            'key':data.key,
            'up': 0,
            'over':1,
            'down':2,
            'x': data.x,
            'y': data.y,
            'horizontalOffset': data.horizontalOffset,
            'vDelta': data.vDelta,
            'onClick': data.onClick,
            'onPressed': data.onPressed,
            'handlerScene': this.parent
        });
        
        this.stopBtnKey = data.key;
        this.counterBtnKey = data.counter_key;

        this.btn_x = data.x;
        this.btn_y = data.y;

        this.autoCounter = this.add.bitmapText(data.x, data.y - 10, 'fontBet', '100');
        this.autoCounter.setVisible(false);
        this.autoCounter.setOrigin(0.5);
    
        this.scene.sleep();
    }
    
    sleep()
    {
        this.autoCounter.setVisible(false);
        this.button.setTexture(this.stopBtnKey);

        if(this.canSleep)
        {
            this.scene.sleep();
        }            
    }

    wake()
    {
        if(this.canWake)
        {
            this.scene.wake();
            this.scene.bringToTop();
        }            
    }

    setCanSleep(v)
    {
        this.canSleep = v;
    }

    setCanWake(v)
    {
        this.canWake = v;
    }

    setPosition(x, y)
    {
        this.button.setPosition_(x, y);

        if(y < 800)
        {
            this.autoCounter.setScale(1.0);
        }
        else
        {
            this.autoCounter.setScale(2.0);
        }

        this.btn_x = x;
        this.btn_y = y;

        this.autoCounter.setPosition(x , y - 10);

    }
    
    setScale(v)
    {
        this.button.setScale(v);
    }

    setFrame(frame)
    {
        this.frame = this.button.getFrame();

        switch(frame)
        {
            case 'DOWN':this.frameTimer = this.frameTime;this.button.setFrame_(0);break;
            case 'OVER':this.frameTimer = this.frameTime;this.button.setFrame_(1);break;
            case 'UP':this.frameTimer = this.frameTime;this.button.setFrame_(2);break;
            default:break;
        }
    }

    enable()
    {
        this.button.setFrame_(0);
        this.button.enable_();
        this.disabled = false;
    }

    disable()
    {
        this.button.setFrame_(2);
        this.button.disable_();        
        this.disabled = true;
    }

    onPressedComplete()
    {
        this.button.onPressedComplete();
    }

    startCountdown()
    {
        this.button.setTexture(this.counterBtnKey);
        this.autoCounter.setVisible(true);
    }

    setCounter(autoCount)
    {
        this.autoCounter.setText(autoCount);
    }

    update(time, delta)
    {
        if(this.disabled)
        {
            return;
        }
        
        this.button.update(time, delta);
        
        if(this.frameTimer >= 0)
        {
            this.frameTimer -= delta;
            if(this.frameTimer < 0)
            {
                this.button.setFrame_(this.frame);
            }
        }
    }
}
