// const i18n = require('i18n');

cc.Class({
    extends: cc.Component,

    properties: {
        backSprite: cc.Node,
        rankLabel: cc.Label,
        avatarImgSprite: cc.Sprite,
        nickLabel: cc.Label,
        topScoreLabel: cc.Label,
        outsideSprite:cc.Sprite
    },
    
    onLoad: function () {
    },

    updateItem: function(data) {
        let avatarUrl = data.head;
        let nick = data.nick.length <= 10 ? data.nick : data.nick.substr(0, 10) + "...";
        let grade = data.score;
        let rank  = data.rank;
        this.backSprite.active = true;
        this.rankLabel.string = rank;
        this.createImage(avatarUrl);
        this.nickLabel.string = nick;
        this.topScoreLabel.string = grade;
        this.outsideSprite.active = false;
    },

    outSide(){
        this.outsideSprite.active = true;
    },

    createImage(avatarUrl) {

        var index = avatarUrl.indexOf('https:');
        if(index == 0){
            //以'https:'开头

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