class ReelManager
{
    constructor (config)
    {
        this.parentScene = config.scene;

        this.reelPanel = this.parentScene.add.image(0, 0, "bgrreels").setVisible(false);

        this.nmReelsStopped = 0;//at the end of spin, number of reels that are stopped

        this.extraSpinTime = 335;

        this.winLineTime = 3000;
        this.winLineTimer = 0;
     
        this.spinPositions = [];

        for (let i = 0; i < config.ngreels.length; ++i )
        {
            let pos = (Math.floor(Math.random() * (config.ngreels[i].length)));
            this.spinPositions.push(pos);
        }

        this.fgSpinPositions = [];

        for (let i = 0; i < config.fgreels.length; ++i )
        {
            let pos = (Math.floor(Math.random() * (config.fgreels[i].length)));
            this.fgSpinPositions.push(pos);
        }

        this.winLines = [];
        this.winLinesAS = [];

        this.winLine = 0;

        this.moveCount = 0;
        
        this.reels = [];

        let reelconf = 
        {
            scene: this.parentScene,
            rm: this,

            containerY: 1,

            duration: 50,
            backDuration: 280,
            backEase: "Quart.easeOut",
            
            spinTime: REEL_SPIN_DURATION + this.parentScene.gameSocket.getExtraStartSpinTime()//max spin time
        }            

        reelconf.reelId = 0;
        reelconf.reelPosition = this.spinPositions[0];
        reelconf.ngreel = config.ngreels[0];
        reelconf.fgreel = config.fgreels[0];

        reelconf.containerX = -2 * REEL_RECT_WIDTH - 1;

        this.reels.push(new Reel(reelconf));

        reelconf.reelId = 1;
        reelconf.reelPosition = this.spinPositions[1];
        reelconf.ngreel = config.ngreels[1];
        reelconf.fgreel = config.fgreels[1];

        reelconf.spinTime += this.extraSpinTime;

        reelconf.containerX = -REEL_RECT_WIDTH - 1;

        this.reels.push(new Reel(reelconf));

        reelconf.reelId = 2;
        reelconf.reelPosition = this.spinPositions[2];
        reelconf.ngreel = config.ngreels[2];
        reelconf.fgreel = config.fgreels[2];

        reelconf.spinTime += this.extraSpinTime;

        reelconf.containerX = 0;

        this.reels.push(new Reel(reelconf));

        reelconf.reelId = 3;
        reelconf.reelPosition = this.spinPositions[3];
        reelconf.ngreel = config.ngreels[3];
        reelconf.fgreel = config.fgreels[3];

        reelconf.spinTime += this.extraSpinTime;

        reelconf.containerX = REEL_RECT_WIDTH + 1;

        this.reels.push(new Reel(reelconf));

        reelconf.reelId = 4;
        reelconf.reelPosition = this.spinPositions[4];
        reelconf.ngreel = config.ngreels[4];
        reelconf.fgreel = config.fgreels[4];

        reelconf.spinTime += this.extraSpinTime;

        reelconf.containerX = REEL_RECT_WIDTH * 2 + 2;

        this.reels.push(new Reel(reelconf));

        this.canShowStopBtn = true;

        this.spinResult = null;

        this.groupedSymbols = [];
        this.groupedSymbolsAS = [];

        this.scatterCount = 0;

        this.animationsComplete = 0;
        this.animationsTotal = 0;

        this.container = this.parentScene.add.container(960, 608).setDepth(4);
        
        this.container.add(this.reelPanel);

        this.container.add(this.reels[0].container);
        this.container.add(this.reels[1].container);
        this.container.add(this.reels[2].container);
        this.container.add(this.reels[3].container);
        this.container.add(this.reels[4].container);

        this.container.bringToTop(this.reels[0].container);
        this.container.bringToTop(this.reels[1].container);
        this.container.bringToTop(this.reels[2].container);
        this.container.bringToTop(this.reels[3].container);
        this.container.bringToTop(this.reels[4].container);

        this.visible = true;

        this.wp = 0;
        this.sp = 0;

        this.garlandEmitter = new GarlandEmitter({ scene: this.parentScene, x: 960, y: 280, depth: 5, key: "garland" });
    }

