class SpriteEx extends Phaser.GameObjects.RenderTexture
{
    constructor (scene, reel, x, y, w, h, key)
    {
        super(scene, x, y, w, h);
        
        this.scene.add.existing(this);

        this.sprite = this.scene.add.sprite(0, 0, key).setVisible(false);

        this.sprite.on("animationcomplete", this.animationComplete, this);

        this.reel = reel;

        this.renderSprite();

        this.isPlaying = false;

        this.isLanding = false;

        return this;
    }
    
    setVisible (v)
    {
        this.visible = v;

        return this;
    }

    playAnimation (repeat = 0)
    {
        if (this.sprite.anims.currentAnim != null)
        {
            if (repeat != undefined)
            {
                this.sprite.anims.currentAnim.repeat = repeat;
            }

            this.sprite.play(this.sprite.anims.currentAnim.key, false);
        }

        let scaleRepeat = (repeat == 0) ? 1 : 3;

        this.scale(400, scaleRepeat);

        this.renderSprite();

        this.isPlaying = true;

        return this;
    }

    stopAnimation ()
    {
        this.scene.tweens.killTweensOf(this.sprite);
        
        this.sprite.stop();

        this.sprite.setScale(1.0);

        if (this.sprite.anims.currentAnim != null)
        {
            this.sprite.setFrame(this.sprite.anims.currentAnim.frames[0].textureFrame);
        }
        
        this.renderSprite();
        
        this.isPlaying = false;

        return this;
    }

    destroyAnimation ()
    {
        this.stopAnimation();
        
        if (this.sprite.anims.currentAnim != null)
        {
            let key = this.sprite.anims.currentAnim.key;
            this.sprite.anims.animationManager.remove(key);

            this.sprite.anims.currentAnim = null;
        }

        return this;
    }

    animationComplete ()
    {
        if (this.sprite.anims.currentAnim != null && this.sprite.anims.currentAnim.frames.length > 1)
        {
            this.sprite.setFrame(this.sprite.anims.currentAnim.frames[0].textureFrame);

            this.renderSprite();
        }
    }

    setTextureKey (key)
    {
        this.setScale(1.0);

        this.sprite.setTexture(key);

        this.renderSprite();

        return this;
    }

    getTextureKey ()
    {
        return this.sprite.texture.key;
    }

    startLandingAnimation ()
    {
        this.isLanding = true;

        this.renderSprite();
    }

    stopLandingAnimation ()
    {
        this.isLanding = false;

        this.renderSprite();
    }
    
    scale (duration, repeat)
    {
        this.scene.tweens.add({
            targets: this.sprite,
            ease: "Linear",
            duration: duration,
            scale: 1.2,
            repeat: repeat,
            yoyo: true,
            callbackScope: this,
            onComplete: function ()
            {
                this.stopAnimation();

                this.reel.animationComplete();
            },
        });
    }

    update (time, delta)
    {
        this.sprite.preUpdate(time, delta);

        this.render();
    }

    renderSprite ()
    {
        this.clear();

        this.draw(this.sprite, this.displayWidth / 2, this.displayHeight / 2);

        if (this.sprite.texture.key == "scatter" && this.isLanding)
        {
            this.draw(this.reel.landingSprite, this.displayWidth / 2, this.displayHeight / 2 + 40);
        }
    }

    render ()
    {
        if (this.isPlaying || this.isLanding)
        {
            this.renderSprite();
        }
    }
}