class SpriteEx extends Phaser.GameObjects.Sprite
 {
    constructor(scene, reel, x, y, key, animtype) //0 -standart,  1 - symbols, 2 - expanded symbols
    {
        super(scene, x, y, key);
        
        this.scene.add.existing(this);
        this.reel = reel;
        this.x = x;
        this.y = y;
        this.animtype = animtype;
        
        this.rt_w = 334;
        this.rt_h = 306;

        this.rt = this.scene.add.renderTexture(x - 108, y - 110, this.rt_w, this.rt_h);
        this.rt.setVisible(false);

        this.setOrigin(0);

        this.isPlaying = false;

        this.default_scale = 1.0;
        this.scale_ = 1.2;

        this.timeouts = [1, -320, 1000, -320, 1000];
        this.timer = -1;
        this.index = 0;
        
        this.isPlaying = false;
        this.pulsations = 0;
    }
    
    play_(frame)
    {
        if(frame == null)
        {
            frame = 0;
        }

        if(this.animtype == 0)
        {
            if(this.anims == null || this.anims.currentAnim == null)
            {
                return;
            }

            this.setOrigin(0);
            this.once('animationcomplete', this.onAnimationCompleted);
            this.play(this.anims.currentAnim.key, false, frame);
        }
        else if(this.animtype == 1)
        {
            this.setOrigin(0.5);
        }
        else if(this.animtype == 2)
        {
            this.setOrigin(0.5);
            this.startSpriteScaleScatter();
        }
        
        this.index = 0;
        this.timer = -1;
        this.pulsations = 0;

        this.isPlaying = true;
    }

    stop_()
    {
        if(this.anims.currentAnim != null)
        {
            this.play(this.anims.currentAnim.key, false, 0);//stop at frame 0
            this.anims.stop();
            this.renderSprite();
        }
                    
        this.scene.tweens.killTweensOf(this);
        
        this.setAlpha(1.0);
        this.index = 0;
        this.timer = -1;
        this.pulsations = 0;

        this.isPlaying = false;
    }

    destroy_()
    {
        if(this.anims.currentAnim != null)
        {
            let anim_key = this.anims.currentAnim.key;
            this.anims.animationManager.remove(anim_key);
            this.anims.currentAnim = null;
        }

        this.isPlaying = false;

        this.scale_ = 1.2;

        this.timer = -1;
        this.index = 0;
        this.pulsations = 0;

        this.animtype = 0;
    }
    
    setVisible_(v)
    {
        this.setVisible(v);
    }

    setAlpha_(v)
    {
        this.setAlpha(v);
        if(this.animtype == 0)
        {
            this.renderSprite();
        }
        else if(this.animtype == 1)
        {
            this.renderSymbol();
        }
    }

    setDefaultScale_()
    {
        this.setScale(this.default_scale);
    }

    setTextureW(key, animtype)
    {
        if(this.anims.currentAnim != null)
        {
            let anim_key = this.anims.currentAnim.key;
            this.anims.animationManager.remove(anim_key);
            this.anims.currentAnim = null;
        }

        this.setTexture(key);
        this.animtype = animtype;
    }

    getTextureKey()
    {
        return this.texture.key;
    }
    
    getFrame()
    {
        return this.rt;
    }

    startSpriteScale()
    {
        this.scene.tweens.add({
            targets: this,
            ease: 'Linear',
            duration: 100,
            scale: this.scale_,
            repeat: 0,
            yoyo: true,
            onCompleteScope: this,
            onComplete: function() 
            {
                this.setScale(this.default_scale);
            },
        });
    }

    startSpriteScaleScatter()
    {
        this.scene.tweens.add({
            targets: this,
            ease: 'Linear',
            duration: 250,
            scale: this.scale_,
            repeat: 0,
            yoyo: true,
            onCompleteScope: this,
            onComplete: function() 
            {
                this.setScale(this.default_scale);
                this.scene.tweens.killTweensOf(this);
                if(++this.pulsations == 2)
                {
                    this.stop_();
                }
                else
                {
                    this.timer = 500;//pause between first and second pulsation
                }                    
            },
        });
    }

    onAnimationCompleted()
    {
        this.reel.on_animation_completed();//notify the reel
    }

    preUpdate (time, delta)
    {
       super.preUpdate(time, delta);
       this.update(delta);
    }

    update(delta)
    {
        if(!this.isPlaying) return;

        if(this.animtype == 0)
        {
            this.renderSprite();
        }
        else if(this.animtype == 1)
        {
            this.renderSymbol();
        }
        else if(this.animtype == 2)
        {
            if(this.timer >= 0)
            {
                this.timer -= delta;
                if(this.timer < 0)
                {
                    this.startSpriteScaleScatter();
                }
            }

            this.renderSymbol();
        }
    }

    renderSprite()
    {
        this.rt.clear();
        
        if(this.texture.source.length > 0)
        {
            let dx = (this.rt.displayWidth - this.displayWidth) / 2;
            let dy = (this.rt.displayHeight - this.displayHeight) / 2;
       
            this.rt.draw(this, dx, dy);
        }            
    }

    renderSymbol()
    {
        this.rt.clear();
        let dx = (this.rt.displayWidth - this.width) / 2;
        let dy = (this.rt.displayHeight - this.height) / 2;
        
        this.rt.draw(this, this.width / 2 + dx, this.height/2 + dy);
    }
}