    update (time, delta) 
    {
        this.reels.forEach(reel => {reel.update(time, delta);});

        switch (this.state)
        {
            case RM_STATE_TOTAL_WIN:
            case RM_STATE_BEFORE_SWAP:
            case RM_STATE_AFTER_SWAP:
            case RM_STATE_S_SCATTER_WIN:break;
    
            case RM_STATE_PLAY_WIN:
                {
                    this.winLineTimer -= delta;
                    if (this.winLineTimer < 0)
                    {
                        this.winLineTimer = this.winLineTime;

                        if (++this.winLine >= this.winLines.length)
                        {
                            this.winLine = 0;
                        }

                        this.reels.forEach(reel => {this.container.bringToTop(reel.container);});
                        this.reels.forEach(reel => { reel.setWinLine(this.winLine); });
                        this.reels.forEach(reel => { reel.playLineAnimation(); });
                        
                        this.parentScene.txtWin.setText(this.parentScene.getText(GLOBAL_TXT_WAY_PAYS) + " " + (this.winLine + 1) + " " + this.parentScene.getText(GLOBAL_TXT_PAYS) + " " + this.getLineWin());
                    }                        
                };break;
        }
    }

    showWinCounter ()
    {
        this.parentScene.tw.setDisplayWinCombinations(false);
        
        if (this.parentScene.gameState == GAME_STATE_TOTAL_WIN)
        {
            this.parentScene.tw.setWin(this.parentScene.gameSocket.getTotalWinBeforeFreeGames(), false);
        }
        else
        {
            this.parentScene.tw.setWin(this.parentScene.getTotalWin(), false);

            if (this.spinResult.reelSwapIndex != -1)//allow collect win after swap
            {
                this.parentScene.collectBtn.wake();
            }
        }            
    }

    showWins ()
    {
        this.parentScene.txtWin.setVisible(true);

        this.playWinLines();
    }

    playTotalWin ()
    {
        if (this.spinResult.reelSwapIndex == -1)
        {
            this.playNormalWin();
        }
        else
        {
            if (this.winLines.length > 0)
            {
                this.playWinBeforeSwap();
            }
            else
            {
                this.state = RM_STATE_BEFORE_SWAP;

                this.swapReel (this.spinResult.reelSwapIndex, this.spinResult.symbolFieldAS);
            }

            this.parentScene.sndWin.play();
        }
    }

    playNormalWin ()
    {
        this.reels.forEach(reel => { reel.destroyAnimations(); });

        this.animationsTotal = 0;
        
        if (this.spinResult.reelSwapIndex == -1)
        {
            if (this.winLines.length > 0)
            {
                this.reels.forEach(reel => {reel.createLineAnimations();});

                for (let i = 0; i < this.reels.length; ++i)
                {
                    let v = [];

                    v.push(this.groupedSymbols[0][i]);
                    v.push(this.groupedSymbols[1][i]);
                    v.push(this.groupedSymbols[2][i]);

                    this.reels[i].setWinGroup(v);
                    this.animationsTotal += this.reels[i].playGroupAnimations();
                }

                if (!this.parentScene.sndWin.isPlaying)
                {
                    this.parentScene.sndWin.play();
                }
            
                this.state = RM_STATE_TOTAL_WIN;
            }
            else if (this.spinResult.sScatters > 2)//scatters
            {
                this.playScatters();
            }
        }
        else
        {
            this.parentScene.time.delayedCall(600, this.showWinCounter, [], this);
            this.state = RM_STATE_TOTAL_WIN;
        }
    }

    playWinBeforeSwap ()
    {
        this.reels.forEach(reel => {reel.createLineAnimations();});

        for (let i = 0; i < this.reels.length; ++i)
        {
            let v = [];

            v.push(this.groupedSymbols[0][i]);
            v.push(this.groupedSymbols[1][i]);
            v.push(this.groupedSymbols[2][i]);

            this.reels[i].setWinGroup(v);
            this.animationsTotal += this.reels[i].playGroupAnimations();
        }

        this.state = RM_STATE_BEFORE_SWAP;
    }

    playWinAfterSwap ()
    {
        this.animationsTotal = 0;

        this.reels.forEach(reel => {reel.createLineAnimationsAS();});

        for (let i = 0; i < this.reels.length; ++i)
        {
            let v = [];

            v.push(this.groupedSymbolsAS[0][i]);
            v.push(this.groupedSymbolsAS[1][i]);
            v.push(this.groupedSymbolsAS[2][i]);

            this.reels[i].setWinGroup(v);
            this.animationsTotal += this.reels[i].playGroupAnimations();
        }
        
        if (!this.parentScene.sndWin.isPlaying)
        {
            this.parentScene.sndWin.play();
        }

        this.state = RM_STATE_AFTER_SWAP;
    }

