cc.Class({
    extends: cc.Component,

    properties: {
        GameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameConfig: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameTalent: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameMakeUp: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameEloquence: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameFashion: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameDance: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    OnActive()
    {
        //Game Logic
        this.GameConfigScript = this.GameConfig.getComponent('GameConfig');
        this.GameStatesScript = this.GameStates.getComponent('GameStates');
        //Talent
        this.TalentStartCourseBtn = this.GameTalent.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.TalentFinishBtn = this.GameTalent.getChildByName('UnlockBtn').getChildByName('FinishBtn');

        this.TalentTotalTimeLabel = this.GameTalent.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.TalentTotalTimeScript = this.GameTalent.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');

        this.TalentTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.TalentTotalTime);

        this.TalentProgressBar = this.GameTalent.getChildByName('Panel').getChildByName('ProgressBar');

        this.TalentFinishCourseFrame = this.GameTalent.getChildByName('Panel').getChildByName('GameFinishCourseFrame');

        //MakeUp
        this.MakeUpStartCourseBtn = this.GameMakeUp.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.MakeUpFinishBtn = this.GameMakeUp.getChildByName('UnlockBtn').getChildByName('FinishBtn');

        this.MakeUpTotalTimeLabel = this.GameMakeUp.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.MakeUpTotalTimeScript = this.GameMakeUp.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');

        this.MakeUpTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MakeUpTotalTime);

        this.MakeUpProgressBar = this.GameMakeUp.getChildByName('Panel').getChildByName('ProgressBar');

        this.MakeUpFinishCourseFrame = this.GameMakeUp.getChildByName('Panel').getChildByName('GameFinishCourseFrame');

        //GameEloquence
        this.EloquenceStartCourseBtn = this.GameEloquence.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.EloquenceFinishBtn = this.GameEloquence.getChildByName('UnlockBtn').getChildByName('FinishBtn');

        this.EloquenceTotalTimeLabel = this.GameEloquence.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.EloquenceTotalTimeScript = this.GameEloquence.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');

        this.EloquenceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.EloquenceTotalTime);

        this.EloquenceProgressBar = this.GameEloquence.getChildByName('Panel').getChildByName('ProgressBar');

        this.EloquenceFinishCourseFrame = this.GameEloquence.getChildByName('Panel').getChildByName('GameFinishCourseFrame');

        // Fashion
        this.FashionStartCourseBtn = this.GameFashion.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.FashionFinishBtn = this.GameFashion.getChildByName('UnlockBtn').getChildByName('FinishBtn');

        this.FashionTotalTimeLabel = this.GameFashion.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.FashionTotalTimeScript = this.GameFashion.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');

        this.FashionTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.FashionTotalTime);

        this.FashionProgressBar = this.GameFashion.getChildByName('Panel').getChildByName('ProgressBar');

        this.FashionFinishCourseFrame = this.GameFashion.getChildByName('Panel').getChildByName('GameFinishCourseFrame');
        // Dance
        this.DanceStartCourseBtn = this.GameDance.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.DanceFinishBtn = this.GameDance.getChildByName('UnlockBtn').getChildByName('FinishBtn');

        this.DanceTotalTimeLabel = this.GameDance.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.DanceTotalTimeScript = this.GameDance.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');

        this.DanceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.DanceTotalTime);

        this.DanceProgressBar = this.GameDance.getChildByName('Panel').getChildByName('ProgressBar');

        this.DanceFinishCourseFrame = this.GameDance.getChildByName('Panel').getChildByName('GameFinishCourseFrame');


        this.GetRemainTime(function () {
            // Talent
            if( this.GameStatesScript.TalentRemainTime>0)
            {
                this.TalentFinishBtn.active=true;
                this.TalentFinishBtn.getComponent(cc.Button).interactable = false;
                this.TalentStartCourseBtn.active = false;
                this.ShowProgressBar('Talent');
            }
            else
            {
                if(this.GameStatesScript.BTalentFinished)
                {
                    this.TalentFinishBtn.active=false;
                    this.TalentStartCourseBtn.active = true;
                }
                else {
                    this.TalentFinishBtn.active=true;
                    this.TalentFinishBtn.getComponent(cc.Button).interactable = true;
                    this.TalentStartCourseBtn.active = false;
                    this.TalentFinishCourseFrame.active=true;
                }
            }
            // MakeUp
            if( this.GameStatesScript.MakeUpRemainTime>0)
            {
                this.MakeUpFinishBtn.active=true;
                this.MakeUpFinishBtn.getComponent(cc.Button).interactable = false;
                this.MakeUpStartCourseBtn.active = false;
                this.ShowProgressBar('MakeUp');
            }
            else
            {
                if(this.GameStatesScript.BMakeUpFinished)
                {
                    this.MakeUpFinishBtn.active=false;
                    this.MakeUpStartCourseBtn.active = true;
                }
                else {
                    this.MakeUpFinishBtn.active=true;
                    this.MakeUpFinishBtn.getComponent(cc.Button).interactable = true;
                    this.MakeUpStartCourseBtn.active = false;
                    this.MakeUpFinishCourseFrame.active=true;
                }
            }
            // Eloquence
            if( this.GameStatesScript.EloquenceRemainTime>0)
            {
                this.EloquenceFinishBtn.active=true;
                this.EloquenceFinishBtn.getComponent(cc.Button).interactable = false;
                this.EloquenceStartCourseBtn.active = false;
                this.ShowProgressBar('Eloquence');
            }
            else
            {
                if(this.GameStatesScript.BEloquenceFinished)
                {
                    this.EloquenceFinishBtn.active=false;
                    this.EloquenceStartCourseBtn.active = true;
                }
                else {
                    this.EloquenceFinishBtn.active=true;
                    this.EloquenceFinishBtn.getComponent(cc.Button).interactable = true;
                    this.EloquenceStartCourseBtn.active = false;
                    this.EloquenceFinishCourseFrame.active=true;
                }
            }
            // Fashion
            if( this.GameStatesScript.FashionRemainTime>0)
            {
                this.FashionFinishBtn.active=true;
                this.FashionFinishBtn.getComponent(cc.Button).interactable = false;
                this.FashionStartCourseBtn.active = false;
                this.ShowProgressBar('Fashion');
            }
            else
            {
                if(this.GameStatesScript.BFashionFinished)
                {
                    this.FashionFinishBtn.active=false;
                    this.FashionStartCourseBtn.active = true;
                }
                else {
                    this.FashionFinishBtn.active=true;
                    this.FashionFinishBtn.getComponent(cc.Button).interactable = true;
                    this.FashionStartCourseBtn.active = false;
                    this.FashionFinishCourseFrame.active=true;
                }
            }
            // Dance
            if( this.GameStatesScript.DanceRemainTime>0)
            {
                this.EloquenceFinishBtn.active=true;
                this.EloquenceFinishBtn.getComponent(cc.Button).interactable = false;
                this.EloquenceStartCourseBtn.active = false;
                this.ShowProgressBar('Eloquence');
            }
            else
            {
                if(this.GameStatesScript.BEloquenceFinished)
                {
                    this.EloquenceFinishBtn.active=false;
                    this.EloquenceStartCourseBtn.active = true;
                }
                else {
                    this.EloquenceFinishBtn.active=true;
                    this.EloquenceFinishBtn.getComponent(cc.Button).interactable = true;
                    this.EloquenceStartCourseBtn.active = false;
                    this.EloquenceFinishCourseFrame.active=true;
                }
            }
            // Fashion
            // Dance
        }.bind(this));
    },
    GetRemainTime(CallBack)
    {
        let URL = this.GameConfigScript.Host+'GameCourse/GetCourseRemainTime?Openid='+ cc.sys.localStorage.getItem('Openid');
        let XHR = new XMLHttpRequest();

        XHR.onreadystatechange = function () {
            if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400)) {
                let response = XHR.responseText;
                // console.log(response);
                let ResultJson = JSON.parse(response);

                this.GameStatesScript.TalentRemainTime = ResultJson.TalentRemainTime;
                this.GameStatesScript.BTalentFinished = ResultJson.BTalentFinished;

                this.GameStatesScript.MakeUpRemainTime = ResultJson.MakeUpRemainTime;
                this.GameStatesScript.BMakeUpFinished = ResultJson.BMakeUpFinished;

                this.GameStatesScript.EloquenceRemainTime = ResultJson.EloquenceRemainTime;
                this.GameStatesScript.BEloquenceFinished = ResultJson.BEloquenceFinished;

                // Fashion
                this.GameStatesScript.FashionRemainTime = ResultJson.FashionRemainTime;
                this.GameStatesScript.BFashionFinished = ResultJson.BFashionFinished;
                // Dance
                this.GameStatesScript.DanceRemainTime = ResultJson.DanceRemainTime;
                this.GameStatesScript.BDanceFinished = ResultJson.BDanceFinished;

                CallBack();
            }
        }.bind(this);
        XHR.open("GET", URL, true);
        XHR.setRequestHeader("Content-Type" , "application/json");
        XHR.send();
    },
    OnStarCourse(event, customEventData)
    {
        let CourseName = customEventData;
        let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid');
        let URL = this.GameConfigScript.Host+'GameCourse/Start'+CourseName+'Course/';
        let XHR = new XMLHttpRequest();

        XHR.onreadystatechange = function ()
        {
            if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
            {
                let response = XHR.responseText;
                console.log(response);
                if(CourseName == 'Talent')
                {
                    this.TalentStartCourseBtn.active = false;
                    this.TalentFinishBtn.active = true;
                }
                else if(CourseName == 'MakeUp')
                {
                    this.MakeUpStartCourseBtn.active = false;
                    this.MakeUpFinishBtn.active = true;
                }
                else if(CourseName == 'Eloquence')
                {
                    this.EloquenceStartCourseBtn.active = false;
                    this.EloquenceFinishBtn.active = true;
                }
                else if(CourseName == 'Fashion')
                {
                    this.FashionStartCourseBtn.active = false;
                    this.FashionFinishBtn.active = true;
                }
                else if(CourseName == 'Dance')
                {
                    this.DanceStartCourseBtn.active = false;
                    this.DanceFinishBtn.active = true;
                }

                this.UpdateGameStates(response);
                this.GetRemainTime(function () {
                    this.ShowProgressBar(CourseName);
                }.bind(this));

            }
        }.bind(this);
        XHR.open("POST", URL, false);
        XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        XHR.send(Data);
    },
    ShowProgressBar(CourseName)
    {
        let CourseScript = null;
        let TotalTime = 0;
        let RemainTime = 0;
        if(CourseName=='Talent')
        {
            this.TalentProgressBar.active = true;
           CourseScript = this.TalentTotalTimeScript;
           TotalTime= this.GameConfigScript.TalentTotalTime;
           RemainTime = this.GameStatesScript.TalentRemainTime;
        }
        else if(CourseName=='MakeUp')
        {
            this.MakeUpProgressBar.active = true;
            CourseScript = this.MakeUpTotalTimeScript;
            TotalTime= this.GameConfigScript.MakeUpTotalTime;
            RemainTime = this.GameStatesScript.MakeUpRemainTime;
        }
        else if(CourseName=='Eloquence')
        {
            this.EloquenceProgressBar.active = true;
            CourseScript = this.EloquenceTotalTimeScript;
            TotalTime= this.GameConfigScript.EloquenceTotalTime;
            RemainTime = this.GameStatesScript.EloquenceRemainTime;
        }
        else if(CourseName=='Fashion')
        {
            this.FashionProgressBar.active = true;
            CourseScript = this.FashionTotalTimeScript;
            TotalTime= this.GameConfigScript.FashionTotalTime;
            RemainTime = this.GameStatesScript.FashionRemainTime;
        }
        else if(CourseName=='Dance')
        {
            this.DanceProgressBar.active = true;
            CourseScript = this.DanceTotalTimeScript;
            TotalTime= this.GameConfigScript.DanceTotalTime;
            RemainTime = this.GameStatesScript.DanceRemainTime;
        }

        // 重复次数
        let Repeat = cc.REPEAT_FOREVER;
        // 以秒为单位的时间间隔
        let Interval = 1;
        // 开始延时
        let Delay = 0.01;

        let Index = Math.floor(RemainTime);

        let StartCourseSchedule = function()
        {
            Index--;
            CourseScript.SetProgressBar(Index,TotalTime);

            if(0==Index)
            {
                this.FinishCourse(CourseName);
                this.unschedule(StartCourseSchedule);
            }
        }.bind(this);

        this.schedule(StartCourseSchedule, Interval, Repeat-1, Delay);
    },
    FinishCourse(CourseName)
    {
        // console.log(CourseName)
        if(CourseName=='Talent')
        {
            this.TalentFinishBtn.getComponent(cc.Button).interactable = true;
            this.TalentProgressBar.active = false;
            this.TalentTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.TalentTotalTime);
            this.TalentFinishCourseFrame.active=true;
        }
        else if(CourseName=='MakeUp')
        {
            this.MakeUpFinishBtn.getComponent(cc.Button).interactable = true;
            this.MakeUpProgressBar.active = false;
            this.MakeUpTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MakeUpTotalTime);
            this.MakeUpFinishCourseFrame.active=true;
        }
        else if(CourseName=='Eloquence')
        {
            this.EloquenceFinishBtn.getComponent(cc.Button).interactable = true;
            this.EloquenceProgressBar.active = false;
            this.EloquenceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.EloquenceTotalTime);
            this.EloquenceFinishCourseFrame.active=true;
        }
        else if(CourseName=='Fashion')
        {
            this.FashionFinishBtn.getComponent(cc.Button).interactable = true;
            this.FashionProgressBar.active = false;
            this.FashionTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.FashionTotalTime);
            this.FashionFinishCourseFrame.active=true;
        }
        else if(CourseName=='Dance')
        {
            this.DanceFinishBtn.getComponent(cc.Button).interactable = true;
            this.DanceProgressBar.active = false;
            this.DanceTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.DanceTotalTime);
            this.DanceFinishCourseFrame.active=true;
        }
    },
    OnCourseFinished(event, customEventData)
    {
        let CourseName = customEventData;
        if(CourseName=='Talent')
        {
            this.TalentFinishBtn.getComponent(cc.Button).interactable = false;
            this.TalentFinishBtn.active = false;
            this.TalentStartCourseBtn.active = true;
            this.TalentFinishCourseFrame.active=false;
        }
        else if(CourseName=='MakeUp')
        {
            this.MakeUpFinishBtn.getComponent(cc.Button).interactable = false;
            this.MakeUpFinishBtn.active = false;
            this.MakeUpStartCourseBtn.active = true;
            this.MakeUpFinishCourseFrame.active=false;
        }
        else if(CourseName=='Eloquence')
        {
            this.EloquenceFinishBtn.getComponent(cc.Button).interactable = false;
            this.EloquenceFinishBtn.active = false;
            this.EloquenceStartCourseBtn.active = true;
            this.EloquenceFinishCourseFrame.active=false;
        }
        else if(CourseName=='Fashion')
        {
            this.FashionFinishBtn.getComponent(cc.Button).interactable = false;
            this.FashionFinishBtn.active = false;
            this.FashionStartCourseBtn.active = true;
            this.FashionFinishCourseFrame.active=false;
        }
        else if(CourseName=='Dance')
        {
            this.DanceFinishBtn.getComponent(cc.Button).interactable = false;
            this.DanceFinishBtn.active = false;
            this.DanceStartCourseBtn.active = true;
            this.DanceFinishCourseFrame.active=false;
        }

        let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid');
        let URL = this.GameConfigScript.Host+'GameCourse/Finish'+CourseName+'/';
        // console.log(URL)
        let XHR = new XMLHttpRequest();

        XHR.onreadystatechange = function ()
        {
            if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
            {
                let response = XHR.responseText;

                this.UpdateGameStates(response);
            }
        }.bind(this);
        XHR.open("POST", URL, false);
        XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        XHR.send(Data);
    },
    UpdateGameStates(response)
    {
        let GameStates = JSON.parse(response);
        // console.log(ResultJson);
        // Game
        // let GameStates = ResultJson.GameConfig;
        // console.log(GameConfig);
        this.GameStatesScript.PlusActionPower(GameStates.ActionPower,false);
        this.GameStatesScript.PlusCoin(GameStates.Coin);
        this.GameStatesScript.PlusDiamond(GameStates.Diamond);
        this.GameStatesScript.PlusFans(GameStates.Fans);
        this.GameStatesScript.PlusFavorableImpression(GameStates.FavorableImpression);
    },
    CastSecondsToHours(Time)
    {
        if(Time<60) return Time+'秒';

        if(Time<3600 && Time >=60)
        {
            return Math.floor(Time/60)+'分'+Time%60+'秒';
        }
        if(Time>=3600)
        {
            let Hour = Math.floor(Time/3600);
            let s = Time%3600;
            let Min = Math.floor(s/60);
            return Hour+'小时'+Min+'分';
        }
        return Math.floor(Time/3600);
    }
});
