
const Api = require('../net/Api');
const FriendApi = require('../net/FriendSystemApi');
const UserInformationType = require('../utils/GameEnum').UserInformationType;
var ShaderUtil = require('../utils/ShaderUtil');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
var DWTool = require('../utils/DWTool');
cc.Class({
    extends: cc.Component,

    properties: {

        bgSprite: cc.Sprite,

        content: cc.Node,

        headSprite: cc.Sprite,
        nickLabel: cc.Label,
        starCountLabel: cc.Label,
        jobLevelNode: cc.Node,
        jobLevelText: cc.RichText,
        genderIcon: cc.Sprite,

        homeNode: cc.Node,

        acceptButton: cc.Node,
        rejectButton: cc.Node,

        apply: cc.Node,

        topBarSprite: cc.Sprite,
        roleText: cc.RichText,
        stateSprite: cc.Sprite,

        _friendList: null,

        _itemId: 0,

        _listViewAdapter: null,

        clickable: true,

        titleLabel: cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.bgSprite.node.on(cc.Node.EventType.TOUCH_END, () => {
            if (this.user.isApplied) {
                return;
            }
            GameEvent.fire(GameNotificationKey.ShowUserInfomation, this.user.uid);

        }, this)

        this.homeNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
            GameEvent.fire(GameNotificationKey.VisitFriendHome, this.user.uid);
        }, 1000, true), this);

        // this.headSprite.node.width = this.headSprite.node.width / 4;
        // this.headSprite.node.height = this.headSprite.node.height / 4;
    },

    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;

        if (userInfo.isApplied) {
            this.apply.active = true;
            this.homeNode.active = false;
            this.stateSprite.node.active = false;
            this.topBarSprite.node.active = false;
            this.roleText.node.active = false;
            this.titleLabel.node.active = false;
            this.content.y = 20;
        } else {
            this.apply.active = false;
            this.homeNode.active = true;
            this.topBarSprite.node.active = true;
            this.roleText.node.active = true;
            this.titleLabel.node.active = true;
            this.content.y = 0;

            let typeImage = '';
            let typeText = '';
            switch (userInfo.role) {
                case UserInformationType.Boss:
                    this.stateSprite.node.active = false;
                    typeImage = './friendList/my_boss_bar'
                    typeText = '我的老板';
                    break;
                case UserInformationType.MyArtist:
                    this.stateSprite.node.active = false;
                    typeImage = './friendList/my_actor_bar'
                    typeText = '我的艺人';
                    break;
                case UserInformationType.ArtistFree:
                    this.stateSprite.node.active = true;
                    DWTool.loadResSpriteFrame('./friendList/state_free').then(spriteFrame => {
                        this.stateSprite.spriteFrame = spriteFrame;
                    });
                    typeImage = './friendList/free_bar'
                    typeText = '自由身';
                    break;
                case UserInformationType.OtherArtist:
                    this.stateSprite.node.active = true;
                    DWTool.loadResSpriteFrame('./friendList/state_other_actor').then(spriteFrame => {
                        this.stateSprite.spriteFrame = spriteFrame;
                    });
                    typeImage = './friendList/other_actor_bar'
                    typeText = '他人艺人';
                    break;
                default:
                    this.stateSprite.node.active = true;
                    DWTool.loadResSpriteFrame('./friendList/state_free').then(spriteFrame => {
                        this.stateSprite.spriteFrame = spriteFrame;
                    });
                    typeImage = './friendList/free_bar'
                    typeText = '自由身';
                    break;
            }


            DWTool.loadResSpriteFrame(typeImage).then(spriteFrame => {
                this.topBarSprite.spriteFrame = spriteFrame;
            });

            this.roleText.string = '<b>' + typeText + '</b>'

            if (userInfo.jobName) {
                this.jobLevelNode.active = true;
                this.titleLabel.string = userInfo.jobName;
            } else {
                this.jobLevelNode.active = false;
            }

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

        }

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


        Api.createImageFromUrl(userInfo.head, (spriteFrame) => {
            this.headSprite.spriteFrame = spriteFrame;
            // this.headSprite.node.scaleX = 4;
            // this.headSprite.node.scaleY = 4;
        }, () => {

        });

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

        //     this.commonFriendLabel.string = userInfo.commons.length;
        //     this.sameFriend1.node.active = true;
        //     Api.createImageFromUrl(userInfo.commons[0].head, (spriteFrame) => {
        //         this.sameFriend1.spriteFrame = spriteFrame;
        //     });

        //     if (userInfo.commons.length > 1) {
        //         this.sameFriend2.node.active = true;
        //         Api.createImageFromUrl(userInfo.commons[1].head, (spriteFrame) => {
        //             this.sameFriend2.spriteFrame = spriteFrame;
        //         })
        //     } else {
        //         this.sameFriend2.node.active = false;
        //         this.sameFriend3.node.active = false;
        //     }

        //     if (userInfo.commons.length > 2) {
        //         this.sameFriend3.node.active = true;
        //         Api.createImageFromUrl(userInfo.commons[2].head, (spriteFrame) => {
        //             this.sameFriend3.spriteFrame = spriteFrame;
        //         })
        //     } else {
        //         this.sameFriend3.node.active = false;
        //     }
        // } else {
        //     this.commonFriendTip.active = false;
        //     this.commonFriendLabel.node.active = false;
        // }

    },

    start() {

    },


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

    acceptApply() {
        FriendApi.processApply(this.user.uid, 1);
        this.user.isApplied = false;
        this.updateItem(this.user, this._itemId);

        // 通过好友申请，操作类型：9
        let opt = 9;
        GameEvent.fire(GameNotificationKey.NoticeRoleOpt, this.user.uid, opt)
    },

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