    playWinLines ()
    {
        this.reels.forEach(reel => { reel.stopAnimations(); });

        this.winLineTimer = this.winLineTime;
        this.winLine = 0;

        this.reels.forEach(reel => {this.container.bringToTop(reel.container);});
        this.reels.forEach(reel => {reel.setWinLine(this.winLine);});
        this.reels.forEach(reel => {reel.playLineAnimation();});

        this.parentScene.txtWin.setText(this.parentScene.getText(GLOBAL_TXT_WAY_PAYS) + " " + (this.winLine + 1) + " " + this.parentScene.getText(GLOBAL_TXT_PAYS) + " " + this.getLineWin());

        this.state = RM_STATE_PLAY_WIN;
    }

    playScatters ()
    {
	    this.reels.forEach(reel => {reel.createScatterAnimation(this.spinResult.sScattersField[reel.reelId]);});

        this.animationsTotal = 0;
        
        this.reels.forEach(reel => {this.animationsTotal += reel.playScatterAnimation();});

        this.parentScene.sound.play("fgMark");

        if (!this.parentScene.sndWin.isPlaying)
        {
            this.parentScene.sndWin.play();
        }

        this.state = RM_STATE_S_SCATTER_WIN;
    }

    beginSpinReels ()
    {
        this.parentScene.txtWin.setVisible(false);

        this.nmReelsStopped = 0;
        this.canShowStopBtn = true;

        this.scatterCount = 0;

        this.moveCount = 0;

        this.animationsComplete = 0;
        this.animationsTotal = 0;

        this.wp = 0;
        this.sp = 0;

        if (!this.parentScene.isFastSpin())
        {
            if (this.parentScene.gameState == GAME_STATE_FREE_GAMES)
            {
                this.parentScene.sndBackgroundFreeGames.play();
            }
            else
            {
                this.parentScene.sndBackground.play();
            }
        }

        this.reels.forEach(reel => {reel.beginSpin();});

        this.state = RM_STATE_SPIN;
    }

    reelStopped (reelId)
    {
        this.parentScene.reelStopped();

        ++this.nmReelsStopped;

        if (this.reels[reelId].haveScatter)
        {
            ++this.scatterCount;
        }
        
        if (!this.reels[reelId].forceStopped && (this.nmReelsStopped < this.reels.length))
        {
            if (this.scatterCount == 2 && reelId < 4)
            {
                this.state = RM_STATE_EXTENDED_SPIN;

                this.pauseBackgroundSounds(true);
                
                this.parentScene.sndBackgroundLongMovement.play();

                this.reels[reelId + 1].specialSpin(EXTENDED_SPIN_DURATION);
        
                this.parentScene.sound.play("longMVS");

                switch (reelId + 1)
                {
                    case 2: this.garlandEmitter.start(0);break;
                    case 3: this.garlandEmitter.start(1);break;
                    case 4: this.garlandEmitter.start(2);break;
                }
             }
            else if (this.state == RM_STATE_EXTENDED_SPIN && this.scatterCount > 2)
            {
                if (this.nmReelsStopped < this.reels.length)
                {
                    this.reels[reelId + 1].specialSpin(EXTENDED_SPIN_DURATION);
        
                    this.parentScene.sound.play("longMVS");

                    switch (reelId + 1)
                    {
                        case 3: this.garlandEmitter.start(1);break;
                        case 4: this.garlandEmitter.start(2);break;
                    }
                }
            }
        }

        if (this.nmReelsStopped == this.reels.length)
        {
            if (this.parentScene.gameState == GAME_STATE_ERROR && this.parentScene.betlimit.scene.isVisible())
            {
                return;
            }

            if (this.garlandEmitter.emitting)
            {
                this.garlandEmitter.stop();
            }

            this.state = RM_STATE_SPIN;
            
            this.parentScene.time.delayedCall(250, this.processWin, [], this);
        }
    }

    processWin()
    {
        this.pauseBackgroundSounds(false);

        this.reels.forEach(reel => {reel.stopLandingAnimations();});

        let tw = this.parentScene.getTotalWin();

        if (this.parentScene.gameState == GAME_STATE_SPIN && this.spinResult.sScatters > 2 && this.parentScene.spinHoldActive)
        {
            this.parentScene.spinBtn.clearHold();
            this.parentScene.spinBtn.sleep();
        }

        if (tw == 0)
        {
            if (this.spinResult.sScatters > 2)
            {
                this.playScatters();

                return;
            }

            if (this.parentScene.gameState == GAME_STATE_FREE_GAMES)
            {
                this.parentScene.time.delayedCall(250, this.playFreeGame, [], this);
            }
            else
            {                           
                this.state = RM_STATE_READY;
                this.parentScene.onReelsReady();
            }
        }
        else
        { 
            this.parentScene.time.delayedCall(250, this.parentScene.onTotalWin, [], this.parentScene);
        }
    }

