import Common from '../common/Common';
import DataModelBase from '../framework/DataModelBase';
import DataModelManager from '../framework/DataModelManager';
import LogicUnitManager from '../framework/LogicUnitManager';
import Event from '../net/Event';

// TODO 查询数据保存到WeekRank中
export default class WeekQuery extends DataModelBase
{
    constructor(modelName)
    {
        super(modelName);
        this.weekRank = DataModelManager.getModel('WeekRank');
        this.configData = DataModelManager.getModel('ConfigData');
        this.lum = LogicUnitManager;
        this.loginLogic = this.lum.getBehavior('LoginBehavior');
    }

    getPullRankInfoUrl(rankType)
    {
       //return requestUrl + areaCode;
    }

    pullData(rankType)
    {
        var requestUrl = this.configData.getApiAddress() == '' ? 'http://192.168.3.178:8080/ranklist/pull_area_ranklist' : this.configData.getApiAddress()+'/ranklist/pull_area_ranklist';
        var areaCode = this.loginLogic.area_code;
        
        switch(rankType)
        {
            case Common.RankType.CityRank:
                 areaCode = areaCode / 100 * 100;
                break;
            case Common.RankType.ProvinceRank:
                 areaCode = areaCode / 10000 * 10000;
                break;
            case Common.RankType.CountryRank:
                 areaCode = 111111;
                break;
        }

        this.weekRank.clearBuff();

        var xhr = cc.loader.getXMLHttpRequest();
        xhr.open("POST", requestUrl, true);
        let self = this;
        xhr.onreadystatechange = function()
        {
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300))
            {
                console.log('http res(' + xhr.responseText.length + '):' + xhr.responseText);
                try
                {
                    var ret = JSON.parse(xhr.responseText);
                    self.onGetDataSuccess(rankType, ret);
                }
                catch(e)
                {
                    console.log('err:' + e);
                }
            }
        };
        var body = {"area_code" : areaCode.toString() };
        xhr.send(JSON.stringify(body));

    }

    // TODO 保存数据
    onGetDataSuccess(rankType, json)
    {
        //this.weekRank.addRank(rankType, rankInfo);

        for (var i = 0; i < json.records.length; ++i)
        {
            var rankInfo = 
            {
                headUrl : 'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJ0J3Obq3uG2q5Pib0S3B8ickiaaLhffLbqLWiaiaiaFW2nHrxDWI4uoGibyKDUHhtNNoYmaYbDwtSFbbrVg/132',
                nickName : 'avatar',
                weekScore : 890,
            };
            if (!json.records[i].userInfo)
            {
                continue;
            }
            var userInfo = JSON.parse(json.records[i].userInfo);
            rankInfo.weekScore = json.records[i].score ? json.records[i].score : 0;
            rankInfo.headUrl = userInfo.avatarUrl;
            rankInfo.nickName = userInfo.nickName;

            this.weekRank.addRank(rankType, rankInfo);
        }
        this.weekRank.swapBuff(rankType);

        this.loginLogic.emit(Event.COM_MSG.REFRESH_RANK_VIEW);
    }
}