cc.Class({
    extends: cc.Component,

    properties: {

    },
    onLoad () {
        this.videoTotalIndexNumArr = [
            25,//1
            27,//2
            21,//3
            52,//4
            0,//5
            25,//6
            35,//7 71,//7
            52,//8
            54,//9
            51,//10
            52,//11
            42,//12
            34,//13
            47,//14
            45,//15
            46,//16
            52,//17
            53,//18
            21,//19
            27,//20
            35,//21
            0,//22
            0,//23
            32,//24
            46,//25
        ];

        this.videoFrameArr = [];
        this.currentRound = 0;
    },
    play(node,index,self,callback)
    {
        let sprite = node.getComponent(cc.Sprite);
        // console.log(this.videoFrameArr)
        this.currentPlayer =  this.playLocalVideo(this.videoFrameArr,sprite,function () {
            callback(self);
        });
    },
    prepareCurrentRoundVideo(index,callback)
    {
        let self = this;
        this.currentRound = index;
        if(index!=0)
        {
            this.stop();
            for(let i=0;i<this.videoFrameArr.length;i++)
            {
                cc.loader.release(cc.loader.getDependsRecursively(this.videoFrameArr[i]));
            }

            this.videoFrameArr.length = 0;
        }

        this.scheduleOnce(function () {
            let forderName = 'Videos/'+(index+1).toString();
            let from  = 0;
            let to    = self.videoTotalIndexNumArr[index];
            if(to == 0) to = 1;

            self.prepareLocalPic(forderName,from,to,function () {
                callback();
            });
        },0.5);
    },
    prepareLocalPic(forderName,from,to,callback)
    {
        let self = this;

        let index = 0;
        let download = function()
        {
            if((index+from) == to)
            {
                callback();
                return ;
            }

            let path = forderName+'/'+(index+from).toString();

            cc.loader.loadRes(path, cc.SpriteFrame, function (err, spriteFrame) {
                self.videoFrameArr.push(spriteFrame);
                // console.log('path=',path)
                index++;
                download();
            });
        };

        download();
    },
    playLocalVideo(picArr,sprite,callback)
    {
        let index = 0;

        let playMovie = function () {

            if(index==picArr.length)
            {
                this.unschedule(playMovie);
                callback();
                return;
            }

            sprite.spriteFrame = picArr[index];

            index++;
        };

        this.schedule(playMovie,1/30);

        return playMovie;
    },
    stop()
    {
        // this.unschedule(this.currentPlayer);
        this.unscheduleAllCallbacks();
    },
});