    setState (state)
    {
        this.state = state;
    }

    getState ()
    {
        return this.state;
    }

    playGame (spinResult)
    {
        this.spinResult = spinResult;

        this.reels.forEach((reel, i) => { reel.setSymbolField(this.spinResult.symbolField[i], this.spinResult.symbolFieldAS[i]) });

        this.setScatters();

        this.winLines = this.spinResult.winLines;
        this.winLinesAS = this.spinResult.winLinesAS;

        this.winLine = 0;

        this.reels.forEach(reel => {reel.setWinLines(this.winLines, this.winLinesAS);});

        this.groupedSymbols = [[0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0]];

        for (let i = 0; i < this.winLines.length; ++i)
        {
            let len = this.winLines[i].len;

            for (let j = 0; j < len; ++j)
            {
                let v = this.winLines[i].line[j];

                switch (v)
                {
                    case -1: {this.groupedSymbols[0][j] = 1;};break;
                    case 0: {this.groupedSymbols[1][j] = 1;};break;
                    case 1: {this.groupedSymbols[2][j] = 1;};break;
                }
            }
        }

        this.groupedSymbolsAS = [[0, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 0]];

        if (this.spinResult.reelSwapIndex != -1)
        {
            for (let i = 0; i < this.winLinesAS.length; ++i)
            {
                let len = this.winLinesAS[i].len;

                for (let j = 0; j < len; ++j)
                {
                    let v = this.winLinesAS[i].line[j];

                    switch (v)
                    {
                        case -1: {this.groupedSymbolsAS[0][j] = 1;};break;
                        case 0: {this.groupedSymbolsAS[1][j] = 1;};break;
                        case 1: {this.groupedSymbolsAS[2][j] = 1;};break;
                    }
                }
            }
        }

        if ((this.spinResult.sScatters != undefined && this.spinResult.sScatters < 2))
        {
            for (let i = 0; i < this.reels.length; ++i)
            {
                this.reels[i].setSpinTime(this.parentScene.responseTime, i * this.extraSpinTime);

                if (this.parentScene.isFastSpin())
                {
                    this.reels[i].fastSpin();
                }
                else
                {
                    this.reels[i].normalSpin();
                }
            }
        }
        else
        {
            let sc = 0;

            for (let i = 0; i < this.reels.length; ++i)
            {
                if (this.reels[i].haveScatter)
                {
                    ++sc;
                }

                this.reels[i].setSpinTime(this.parentScene.responseTime, i * this.extraSpinTime);

                if (this.parentScene.isFastSpin())
                {
                    this.reels[i].fastSpin();
                }
                else
                {
                    this.reels[i].normalSpin();
                }

                if (sc == 2)
                {
                    break;
                }
            }
        }
    }

    setScatters ()
    {
        let sc = 0;

        for (let i = 0; i < this.reels.length; ++i)
        {
            for (let j = 0; j < 3; ++j)
            {
                if (this.spinResult.sScattersField[i][j])
                {
                    switch (i)
                    {
                        case 0:
                        case 1:
                        case 2:{this.reels[i].setScatter(); ++sc;};break;
                        case 3:{if (sc > 0) {this.reels[i].setScatter()}; ++sc;}break;
                        case 4:{if (sc > 1) {this.reels[i].setScatter()};}break;
                    }
                }
            }
        }
    }

    playFreeGame ()
    {
        let spinResult = this.parentScene.getNextFreeGame();
        
        if (spinResult != null)
        {
            this.beginSpinReels ();

            this.parentScene.fg.freeGameStart();

            this.playGame(spinResult);
        }
        else
        {
            this.state = RM_STATE_READY;

            this.parentScene.sndBackgroundFreeGames.stop();

            this.waitReelsToComplete();
        }
    }

    setFreeGameReels ()
    {
        this.reels.forEach(reel => {reel.setFGReel();});

        this.reels[1].setSpinDirection(REEL_UP);
        this.reels[3].setSpinDirection(REEL_UP);
    }

