class Reel {

    constructor (config)
    {
        this.parentScene = config.scene;
        this.rm = config.rm;
        
        this.reel = config.reel;//array, [0..n] integers

        this.reelId = config.reelId;
        this.position = config.reelPosition;//position at which reel must stop [0..n-1]
        
        this.reelRectWidth = 422;//width of reel rect
        this.reelRectHeight = 422;//height of reel rect
        
        this.container = this.parentScene.add.container(536, 963);//container x, y position, can vary
        this.container.setSize(this.reelRectWidth, this.reelRectHeight * 3);

        this.reeltween = new ReelTween({reel: this,
                                    scene: this.parentScene,
                                    duration: config.duration,
                                    backDuration: config.backDuration,
                                    backEase: config.backEase,
                                    spinTime: config.spinTime
                                });


        this.spinTime = 0;

        this.spinSymbol = EMPTY;
        this.suitSymbol = WILD;

        this.sprites = [];
        
        this.state = REEL_STATE_STOPPED;

        this.initReel();
    }

    initReel ()
    {
        let spr1 = this.parentScene.add.sprite(0, this.reelRectHeight + 40, this.getSymbolKey());//
        let spr2 = this.parentScene.add.sprite(0, 40, this.getSymbolKey());// center sprite, visible
        let spr3 = this.parentScene.add.sprite(0, -this.reelRectHeight + 40, this.getSymbolKey());//top/next sprite, invisible

        this.container.add([ spr1, spr2, spr3 ]);
        this.container.setDepth(4);

        this.clippingRect = this.parentScene.add.rectangle(540, 990, 422, 422, 0x9966ff).setVisible(false).setAlpha(0.5);
        
        this.mask = this.clippingRect.createGeometryMask();

        this.container.setMask(this.mask);
    }

    getSymbolKey ()
    {
        let sym = 1 + Math.floor(Math.random() * (BUST - 1))
        
        return SYMBOLS[sym];
    }

    update (time, delta) 
    {
    }

    startSpin ()
    {
        this.reeltween.startReel();
    }

    normalSpin()
    {
        this.reeltween.normalSpin(this.spinTime);
    }

    fastSpin()
    {
        this.reeltween.fastSpin();
    }

    beginSpin ()
    {
        if (this.sprites[1] != undefined && this.sprites[1].anims.currentAnim != null)
        {
            this.sprites[1].anims.stop();
            this.sprites[1].anims.currentAnim = null;
        }

        this.startSpin();
    }

    endSpin()
    {
        this.state = REEL_STATE_STOPPED;
        
        this.resetContainer();

        this.sprites[0] = this.container.list[0];
        this.sprites[1] = this.container.list[1];
        this.sprites[2] = this.container.list[2];

        if (this.spinSymbol != EMPTY)
        {
            this.sprites[1].play(this.parentScene.symAnims[this.spinSymbol - 1].key);
        }
        else
        {
            this.sprites[1].play(this.parentScene.suitAnims[this.suitSymbol - 1].key);
        }
        
        this.rm.reelStopped(this.reelId);
    }

    setSpinSymbol (symbol, suit)
    {
        this.spinSymbol = symbol;
        this.suitSymbol = suit;
    }

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

    getState ()
    {
        return this.state;
    }

    setSpinTime (time)
    {
        this.spinTime = time;
    }

    stop ()
    {
        this.rm.hideStopBtn();

        this.fastSpin();
                
        this.state = REEL_STATE_SPIN;
    }

    errStop ()
    {
        this.reeltween.errStop();
    }

    setVisible (v)
    {
        this.container.setVisible(v);
    }

    resetContainer ()
    {
        this.container.setPosition(536, 963);
        
        this.container.list[0].setPosition(0, this.reelRectHeight + 40);
        this.container.list[1].setPosition(0, 40);
        this.container.list[2].setPosition(0, -this.reelRectHeight + 40);
    }
}
