
const { LevelHomeArtistItemStyle, ArtistOperation } = require('../utils/GameEnum')
const Api = require('../net/Api');
const DWTool = require('../utils/DWTool');
const AlertManager = require('../utils/AlertManager');

cc.Class({
    extends: cc.Component,

    properties: {
       
        // 0是自己, 1是好友
        artistBgSpriteFrames: [cc.SpriteFrame],
        
        bgSprite: cc.Sprite,

        addBtn: cc.Button,

        artistNode: cc.Node,

        headSprite: cc.Sprite,

        tipNode: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:
    initWithBuildingInfo(buildingInfo, uid, isSelf) {
        this.setStyle(LevelHomeArtistItemStyle.Add);
        this.buildingInfo = buildingInfo;
        this.uid = uid;
        this.isSelf = isSelf;
    },

    initWithArtistData(buildingInfo, uid, isSelf, artistData) {
        
        this.setStyle(LevelHomeArtistItemStyle.Artist);
        this.artistData = artistData;
        this.buildingInfo = buildingInfo;
        this.uid = uid;
        this.isSelf = isSelf;
        // 自己艺人
        if (this.artistData.role === 2) {
            this.bgSprite.spriteFrame = this.artistBgSpriteFrames[0];
        } else {
            this.bgSprite.spriteFrame = this.artistBgSpriteFrames[1];
        }
        
        Api.createImageFromUrl(this.artistData.head, (spriteFrame) => {
            this.headSprite.spriteFrame = spriteFrame;
        }, null);
    },

    onLoad () {

        this.artistNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {

            // 去好友家园, 并且这个艺人是该家园主人的艺人, 不能点击
            if (!this.isSelf && this.artistData.role === 2) { return; }

            // 是自己的家园, 并且是自己的艺人, 弹出自己的艺人列表, 可以切换驻场艺人
            if (this.isSelf && this.artistData.role === 2) {
                AlertManager.showArtistResident(this.buildingInfo, this.uid, this.isSelf);
            } else {
                AlertManager.showArtistOperationAlert(this.buildingInfo, this.uid, this.isSelf, this.artistData);
            }
            
        }, 1000, true), this);

        this.handleAddArtist = _.debounce(() => {
            AlertManager.showArtistResident(this.buildingInfo, this.uid, this.isSelf);
        }, 1000, true);
    },

    setStyle(style) {

        if (this.style === style) {
            return;
        }
        switch (style) {
            case LevelHomeArtistItemStyle.Add:
                this.addBtn.node.active = true;
                this.artistNode.active = false;
                break;
            case LevelHomeArtistItemStyle.Artist:
                this.addBtn.node.active = false;
                this.artistNode.active = true;
                break;
            default:
                break;
        }
        this.style = style;
    },

    addArtist() {
        this.handleAddArtist();
    },

    update (dt) {

        if (this.style === LevelHomeArtistItemStyle.Artist 
            && this.artistData
            && this.artistData.role === 4
            && this.isSelf) {
            
            let time = (Date.parse(new Date()) - this.artistData.stationTime) / 1000;
            if (time < 900) { // 入驻15分钟才可以召回
                this.tipNode.active = false;
            } else {
                this.tipNode.active = true;
            }
        }
    },
});
