cc.Class({
    extends: cc.Component,

    properties: {
        Item: cc.Prefab,
        Ranking: cc.Node,
        FirstItem: cc.Node,
    },
    start () {
        if(cc.sys.platform === cc.sys.WECHAT_GAME)
        {
            wx.onMessage( data => {
                if (data.type == 'updateMaxScore') {
                    // console.log(data.type);
                    // this.initUserInfo();
                    this.initFriendInfo(data.message);
                }
            });
        } 
    },
    initFriendInfo (nickName) {
        wx.getFriendCloudStorage({
            keyList: ["Riches","LV"],
            success: (res) => {
                cc.log(res.data);
                res.data.sort((a, b) => {
            		if(a.KVDataList.length!=0 && b.KVDataList.length!=0)
            		{
            			 return parseInt(a.KVDataList[0].value) - parseInt(b.KVDataList[0].value);
            		}
                });
                // this.Ranking.removeAllChildren();
                console.log('00000000000=',res.data.length);
                for (let i = 0; i < res.data.length; i++) {

                    let Content = res.data[i];
                    if(Content.nickname==nickName)
                    {
                        this.FirstItem.getChildByName('Ranking').getComponent(cc.Label).string = i+1+'名';
                        var url = Content.avatarUrl;

                        let Self = this;
                        cc.loader.load({url: url, type: 'png'}, function(err,img){
                            var mylogo  = new cc.SpriteFrame(img);
                            Self.FirstItem.getChildByName('AvatarMask').getChildByName('Avatar').getComponent(cc.Sprite).spriteFrame = mylogo;
                        });

                        this.FirstItem.getChildByName('Name').getComponent(cc.Label).string = nickName;
                        this.FirstItem.getChildByName('RankLvBG').getChildByName('label').getComponent(cc.Label).string = 'Lv.'+Content.KVDataList[1].value;
                        this.FirstItem.getChildByName('Riches').getComponent(cc.Label).string = Content.KVDataList[0].value;

                        break;
                    }

                    var ItemPrefab = cc.instantiate(this.Item);
                    ItemPrefab.parent = this.Ranking;
                    // ItemPrefab.setPosition(0,-(ItemPrefab.height*i+ItemPrefab.height/2));
                    // this.Ranking.setContentSize(this.Ranking.width, ItemPrefab.height*i+ItemPrefab.height);
                    // this.Ranking.parent.setContentSize(this.Ranking.width, ItemPrefab.height*i+ItemPrefab.height);

                    ItemPrefab.getChildByName('Ranking').getComponent(cc.Label).string = i+1+'名';
                    var url = Content.avatarUrl;

                    cc.loader.load({url: url, type: 'png'}, function(err,img){
                        var mylogo  = new cc.SpriteFrame(img);
                        ItemPrefab.getChildByName('AvatarMask').getChildByName('Avatar').getComponent(cc.Sprite).spriteFrame = mylogo;
                    });

                    ItemPrefab.getChildByName('Name').getComponent(cc.Label).string = Content.nickname;
                    console.log('111111111=',Content.KVDataList.length)
                    if(Content.KVDataList.length==0)
                    {
                        ItemPrefab.getChildByName('RankLvBG').getChildByName('label').getComponent(cc.Label).string = 'Lv.'+Content.KVDataList[1].value;
                        ItemPrefab.getChildByName('Riches').getComponent(cc.Label).string = Content.KVDataList[0].value;
                    }
                    else
                    {
                        ItemPrefab.getChildByName('RankLvBG').getChildByName('label').getComponent(cc.Label).string = 'Lv.0';
                        ItemPrefab.getChildByName('Riches').getComponent(cc.Label).string = '0';
                    }

                }
            },
            fail: (res) => {
                console.error(res);
            }
        });
    },
});
