cc.Class({
    extends: cc.Component,

    properties: {
        GameStates: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameConfig: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        VarietyShow: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        Interview: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        Movie: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        HangOut: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    OnActive()
    {
        //Game Logic
        this.GameConfigScript = this.GameConfig.getComponent('GameConfig');
        this.GameStatesScript = this.GameStates.getComponent('GameStates');

        //VarietyShow
        //Button
        this.VarietyShowStartBtn = this.VarietyShow.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.VarietyShowFinishBtn = this.VarietyShow.getChildByName('UnlockBtn').getChildByName('FinishBtn');
        //TotalTime
        this.VarietyShowTotalTimeLabel = this.VarietyShow.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.VarietyShowTotalTimeScript = this.VarietyShow.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
        //ProgressBar
        this.VarietyShowProgressBar = this.VarietyShow.getChildByName('Panel').getChildByName('ProgressBar');

        //Interview
        //Button
        this.InterviewStartBtn = this.Interview.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.InterviewFinishBtn = this.Interview.getChildByName('UnlockBtn').getChildByName('FinishBtn');
        //TotalTime
        this.InterviewTotalTimeLabel = this.Interview.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.InterviewTotalTimeScript = this.Interview.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
        //ProgressBar
        this.InterviewProgressBar = this.Interview.getChildByName('Panel').getChildByName('ProgressBar');

        //Movie
        //Button
        this.MovieStartBtn = this.Movie.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.MovieFinishBtn = this.Movie.getChildByName('UnlockBtn').getChildByName('FinishBtn');
        //TotalTime
        this.MovieTotalTimeLabel = this.Movie.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.MovieTotalTimeScript = this.Movie.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
        //ProgressBar
        this.MovieProgressBar = this.Movie.getChildByName('Panel').getChildByName('ProgressBar');

        //HangOut
        //Button
        this.HangOutStartBtn = this.HangOut.getChildByName('UnlockBtn').getChildByName('StartCourseBtn');
        this.HangOutFinishBtn = this.HangOut.getChildByName('UnlockBtn').getChildByName('FinishBtn');
        //TotalTime
        this.HangOutTotalTimeLabel = this.HangOut.getChildByName('Panel').getChildByName('TotalTimeLabel');
        this.HangOutTotalTimeScript = this.HangOut.getChildByName('Panel').getChildByName('TotalTimeLabel').getComponent('ChangeTimeAndProgressBar');
        //ProgressBar
        this.HangOutProgressBar = this.HangOut.getChildByName('Panel').getChildByName('ProgressBar');

        this.ShowDefaultUIState();
    },
    ShowDefaultUIState()
    {
        this.GetRemainTime(function () {
            // VarietyShow
            if( this.GameStatesScript.VarietyShowRemainTime>0)
            {
                this.VarietyShowFinishBtn.active=true;
                this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = false;
                this.VarietyShowStartBtn.active = false;
                this.ShowProgressBar('VarietyShow');
            }
            else
            {
                if(this.GameStatesScript.BVarietyShowFinished)
                {
                    this.VarietyShowFinishBtn.active=false;
                    this.VarietyShowStartBtn.active = true;
                    this.VarietyShowTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.VarietyShowTotalTime);
                }
                else {
                    this.VarietyShowFinishBtn.active=true;
                    this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = true;
                    this.VarietyShowStartBtn.active = false;
                    this.VarietyShowTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
                }
            }

            // Interview
            if( this.GameStatesScript.InterviewRemainTime>0)
            {
                this.InterviewFinishBtn.active=true;
                this.InterviewFinishBtn.getComponent(cc.Button).interactable = false;
                this.InterviewStartBtn.active = false;
                this.ShowProgressBar('Interview');
            }
            else
            {
                if(this.GameStatesScript.BInterviewFinished)
                {
                    this.InterviewFinishBtn.active=false;
                    this.InterviewStartBtn.active = true;
                    this.InterviewTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.InterviewTotalTime);
                }
                else {
                    this.InterviewFinishBtn.active=true;
                    this.InterviewFinishBtn.getComponent(cc.Button).interactable = true;
                    this.InterviewStartBtn.active = false;
                    this.InterviewTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
                }
            }

            // Movie
            if( this.GameStatesScript.MovieRemainTime>0)
            {
                this.MovieFinishBtn.active=true;
                this.MovieFinishBtn.getComponent(cc.Button).interactable = false;
                this.MovieStartBtn.active = false;
                this.ShowProgressBar('Movie');
            }
            else
            {
                if(this.GameStatesScript.BMovieFinished)
                {
                    this.MovieFinishBtn.active=false;
                    this.MovieStartBtn.active = true;
                    this.MovieTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MovieTotalTime);
                }
                else {
                    this.MovieFinishBtn.active=true;
                    this.MovieFinishBtn.getComponent(cc.Button).interactable = true;
                    this.MovieStartBtn.active = false;
                    this.MovieTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
                }
            }

            // HangOut
            if( this.GameStatesScript.HangOutRemainTime>0)
            {
                this.HangOutFinishBtn.active=true;
                this.HangOutFinishBtn.getComponent(cc.Button).interactable = false;
                this.HangOutStartBtn.active = false;
                this.ShowProgressBar('HangOut');
            }
            else
            {
                if(this.GameStatesScript.BHangOutFinished)
                {
                    this.HangOutFinishBtn.active=false;
                    this.HangOutStartBtn.active = true;
                    this.HangOutTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.HangOutTotalTime);
                }
                else {
                    this.HangOutFinishBtn.active=true;
                    this.HangOutFinishBtn.getComponent(cc.Button).interactable = true;
                    this.HangOutStartBtn.active = false;
                    this.HangOutTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(0);
                }
            }
        }.bind(this));
    },
    GetRemainTime(CallBack)
    {
        let URL = this.GameConfigScript.Host+'GameEvent/GetEventRemainTime?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);

                // VarietyShow
                this.GameStatesScript.VarietyShowRemainTime = ResultJson.VarietyShowRemainTime;
                this.GameStatesScript.BVarietyShowFinished = ResultJson.BVarietyShowFinished;

                // Interview
                this.GameStatesScript.InterviewRemainTime = ResultJson.InterviewRemainTime;
                this.GameStatesScript.BInterviewFinished = ResultJson.BInterviewFinished;

                // Movie
                this.GameStatesScript.MovieRemainTime = ResultJson.MovieRemainTime;
                this.GameStatesScript.BMovieFinished = ResultJson.BMovieFinished;

                // HangOut
                this.GameStatesScript.HangOutRemainTime = ResultJson.HangOutRemainTime;
                this.GameStatesScript.BHangOutFinished = ResultJson.BHangOutFinished;

                CallBack();
            }
        }.bind(this);
        XHR.open("GET", URL, true);
        XHR.setRequestHeader("Content-Type" , "application/json");
        XHR.send();
    },
    OnStar(event, customEventData)
    {
        let CourseName = customEventData;
        let Data = 'Openid'+ cc.sys.localStorage.getItem('Openid');
        let URL = this.GameConfigScript.Host+'GameEvent/Start'+CourseName+'/';
        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 == 'VarietyShow')
                {
                    this.VarietyShowFinishBtn.active = true;
                    this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = false;
                    this.VarietyShowStartBtn.active = false;
                }
                else if(CourseName == 'Interview')
                {
                    this.InterviewFinishBtn.active = true;
                    this.InterviewFinishBtn.getComponent(cc.Button).interactable = false;
                    this.InterviewStartBtn.active = false;
                }
                else if(CourseName == 'Movie')
                {
                    this.MovieFinishBtn.active = true;
                    this.MovieFinishBtn.getComponent(cc.Button).interactable = false;
                    this.MovieStartBtn.active = false;
                }
                else if(CourseName == 'HangOut')
                {
                    this.HangOutFinishBtn.active = true;
                    this.HangOutFinishBtn.getComponent(cc.Button).interactable = false;
                    this.HangOutStartBtn.active = false;
                }

                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=='VarietyShow')
        {
            this.VarietyShowProgressBar.active = true;
            CourseScript = this.VarietyShowTotalTimeScript;
            TotalTime= this.GameConfigScript.VarietyShowTotalTime;
            RemainTime = this.GameStatesScript.VarietyShowRemainTime;
        }
        else if(CourseName=='Interview')
        {
            this.InterviewProgressBar.active = true;
            CourseScript = this.InterviewTotalTimeScript;
            TotalTime= this.GameConfigScript.InterviewTotalTime;
            RemainTime = this.GameStatesScript.InterviewRemainTime;
        }
        else if(CourseName=='Movie')
        {
            this.MovieProgressBar.active = true;
            CourseScript = this.MovieTotalTimeScript;
            TotalTime= this.GameConfigScript.MovieTotalTime;
            RemainTime = this.GameStatesScript.MovieRemainTime;
        }
        else if(CourseName=='HangOut')
        {
            this.HangOutProgressBar.active = true;
            CourseScript = this.HangOutTotalTimeScript;
            TotalTime= this.GameConfigScript.HangOutTotalTime;
            RemainTime = this.GameStatesScript.HangOutRemainTime;
        }

        // 重复次数
        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.FinishCallBack(CourseName);
                this.unschedule(StartCourseSchedule);
            }
        }.bind(this);

        this.schedule(StartCourseSchedule, Interval, Repeat-1, Delay);
    },
    FinishCallBack(CourseName)
    {
        // console.log(CourseName)
        if(CourseName=='VarietyShow')
        {
            this.VarietyShowFinishBtn.getComponent(cc.Button).interactable = true;
            this.VarietyShowProgressBar.active = false;
        }
        else  if(CourseName=='Interview')
        {
            this.InterviewFinishBtn.getComponent(cc.Button).interactable = true;
            this.InterviewProgressBar.active = false;
        }
        else  if(CourseName=='Movie')
        {
            this.MovieFinishBtn.getComponent(cc.Button).interactable = true;
            this.MovieProgressBar.active = false;
        }
        else  if(CourseName=='HangOut')
        {
            this.HangOutFinishBtn.getComponent(cc.Button).interactable = true;
            this.HangOutProgressBar.active = false;
        }
    },
    OnFinished(event, customEventData)
    {
        let CourseName = customEventData;
        if(CourseName=='VarietyShow')
        {
            this.VarietyShowFinishBtn.active = false;
            this.VarietyShowStartBtn.active = true;
            this.VarietyShowTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.VarietyShowTotalTime);
        }
        else  if(CourseName=='Interview')
        {
            this.InterviewFinishBtn.active = false;
            this.InterviewStartBtn.active = true;
            this.InterviewTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.InterviewTotalTime);
        }
        else  if(CourseName=='Movie')
        {
            this.MovieFinishBtn.active = false;
            this.MovieStartBtn.active = true;
            this.MovieTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.MovieTotalTime);
        }
        else  if(CourseName=='HangOut')
        {
            this.HangOutFinishBtn.active = false;
            this.HangOutStartBtn.active = true;
            this.HangOutTotalTimeLabel.getComponent(cc.Label).string = this.CastSecondsToHours(this.GameConfigScript.HangOutTotalTime);
        }

        let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid');
        let URL = this.GameConfigScript.Host+'GameEvent/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);
        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);
    }
});
