
const Api = require('../net/Api');
var FriendApi = require('../net/FriendSystemApi');
var DWTool = require('../utils/DWTool');

cc.Class({
    extends: cc.Component,

    properties: {

        bgSprite: cc.Sprite,

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

        commonFriend1: cc.Node,
        commonFriend2: cc.Node,
        commonFriend3: cc.Node,

        commonFriendLabel: cc.Label,

        addFriendButton: cc.Button,
        addFriendLabel: cc.Label,

        _friendList: null,

        _itemId: 0,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
    },

    setFriendList(friendList) {
        this._friendList = friendList;
    },

    setListViewAdapter(listViewAdapter) {
        this._listViewAdapter = listViewAdapter;
    },

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

        this._itemId = itemId;

        this.user = userInfo;

        DWTool.setGenderIcon(userInfo.gender).then((spriteFrame) => {
            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;
        }, (err) => {
            this.headSprite.spriteFrame = null;
        });

        // let addBg = this.user.sendedApply === undefined ? this._addFriendBg : this._sendedBg;

        // cc.loader.loadRes(addBg, cc.SpriteFrame, (err, spriteFrame) => {
        //     this.addFriendButton.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        // });

        if (userInfo.commons != undefined && userInfo.commons != null && userInfo.commons.length > 0) {

            this.commonFriendLabel.string = '...' + userInfo.commons.length + '个共同好友';
            //     this.commonFriend1.active = true;
            //     this.commonFriend1.getComponent('CommonFriend').setAvatar(userInfo.commons[0].head);

            //     if (userInfo.commons.length > 1) {
            //         this.commonFriend2.active = true;
            //         this.commonFriend2.getComponent('CommonFriend').setAvatar(userInfo.commons[1].head);
            //     } else {
            //         this.commonFriend2.active = false;
            //         this.commonFriend3.active = false;
            //     }

            //     if (userInfo.commons.length > 2) {
            //         this.commonFriend3.active = true;
            //         this.commonFriend3.getComponent('CommonFriend').setAvatar(userInfo.commons[1].head);
            //     } else {
            //         this.commonFriend3.active = false;
            //     }

            // } else {
            //     this.commonFriend1.active = false;
            //     this.commonFriend2.active = false;
            //     this.commonFriend3.active = false;
            //     this.commonFriendLabel.string = '...0个共同好友';
        }
    },

    start() {

    },

    deleteItem() {
        FriendApi.processApply(this.user.uid, -1);
        this._listViewAdapter.removeItem(this);
    },

    friendApply() {
        if (this.user.sendedApply === undefined) {
            this.user.sendedApply = true;
            FriendApi.friendApply(this.user.uid, () => {
                this.addFriendButton.interactable = false;
                this.addFriendLabel.string = '已发送';
            });
        }
    },

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