cc.Class({
    extends: cc.Component,

    properties: {
        BDebug: {
            default: true,
            serializable: true,
        },
        ActionPowerlabel: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        GameConfig: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    onLoad () {
        this.ActionPower = 99;
        this.Coin = 5000;
        this.Diamond = 40;
        this.Fans=0;
        this.FavorableImpression=0;

        this.TalentRemainTime = 0;
        this.BTalentFinished = 0;

        this.MakeUpRemainTime = 0;
        this.BMakeUpFinished = 0;

        this.EloquenceRemainTime = 0;
        this.BEloquenceFinished = 0;

        this.FashionRemainTime = 0;
        this.BFashionFinished = 0;

        this.DanceRemainTime = 0;
        this.BDanceFinished = 0;

        //Event
        this.VarietyShowRemainTime = 0;
        this.BVarietyShowFinished = 0;

        this.InterviewRemainTime = 0;
        this.BInterviewFinished = 0;

        this.MovieRemainTime = 0;
        this.BMovieFinished = 0;

        this.HangOutRemainTime = 0;
        this.BHangOutFinished = 0;

        this.GirlAnimations = ['Idle'];
    },
    start () {
        this.GameConfigScript = this.GameConfig.getComponent('GameConfig');
    },
    //actionPower
    GetActionPower () {
        if(!this.BDebug) this.GetValueFromServer('ActionPower');
        return this.ActionPower;
    },
    SetActionPower (ActionPower) {
        this.SetValueFromServer('ActionPower',ActionPower);
    },
    PlusActionPower (Num,bUpdate) {
        let CurrentNum =  this.ActionPower;
        let NewNum =CurrentNum + Num;
        if(bUpdate)  this.SetValueFromServer('ActionPower',NewNum);
        this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
        this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
    },
    //Coin
    GetCoin () {
        if(!this.BDebug) this.GetValueFromServer('Coin');
        return this.Coin;
    },
    SetCoin (Coin) {
        this.SetValueFromServer('Coin',Coin);
    },
    PlusCoin (Num,bUpdate) {
        let CurrentNum =  this.Coin;
        let NewNum =CurrentNum + Num;
        if(bUpdate)  this.SetValueFromServer('Coin',NewNum);
        // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
        // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
    },
    //diamond
    GetDiamond () {
        if(!this.BDebug) this.GetValueFromServer('Diamond');
        return this.Diamond;
    },
    SetDiamond (Diamond) {
        this.SetValueFromServer('Diamond',Diamond);
    },
    PlusDiamond (Num,bUpdate) {
        let CurrentNum =  this.Diamond;
        let NewNum =CurrentNum + Num;
        if(bUpdate)  this.SetValueFromServer('Diamond',NewNum);
        // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
        // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
    },
    //fans
    GetFans () {
        if(!this.BDebug) this.GetValueFromServer('Fans');
        return this.Fans;
    },
    SetFans (Fans) {
        this.SetValueFromServer('Fans',Fans);
    },
    PlusFans (Num,bUpdate) {
        let CurrentNum =  this.Fans;
        let NewNum =CurrentNum + Num;
        if(bUpdate)  this.SetValueFromServer('Fans',NewNum);
        // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
        // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
    },
    //favorableImpression
    GetFavorableImpression () {
        if(!this.BDebug) this.GetValueFromServer('FavorableImpression');
        return this.FavorableImpression;
    },
    SetFavorableImpression (FavorableImpression) {
        this.SetValueFromServer('FavorableImpression',FavorableImpression);
    },
    PlusFavorableImpression (Num,bUpdate) {
        let CurrentNum =  this.FavorableImpression;
        let NewNum =CurrentNum + Num;
        if(bUpdate)  this.SetValueFromServer('FavorableImpression',NewNum);
        // this.ActionPowerScript = this.ActionPowerlabel.getComponent('ValueChange');
        // this.ActionPowerScript.PlusOrMinusValue(CurrentNum,Num);
    },
    GetValueFromServer(Key)
    {
        let URL = this.GameConfigScript.Host+'GameStates/Get'+Key+'/?Openid='+ cc.sys.localStorage.getItem('Openid');
        let XHR = new XMLHttpRequest();
        let Self = this;
        XHR.onreadystatechange = function () {
            if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400)) {
                let response = XHR.responseText;
                Self.SaveValueFromServer(Key,response);
                // console.log(response);
            }
        };
        XHR.open("GET", URL, false);
        XHR.setRequestHeader("Content-Type" , "application/json");
        XHR.send();
    },
    SetValueFromServer(Key,Value)
    {
        let Data = 'Openid='+ cc.sys.localStorage.getItem('Openid')+'&'+'Value='+Value;
        let URL = this.GameConfigScript.Host+'GameStates/Set'+Key+'/';
        let XHR = new XMLHttpRequest();
        var Self =this;
        XHR.onreadystatechange = function ()
        {
            if (XHR.readyState == 4 && (XHR.status >= 200 && XHR.status < 400))
            {
                let response = XHR.responseText;
                // console.log(response);
                Self.SaveValueFromServer(Key,response);
            }
        };
        XHR.open("POST", URL, false);
        XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        XHR.send(Data);
    },
    SaveValueFromServer(Key,Value)
    {
        if(Key=='ActionPower')
        {
            this.ActionPower = Number(Value);
        }
        else if(Key=='Coin')
        {
            this.Coin = Number(Value);
        }
        else if(Key=='Diamond')
        {
            this.Diamond = Number(Value);
        }
        else if(Key=='Fans')
        {
            this.Fans = Number(Value);
        }
        else if(Key=='FavorableImpression')
        {
            this.FavorableImpression = Number(Value);
        }
    }
});
