class SceneButtonEx extends Phaser.Scene
{
    create (data)
    {
        this.parent = data.scene;
        this.img = this.add.image(data.img_x + data.horizontalOffset, data.img_y + data.vDelta, data.img);
        this.img.setVisible(false);

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

        this.canSleep = true;
        this.canWake = true;

		this.enableFadeOut = true;

        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.scene.sleep();
    }

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

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

    reset()
    {
        this.img.setVisible(false);

        this.hiddenTime = 20000;
        this.hiddenTimer = this.hiddenTime;
    }

    setPosition(x, y, img_x, img_y)
    {
        this.button.setPosition_(x, y);
        this.img.setPosition(img_x, img_y);
    }

    setScale(v)
    {
        this.img.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;
        }
    }

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

	showHint()
	{
	    this.fadeIn();
	}

	enableFadeOut(v)
	{
        this.enableFadeOut = true;
	}

	disableFadeOut(v)
	{
        this.enableFadeOut = false;
	}
    
	fadeIn()
    {
        this.img.setVisible(true);
        this.img.setAlpha(0.0);

        this.timeline = this.tweens.timeline({

            targets: this.img,
            ease: 'Linear',
            duration: 500,
    
            tweens: [{
                alpha: 1.0,
            },
            ],
            onCompleteScope: this,
            onComplete: function() 
            {
                setTimeout(this.fadeOut.bind(this), 2000);
            },
        });
    }

    fadeOut()
    {
        if(!this.enableFadeOut)
		{
			return;
		}

		this.timeline = this.tweens.timeline({

            targets: this.img,
            ease: 'Linear',
            duration: 500,
    
            tweens: [{
                alpha: 0.0,
            },
            ],
            onCompleteScope: this,
            onComplete: function() 
            {
                this.img.setVisible(false);
                this.hiddenTimer = this.hiddenTime;
                this.tweens.killTweensOf(this.img);
            },
        });
    }

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