const Api = require('../net/APi')
cc.Class({
    extends: cc.Component,

    properties: {
        avatarSprite: cc.Sprite,
        rankSprite: cc.Sprite,
        rankText: cc.RichText,
        nameText: cc.RichText,
        genderSprite: cc.Sprite,
        starCountLabel: cc.Label,
        starNode: cc.Node,

        rankFrames: [cc.SpriteFrame],
        genderFrames: [cc.SpriteFrame],
    },

    // onLoad () {},

    setListViewAdapter(adapter) {
        this.listAdapter = adapter;
    },

    updateItem(userInfo, itemId) {
        this._itemId = itemId;
        this.user = userInfo;

        this._bindData(userInfo);
    },

    bindMySelf(userInfo) {
        this._bindData(userInfo);
    },

    _bindData(userInfo) {
        if (userInfo.rank <= 3) {
            switch (userInfo.rank) {
                case 1:
                    this.rankSprite.node.active = true;
                    this.rankText.node.active = false;
                    this.rankSprite.spriteFrame = this.rankFrames[0];
                    break;
                case 2:
                    this.rankSprite.node.active = true;
                    this.rankText.node.active = false;
                    this.rankSprite.spriteFrame = this.rankFrames[1];
                    break;
                case 3:
                    this.rankSprite.node.active = true;
                    this.rankText.node.active = false;
                    this.rankSprite.spriteFrame = this.rankFrames[2];
                    break;
                default:
                    this.rankSprite.node.active = false;
                    this.rankText.node.active = true;
                    let rankString = '未上榜';
                    this.rankText.string = `<outline color=#692e0e width=2><b>${rankString}</b></outline>`;
                    break;
            }
        } else {
            this.rankSprite.node.active = false;
            this.rankText.node.active = true;
            let rankString = userInfo.rank;
            this.rankText.string = `<outline color=#692e0e width=2><b>${rankString}</b></outline>`;
        }

        this.starCountLabel.string = userInfo.stars;

        // Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
        //     this.avatarSprite.spriteFrame = spriteFrame;
        // }, () => {

        // });
        this.genderSprite.spriteFrame = this.genderFrames[userInfo.gender];
        this.nameText.string = `<img src=gender/>` + userInfo.nick;
    },

    start() {

    },

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