
const Api = require('../net/Api');
const DWTool = require('../utils/DWTool');
const { GameNotificationKey } = require('../utils/GameEnum')
cc.Class({
    extends: cc.Component,

    properties: {

        bgSprite: cc.Sprite,

        headSprite: cc.Sprite,
        nickLabel: cc.Label,
        starCountLabel: cc.Label,
        genderIcon: cc.Sprite,

        roleSprite: cc.Sprite,

        _itemId: 0,

        jobLevelNameLabel: cc.Label,
        jobLevelText: cc.RichText,
    },

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_END, () => {

            if (this.user.isFriend) {
                this.cb();
                GameEvent.fire(GameNotificationKey.VisitFriendHome, this.user.uid);
            }

        }, this);

    },

    /**
     * gender  [int]
     * head    [string]
     * nick    [string]
     * stars   [int]
     * uid     [int]
     * */
    updateItem(userInfo, itemId, cb) {

        this._itemId = itemId;
        this.user = userInfo;
        this.cb = cb || function () { };

        if (userInfo.isFriend) {
            this.roleSprite.node.active = true;
        } else {
            this.roleSprite.node.active = false;
        }

        DWTool.setGenderIcon(userInfo.gender).then((spriteFrame) => {
            this.genderIcon.node.active = true;
            this.genderIcon.spriteFrame = spriteFrame;
        }, (error) => {
            this.genderIcon.node.active = false;
        });
        this.nickLabel.string = userInfo.nick;
        this.starCountLabel.string = userInfo.stars;

        Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
            this.headSprite.spriteFrame = spriteFrame;
        }, null);

        if (userInfo.jobLevelName) {
            this.jobLevelNameLabel.string = userInfo.jobLevelName;
        } else {
            this.jobLevelNameLabel.string = "";
        }

        if (userInfo.jobLevel) {
            this.jobLevelText.string = `<outline color=#524340 width=2>${userInfo.jobLevel}</outline >`;
        } else {
            this.jobLevelText.string = '0';
        }
    },
});
