
cc.Class({
    extends: cc.Component,

    properties: {
        rankSprite:[cc.SpriteFrame],  //名次图片(第1，第2，第3名)精灵帧
        spMingci: cc.Sprite,  //名次图片精灵
        labmingci: cc.Label,  //名次文字
        labName: cc.Label,  //玩家名字
        labScore: cc.Label,  //玩家分数
        spdoufuHead: cc.Sprite,  //玩家头像
        labChenghao: cc.Label,  //称号
        bg: cc.SpriteFrame,  //背景图 (群排行榜中用到)
        _itemID: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        
    },

    init: function(rank,data,type){
        // console.log("排行榜数据",rank,data);
        var nick = data.nickname;
        var grade = data.KVDataList.length != 0 ? data.KVDataList[0].value : 0;
        var avatarUrl = data.avatarUrl;
        this.labName.string = nick;  //显示玩家名字
        this.labScore.string = "第"+grade+"关";  //显示玩家分数

        if(rank ==0){
            this.spMingci.spriteFrame =this.rankSprite[0];
        }else if(rank ==1){
            this.spMingci.spriteFrame =this.rankSprite[1];
        }else if(rank ==2){
            this.spMingci.spriteFrame =this.rankSprite[2];    
        }else{
            this.spMingci.node.active =false;  //名次图片隐藏
            this.labmingci.node.active =true;  //名次数字显示
            this.labmingci.string =rank+1;
        }

        //在群排行榜中，列表中是自己的数据，此列表数据的背景颜色要改变
        if(type){
            if(data.openid ==cc.HQ.WechatApi.openId){
                this.node.getComponent(cc.Sprite).spriteFrame =this.bg;
            }
        }

        //显示用户头像的图片
        this.createImage(avatarUrl);
        //根据当前关卡数，显示对应玩家称号
        this.onGetChenghao(grade);
    },

    createImage: function(avatarUrl){
        if (CC_WECHATGAME) {
            try {
                let image = wx.createImage();
                image.onload = () => {
                    try {
                        let texture = new cc.Texture2D();
                        texture.initWithElement(image);
                        texture.handleLoadedTexture();
                        this.spdoufuHead.spriteFrame = new cc.SpriteFrame(texture);
                    } catch (e) {
                        cc.log(e);
                        this.spdoufuHead.node.active = false;
                    }
                };
                image.src = avatarUrl;
            }catch (e) {
                cc.log(e);
                this.spdoufuHead.node.active = false;
            }
        } else {
            cc.loader.load({
                url: avatarUrl, type: 'jpg'
            }, (err, texture) => {
                this.spdoufuHead.spriteFrame = new cc.SpriteFrame(texture);
            });
        }
    },

    //根据当前关卡数，显示对应玩家称号
    onGetChenghao(num){
        if(num>=1 && num<=10){
            this.labChenghao.string ="水果小白";
        }else if(num>=11 && num<=30){
            this.labChenghao.string ="水果新秀";   
        }else if(num>=31 && num<=60){
            this.labChenghao.string ="水果学徒"; 
        }else if(num>=61 && num<=100){
            this.labChenghao.string ="水果精英";
        }else if(num>=101 && num<=200){
            this.labChenghao.string ="水果达人";
        }else if(num>=201 && num<=400){
            this.labChenghao.string ="水果大师"; 
        }else if(num>=401){
            this.labChenghao.string ="水果王者";
        }   

    },

    
    // update (dt) {},
});