    setNormalGameReels ()
    {
        this.reels.forEach(reel => {reel.setNGReel();});

        this.reels[1].setSpinDirection(REEL_DOWN);
        this.reels[3].setSpinDirection(REEL_DOWN);
    }

    waitReelsToComplete ()
    {
        if (this.nmReelsStopped == this.reels.length)
        {
            this.parentScene.freeGamesEnd();
        }
        else
        {
            this.time.delayedCall(150, this.waitReelsToComplete, [], this);
        }
    }

    animationComplete ()
    {
        if (this.state == RM_STATE_TOTAL_WIN)
        {
            if (++this.animationsComplete == this.animationsTotal)
            {
                this.animationsComplete = 0;

                this.reels.forEach(reel => reel.resetSymbolsZOrder());

                if (this.spinResult.sScatters > 2)
                {
                    this.playScatters();
                }
                else
                {
                    this.showWinCounter();
                }
            }
        }
        else if (this.state == RM_STATE_BEFORE_SWAP || this.state == RM_STATE_AFTER_SWAP)
        {
            if (++this.animationsComplete == this.animationsTotal)
            {
                this.animationsComplete = 0;

                this.reels.forEach(reel => reel.resetSymbolsZOrder());

                if (this.state == RM_STATE_BEFORE_SWAP)
                {
                     this.swapReel(this.spinResult.reelSwapIndex, this.spinResult.symbolFieldAS);
                }
                else
                {
                    this.swapReel(this.spinResult.reelSwapIndex, this.spinResult.symbolField);
                }
            }
        }
        else if (this.state == RM_STATE_S_SCATTER_WIN)
        {
            if (++this.animationsComplete == this.animationsTotal)
            {
                this.animationsComplete = 0;

                this.reels.forEach(reel => reel.resetSymbolsZOrder());

                if (this.parentScene.getTotalWin() == 0)
                {
                    this.parentScene.time.delayedCall(2000, this.parentScene.playFreeGames, [], this.parentScene);
                }
                else
                {
                    this.parentScene.time.delayedCall(2000, this.showWinCounter, [], this);
                }
            }
        }
        else if (this.state == RM_STATE_PLAY_WIN)
        {
            if (++this.animationsComplete == this.winLines[this.winLine].len)
            {
                this.animationsComplete = 0;

                this.reels.forEach(reel => reel.resetSymbolsZOrder());
            }
        }
    }

    onOrientationChange ()
    {
        if (this.parentScene.isLandscapeMode())
        {
            this.container.setPosition(960, 608);
        }
        else
        {
            this.container.setPosition(540, 944);
        }

        if (this.visible)
        {
            this.reelPanel.setVisible(true);
        }

        this.reels.forEach(reel => {reel.onOrientationChange();});

        this.garlandEmitter.setPosition();
    }

    setVisible (v)
    {
        this.visible = v;

        this.reelPanel.setVisible(v);

        this.reels.forEach(reel => {reel.setVisible(v);});
    }

    stopReels ()
    {
        this.state = RM_STATE_SPIN;

        this.pauseBackgroundSounds (false);

        this.parentScene.onFastStop();

        this.reels.forEach(reel => {reel.stop();});
    }

    setReelDirection (v)
    {
        this.reels[1].setSpinDirection(v);
        this.reels[3].setSpinDirection(v);
    }

    errStopReels ()
    {
        this.reels.forEach(reel => {reel.errStop();});
    }

    pauseBackgroundSounds (isInstant)
    {
        if (this.state == RM_STATE_SPIN && this.parentScene.sndBackgroundLongMovement.isPlaying())
        {
            this.parentScene.sndBackgroundLongMovement.stop(isInstant);
        }
        
        if (this.parentScene.gameState == GAME_STATE_FREE_GAMES)
        {
            this.parentScene.sndBackgroundFreeGames.pause(isInstant);
        }
        else
        {
            this.parentScene.sndBackground.pause(isInstant);
        }
    }

    hideStopBtn ()
    {
        if (this.parentScene.stopBtn.scene.isVisible() && !this.parentScene.spinHoldActive)
        {
            this.parentScene.stopBtn.sleep(true);
        }

        this.canShowStopBtn = false;
    }

    getLineWin ()
    {
        return formatValue(this.parentScene.gameSocket.getUnits() * this.parentScene.gameSocket.getBetPerLine() * this.winLines[this.winLine].win, this.parentScene.currencyPrecision);
    }

