const ListViewAdapter = require('./ListViewAdapter');
cc.Class({
    extends: cc.Component,

    properties: {
        scrollView: cc.ScrollView,
        content: cc.Node,
        prefabRankItem: cc.Node,
        itemScriptName: '',
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.listAdapter = new ListViewAdapter(this.scrollView);

        //-------------------------------------test--------------------------------------------------------------------------//
        // this.mineScore = {
        //     nick: "user" + i,
        //     url: "http://www.psahz.com/uploads/allimg/181006/094Q62X3-2.jpg",
        //     score: (100 - i * 2)
        // };

        // this.prefabRankItem.getComponent('RankItem').updateItem(this.mineScore, 0);

        // for (i = 0; i < 30; i++) {
        //     this.user = {
        //         nick: "user" + i,
        //         url: "http://www.psahz.com/uploads/allimg/181006/094Q62X3-2.jpg",
        //         score: (100 - i * 2)
        //     };

        //     this.users.push(this.user);
        // }


        //-------------------------------------test--------------------------------------------------------------------------//

    },

    onEnable() {
        if (CC_QQPLAY) {
            var attr = "score";//使用哪一种上报数据做排行，可传入score，a1，a2等
            var order = 1;     //排序的方法：[ 1: 从大到小(单局)，2: 从小到大(单局)，3: 由大到小(累积)]
            var rankType = 0; //要查询的排行榜类型，0: 好友排行榜
            let self = this;
            BK.QQ.getRankListWithoutRoom(attr, order, rankType, function (errCode, cmd, data) {
                BK.Script.log(1, 1, "getRankListWithoutRoom callback  cmd" + cmd + " errCode:" + errCode + "  data:" + JSON.stringify(data));
                // 返回错误码信息
                if (errCode !== 0) {
                    BK.Script.log(1, 1, '获取排行榜数据失败!错误码：' + errCode);
                    return;
                }
                // 解析数据
                if (data) {
                    for (var i = 0; i < data.data.ranking_list.length; ++i) {
                        var rd = data.data.ranking_list[i];
                        // rd 的字段如下:
                        //var rd = {
                        //    url: '',            // 头像的 url
                        //    nick: '',           // 昵称
                        //    score: 1,           // 分数
                        //    selfFlag: false,    // 是否是自己
                        //};
                        if (rd.selfFlag) {
                            self.prefabRankItem.getComponent('RankItem').updateItem(rd, i);
                        }
                        self.listAdapter.updateItems(data.data.ranking_list, self.prefabRankItem, self.itemScriptName);
                    }
                }
            });
        } else {
            let reponse = { "errCode": 0, "cmd": "apollo_router_game.apollo_user_rankinglist_linkcmd_custom_ranking", "data": { "data": { "ranking_list": [{ "nick": "石达开", "url": "http://img0.imgtn.bdimg.com/it/u=789186820,4034808660&fm=27&gp=0.jpg", "score": 1234, "a1": 121, "a2": 22, "selfFlag": true }, { "nick": "绝世佳人", "url": "http://a.hiphotos.baidu.com/zhidao/pic/item/21a4462309f79052782f28490ff3d7ca7bcbd591.jpg", "score": 924, "a1": 101, "a2": 20, "selfFlag": false }, { "nick": "千山万水", "url": "http://img5.imgtn.bdimg.com/it/u=1443063381,2025721196&fm=27&gp=0.jpg", "score": 712, "a1": 134, "a2": 24, "selfFlag": false }, { "nick": "三年二班", "url": "http://img1.imgtn.bdimg.com/it/u=3463612537,3165214774&fm=27&gp=0.jpg", "score": 527, "a1": 167, "a2": 23, "selfFlag": false }, { "nick": "henry", "url": "http://img5.imgtn.bdimg.com/it/u=377014827,3751078472&fm=27&gp=0.jpg", "score": 327, "a1": 154, "a2": 32, "selfFlag": false }, { "nick": "图样图森破", "url": "http://img2.imgtn.bdimg.com/it/u=1269743249,2763282807&fm=27&gp=0.jpg", "score": 227, "a1": 125, "a2": 86, "selfFlag": false }, { "nick": "Mo_Bai", "url": "http://img3.imgtn.bdimg.com/it/u=3166078695,1890527236&fm=27&gp=0.jpg", "score": 167, "a1": 138, "a2": 39, "selfFlag": false }] } } }
            this.listAdapter.updateItems(reponse.data.data.ranking_list, this.prefabRankItem, this.itemScriptName);
        }
    },

    start() {

    },

    update(dt) {
        if (this.listAdapter) {
            this.listAdapter.update(dt);
        }
    },
});
