class ReelTween
{ 
    constructor (config)
    {
        this.reel = config.reel;
        this.scene = config.scene;
        this.duration = config.duration;//time to move one tile to anothe position

        this.backDuration = config.backDuration;
        this.backEase = config.backEase;

        this.spinTime = config.spinTime;//total spin time

        this.minRepeat = 4;
        this.errRepeat = 3;

        this.direction = 0;
        this.position = 0;
        this.isFastSpin = false;

        this.tween = null;
    }

    setDirection (direction)
    {
        this.direction = direction;
    }

    getDirection ()
    {
        return this.direction;
    }

    setRepeat (v)
    {
        this.tween.data[0].repeatCounter = v;
    }

    normalSpin (spinTime, extraSpinTime)
    {
        let time = spinTime + extraSpinTime;
        let repeat = Math.ceil(time / this.duration);
        let repeatExtra = Math.ceil(extraSpinTime / this.duration);

        if (time < this.spinTime && repeat >= this.minRepeat)
        {
            this.tween.data[0].repeatCounter = repeat;
        }
        else
        {
            this.tween.data[0].repeatCounter = this.minRepeat + repeatExtra;
        }
    }

    fastSpin()
    {
        if (!this.tween.isActive())
        {
            return;
        }

        this.isFastSpin = true;

        if (this.tween.data[0].repeatCounter > this.minRepeat)
        {
            this.tween.data[0].repeatCounter = this.minRepeat;
        }            
    }

    errStop ()
    {
        if (this.tween != null && this.tween.data != null && this.tween.data[0].repeatCounter != undefined)
        {
            this.tween.data[0].repeatCounter = this.errRepeat;
        }
    }

    renderReel ()
    {
        this.isFastSpin = false;// for clean up
        
        this.renderReelDown();
    }

    renderReelDown ()
    {
        this.position = 0;

        this.tween = this.scene.tweens.add(
        {
            targets: this.reel.container,
            
            y: this.reel.container.y + this.reel.reelRectHeight,
            
            duration: this.duration,
            
            repeat: -1,

            callbackScope: this,

            onRepeat: function  ()
            {
                if (++this.reel.position >= this.reel.reel.length)
                {
                    this.reel.position = 0;
                }

                if (this.tween.data[0].repeatCounter > 0)
                {
                    this.tween.updateTo("y", this.tween.targets[0].y + this.reel.reelRectHeight, true);
                }
                else
                {
                    this.tween.updateTo("y", this.tween.targets[0].y + this.reel.reelRectHeight / 4, true);
                    this.tween.data[0].duration = this.duration / 4;
                }

                this.tween.targets[0].last.y = this.tween.targets[0].first.y - this.reel.reelRectHeight;
                let symbol = this.tween.targets[0].last;

                if (this.tween.data[0].repeatCounter > 3 || this.tween.data[0].repeatCounter == 0 || this.reel.parentScene.gameState == GAME_STATE_ERROR)
                {
                    symbol.setVisible(true).setTexture(SYMBOLS[this.reel.reel[this.reel.position]]);
                }
                else
                {
                    symbol.setVisible(true).setTexture(SYMBOLS[this.reel.symbolField[this.position++]]);
                }

                this.tween.targets[0].moveTo(symbol, 0);

                if (this.tween.data[0].repeatCounter <= 4)
                {
                    let d = 0;

                    if (!this.isFastSpin)
                    {
                        switch (this.tween.data[0].repeatCounter)
                        {
                            case 4:d = this.duration;break;
                            case 3:d = this.duration + 13;break;
                            case 2:d = this.duration + 26;break;
                            case 1:d = this.duration + 39;break;
                            case 0:d = this.duration + 52;break;
                        }
                    
                        this.tween.data[0].duration = d;
                    }
                    else
                    {
                        switch (this.tween.data[0].repeatCounter)
                        {
                            case 4:d = this.duration;break;
                            case 3:d = this.duration + 2;break;
                            case 2:d = this.duration + 4;break;
                            case 1:d = this.duration + 6;break;
                            case 0:d = this.duration + 8;break;
                        }
                        
                        this.tween.data[0].duration = d;
                    }
                }
            },
            onComplete: function ()
            {
                if (!this.isFastSpin)
                {
                    this.scene.sound.play("stop");
                }
                else if (this.reel.reelId == 0)
                {
                    this.scene.sound.play("faststop");
                }

                if (this.reel.reelId == 4)
                {
                    this.reel.rm.hideStopBtn();
                }
                
                this.reel.updateMask();

                this.tween = this.scene.tweens.add(
                {
                    targets: this.reel.container,
                    y: this.reel.container.y - this.reel.reelRectHeight / 4,
                    
                    duration: this.backDuration,
                    
                    repeat: 0,

                    ease: this.backEase,

                    callbackScope: this,

                    onComplete: function ()
                    {
                        this.reel.endSpin();
                    }
                });
            }

        }, this);
    }
}