    swapReel (reelId, symbolField)
    {
        this.reels.forEach(reel => { reel.destroyAnimations(); });

        let tx = this.reels[reelId].container.x;
        let ty = this.reels[reelId].container.y;

        let x = this.reels[4].container.x;
        let y = this.reels[4].container.y;

        this.container.moveBelow(this.reels[4].container, this.reels[3].container);
        this.container.moveBelow(this.reels[4].container, this.reels[2].container);
        this.container.moveBelow(this.reels[4].container, this.reels[1].container);
        this.container.moveBelow(this.reels[4].container, this.reels[0].container);

        this.moveReel (this.reels[4].container, tx, ty, x, y, 4, symbolField);//!

        tx = x;
        ty = y;

        x = this.reels[reelId].container.x;
        y = this.reels[reelId].container.y;

        for (let i = 0; i < this.reels.length; ++i)
        {
            if (i != reelId)
            {
                this.container.moveBelow(this.reels[i].container, this.reels[reelId].container);
            }
        }

        this.moveReel (this.reels[reelId].container, tx, ty, x, y, reelId, symbolField);//!
    }

    moveReel (container, tx, ty, x, y, reelId, symbolField)
    {
        this.parentScene.tweens.add({

            targets: container,
            ease: "Linear",
            duration: 1000,
            x: tx,
            y: ty,

            callbackScope: this,
            onComplete: function () 
            {
                container.setPosition(x, y);

                if (reelId == 4)
                {
                    if (this.state == RM_STATE_BEFORE_SWAP)
                    {
                        let si = this.spinResult.reelSwapIndex;

                        this.reels[reelId].sprites[0].setTextureKey(SYMBOLS[symbolField[reelId][0]], this.reels[si].sprites[0].hasLanding);
                        this.reels[reelId].sprites[1].setTextureKey(SYMBOLS[symbolField[reelId][1]], this.reels[si].sprites[1].hasLanding);
                        this.reels[reelId].sprites[2].setTextureKey(SYMBOLS[symbolField[reelId][2]], this.reels[si].sprites[2].hasLanding);
                    }
                    else
                    {
                        this.reels[reelId].sprites[0].setTextureKey(SYMBOLS[symbolField[reelId][0]], false);
                        this.reels[reelId].sprites[1].setTextureKey(SYMBOLS[symbolField[reelId][1]], false);
                        this.reels[reelId].sprites[2].setTextureKey(SYMBOLS[symbolField[reelId][2]], false);
                    }
                }
                else
                {
                    if (this.state == RM_STATE_AFTER_SWAP)
                    {
                        this.reels[reelId].sprites[0].setTextureKey(SYMBOLS[symbolField[reelId][0]], this.reels[4].sprites[0].hasLanding);
                        this.reels[reelId].sprites[1].setTextureKey(SYMBOLS[symbolField[reelId][1]], this.reels[4].sprites[1].hasLanding);
                        this.reels[reelId].sprites[2].setTextureKey(SYMBOLS[symbolField[reelId][2]], this.reels[4].sprites[2].hasLanding);
                    }
                    else
                    {
                        this.reels[reelId].sprites[0].setTextureKey(SYMBOLS[symbolField[reelId][0]], false);
                        this.reels[reelId].sprites[1].setTextureKey(SYMBOLS[symbolField[reelId][1]], false);
                        this.reels[reelId].sprites[2].setTextureKey(SYMBOLS[symbolField[reelId][2]], false);
                    }
                }

                this.reels[reelId].restoreMaskAfterSwap();

                this.moveComplete();
            },
        });

        this.reels[reelId].updateSwapMask();
    }

    moveReel2 (container, tx, ty, x, y, reelId, symbolField)
    {
        container.setPosition(x, y);

        this.reels[reelId].updateSwapMask();

        this.reels[reelId].sprites[0].setTextureKey(SYMBOLS[symbolField[reelId][0]], false);
        this.reels[reelId].sprites[1].setTextureKey(SYMBOLS[symbolField[reelId][1]], false);
        this.reels[reelId].sprites[2].setTextureKey(SYMBOLS[symbolField[reelId][2]], false);

        this.reels[reelId].restoreMaskAfterSwap();

        this.moveComplete();
    }


    moveComplete ()
    {
        if (++this.moveCount == 2)
        {
            if (this.state == RM_STATE_BEFORE_SWAP)
            {
                this.moveCount = 0;
                
                this.playWinAfterSwap();
            }
            else
            {
                this.playNormalWin();
            }
        }
    }
}    