const Api = require('../net/Api');
const { GameNotificationKey } = require("../utils/GameEnum");
const HomeApi = require("../net/HomeApi");
const AlertManager = require("../utils/AlertManager");

cc.Class({
    extends: cc.Component,

    properties: {

        bgSpriteFrames: [cc.SpriteFrame],
     
        headSprite: cc.Sprite,

        nickRichText: cc.RichText,

        jobLevelLabel: cc.Label,
        
        jobTitleLabel: cc.Label,

        effectLabel: cc.Label,

        cancelResidentBtn: cc.Button,

        recallArtistBtn: cc.Button,

        bgSprite: {
            get: function() {
                if (!this._bgSprite) {
                    this._bgSprite = this.getComponent(cc.Sprite);
                }
                return this._bgSprite;
            },
            set: function(value) {
                this._bgSprite = value;
            }
        },

        flag: {
            get: function () {
                if (!this._flag) {
                    this._flag = 0;
                }
                return this._flag;
            },
            set: function (value) {
                this._flag = value;
                this._bgSprite.spriteFrame = this.bgSpriteFrames[value];
            }

        }


    },

    init(uid, buildingInfo, isSelf, artistData, selectedCallback) {

        this.uid = uid;
        this.buildingInfo = buildingInfo;
        this.artistData = artistData;
        this.isSelf = isSelf;
        this.selectedCallback = selectedCallback;
        this.jobLevelLabel.string = this.artistData.jobLevel;
        this.jobTitleLabel.string = this.artistData.jobName;

        if (this.artistData.isStationed === 0) { // 没有任何驻场艺人

            this.cancelResidentBtn.node.active = false;
            this.recallArtistBtn.node.active = false;
            this.bgSprite.spriteFrame = this.bgSpriteFrames[0];

        } else if (this.artistData.isStationed === 1) { // 入驻了自己家园

            this.cancelResidentBtn.node.active = true;
            this.recallArtistBtn.node.active = false;
            this.bgSprite.spriteFrame = this.bgSpriteFrames[1];

        } else { // 入驻他人家园
            this.cancelResidentBtn.node.active = false;
            this.recallArtistBtn.node.active = true;
        }

        this.nickRichText.string = `<color=#584a47>${this.artistData.nick}</c> <img src='50002'/>`;
        this.effectLabel.string = `加成效果: 收益增加${this.artistData.addition}%`;
        Api.createImageFromUrl(this.artistData.head, (spriteFrame) => {
            this.headSprite.spriteFrame = spriteFrame;
        }, null);
    },

    cancelResident() {
        this.cancelResidentEvent();
    } ,

    // 召回自己艺人
    recallArtist() {
        this.recallArtistEvent();
    },

    onLoad () {

        this.bgSprite = this.getComponent(cc.Sprite);

        this.node.on(cc.Node.EventType.TOUCH_END, () => {
            this.flag = (this.flag === 1) ? 0 : 1;
            if (this.flag) {
                this.selectedCallback(this.artistData);    
                GameEvent.fire(GameNotificationKey.ChangeArtistResidentState, this.artistData.jobId)
            } else {
                this.selectedCallback(null);
            }
        }, this);

        GameEvent.on(GameNotificationKey.ChangeArtistResidentState, this, (jobId) => {
            if (this.artistData.jobId != jobId) {
                this.flag = 0;
            }
        });

        this.recallArtistEvent = _.debounce(() => {
            AlertManager.showArtistOperationAlert(this.buildingInfo, this.uid, this.isSelf, this.artistData);
        }, 1000, true);

        this.cancelResidentEvent = _.debounce(() => {
            HomeApi.friendExpelRecall(this.artistData.id || this.artistData.uid, (responseData) => {
                this.cancelResidentBtn.node.active = false;
                // 取消入驻要收取所有金币
                GameEvent.fire(GameNotificationKey.RefreshResidentArtistList);
                GameEvent.fire(GameNotificationKey.ResidentArtist, this.uid, this.buildingInfo.buildingId);
                GameEvent.fire(GameNotificationKey.NoticeRoleOpt, this.uid, 6);
            }, (code, msg) => {
                console.log(msg);
            });
        }, 1000, true);
    },

    selectItem() {
        this.flag = 1;
        this.selectedCallback(this.artistData);    
    },

    onDestroy() {
        GameEvent.off(GameNotificationKey.ChangeArtistResidentState, this);
    },

    start () {

    },


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