cc.Class({
    extends: cc.Component,

    properties: {
        GameConfig: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameActionPower: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameCoin: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameDiamond: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameFansNum: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameFavorableImpressionPercentage: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameProgressBar: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameLoadingPercentageValue: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameLoading: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    start () {
        this.GameConfigScript =this.GameConfig.getComponent('GameConfig');
        this.GameStatesScript =this.GameStates.getComponent('GameStates');
        this.TotoalProgressNum = 6;
        this.GetActionPower();
    },
    GetActionPower()
    {
        // console.log('GetAction');
        let  ActionPower = this.GameStatesScript.GetActionPower();

        if(ActionPower!=undefined)
        {
            this.GameActionPower.getComponent(cc.Label).string = ActionPower;
            this. SetProgressBarPercentage(1,  'Coin');
        }
        else {
            this.GameLoadingPercentageValue.getComponent(cc.Label).string = '网络连接超时'
        }
    },
    GetCoin()
    {
        // console.log('GetCoin');
        let Coin =  this.GameStatesScript.GetCoin();
        if(Coin!=undefined)
        {
            this.GameCoin.getComponent(cc.Label).string = Coin;
            this. SetProgressBarPercentage(2, 'Diamond');
        }
        else {
            this.GameLoadingPercentageValue.getComponent(cc.Label).string = '网络连接超时'
        }
    },
    GetDiamond()
    {
        let Diamond =  this.GameStatesScript.GetDiamond();
        if(Diamond!=undefined)
        {
            this.GameDiamond.getComponent(cc.Label).string = Diamond;
            this. SetProgressBarPercentage(3,'Fans');
        }
        else {
            this.GameLoadingPercentageValue.getComponent(cc.Label).string = '网络连接超时'
        }
    },
    GetFans()
    {
        let Fans =  this.GameStatesScript.GetFans();
        if(Fans!=undefined)
        {
            this.GameFansNum.getComponent(cc.Label).string = Fans;
            this. SetProgressBarPercentage(4,'FavorableImpression');
        }
        else {
            this.GameLoadingPercentageValue.getComponent(cc.Label).string = '网络连接超时'
        }
    },
    GetFavorableImpression()
    {
        let FavorableImpression =  this.GameStatesScript.GetFavorableImpression();
        if(FavorableImpression!=undefined)
        {
            this.GameFavorableImpressionPercentage.getComponent('ChangeFavorableImpressionLabelValue').setPercentage(FavorableImpression);//setPercentage(FavorableImpression);
            this. SetProgressBarPercentage(5,'GameConfig');
        }
        else {
            this.GameLoadingPercentageValue.getComponent(cc.Label).string = '网络连接超时'
        }
    },
    GetGameConfig ()
    {
        this.GameConfigScript.GetGameConfig(function ()
        {
            this. SetProgressBarPercentage(6,'Finished');
        }.bind(this));
    },
    LoadingFinished()
    {
        this.GameLoading.active = false;
    },
    SetProgressBarPercentage(ProgressIndex,FunctionName)
    {
        let CurrentPercentage = this.CastProportionToPercentage(ProgressIndex);
        let LastPercentage = this.CastProportionToPercentage(ProgressIndex-1);

        let Duration = 0.1;
        // 重复次数
        let repeat = Math.floor(CurrentPercentage-LastPercentage);
        // 以秒为单位的时间间隔
        let interval = Duration/repeat;
        // 开始延时
        let delay = 0;

        let Index = 0;

        this.schedule(function()
        {
            Index++;
            this.GameLoadingPercentageValue.getComponent(cc.Label).string = Math.floor(LastPercentage+Index)+'%';
            this.GameProgressBar.getComponent(cc.ProgressBar).progress= (LastPercentage+Index)/100;

            if(Index==repeat)
            {
                this.ExecuceNextFun(FunctionName);
            }
        }, interval, repeat-1, delay);
    },
    CastProportionToPercentage(ProgressIndex)
    {
        return ProgressIndex/this.TotoalProgressNum*100;
    },
    ExecuceNextFun(FunctionName)
    {
        // console.log('FunctionName='+FunctionName)
        if(FunctionName == 'Coin')
        {
            this.GetCoin();
        }
        else if(FunctionName == 'Diamond')
        {
            this.GetDiamond();
        }
        else if(FunctionName == 'Fans')
        {
            this.GetFans();
        }
        else if(FunctionName == 'FavorableImpression')
        {
            this.GetFavorableImpression();
        }
        else if(FunctionName == 'GameConfig')
        {
            this.GetGameConfig();
        }
        else if(FunctionName == 'Finished')
        {
            this.LoadingFinished();
        }
    }
});
