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

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

        this.spinPositions = [];

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

        this.reels = [];

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

            duration: 55,
            backDuration: 280,
            backEase: "Quart.easeOut",
            
            spinTime: REEL_SPIN_DURATION + this.parentScene.gameSocket.getExtraStartSpinTime()
        }            

        reelconf.reelId = 0;
        reelconf.reelPosition = this.spinPositions[0];
        reelconf.reel = config.reels[0];

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

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

    beginSpinReels ()
    {
        this.nmReelsStopped = 0;
        this.canShowStopBtn = true;

        this.parentScene.autoCollectPending = false;

        if (!this.parentScene.isFastSpin())
        {
            this.parentScene.sndBackground.play();
        }

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

        this.state = RM_STATE_SPIN;
    }

    setSpin ()
    {
        if (this.parentScene.isFastSpin())
        {
            this.reels.forEach(reel => { reel.fastSpin(); });
        }
        else
        {
            this.reels.forEach(reel => {reel.normalSpin();});
        }
    }

    reelStopped (reelId)
    {
        if (this.reels[reelId].state == REEL_STATE_STOPPED && ++this.nmReelsStopped == this.reels.length)
        {
            this.parentScene.resetTotalWin();

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

            this.parentScene.updateGameState();
            
            this.processWin();
        }
    }

    processWin ()
    {
        this.parentScene.sndBackground.pause(false);

        let tw = this.parentScene.getTotalWin();

        if (tw == 0)
        {
            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[0].setSpinSymbol(this.spinResult.spinSymbol, this.spinResult.suitSymbol);

        this.reels[0].setSpinTime(this.parentScene.responseTime);
        
        this.setSpin();
    }

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

    stopReels ()
    {
        this.parentScene.onFastStop();

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

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

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

        this.canShowStopBtn = false;
    }
}    