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

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,

        topSprite: cc.Sprite,

        bototomSprite: cc.Sprite,
    },

    /**
     * 
     * @param {*} buildingInfo 
     * @param {number} uid 目标的uid
     * @param {boolean} isSelf  判断是不是自己的界面，false是好友界面，true是自己界面
     * @param {*} showTop 
     * @param {*} showBottom 
     */
    initWithBuildingInfo(buildingInfo, uid, isSelf, showTop = false, showBottom = false) {
        this.setStyle(LevelHomeArtistItemStyle.Add);
        this.buildingInfo = buildingInfo;
        this.uid = uid;
        this.isSelf = isSelf;

        this.topSprite.node.active = showTop;
        this.bototomSprite.node.active = showBottom;
    },
    
    /**
     * 
     * @param {*} buildingInfo 
     * @param {number} uid 目标的uid
     * @param {boolean} isSelf  判断是不是自己的界面，false是好友界面，true是自己界面
     * @param {*} artistData 
     * @param {*} showTop 
     * @param {*} showBottom 
     */
    initWithArtistData(buildingInfo, uid, isSelf, artistData, showTop = false, showBottom = false) {
        
        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);

        this.topSprite.node.active = showTop;
        this.bototomSprite.node.active = showBottom;
    },

    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 {
                // 自己家园，驱赶好友的驻场艺人

                // 获取引导对象及引导状态
                let guide = GameModule.homeGuide.getComponent('HomeGuide')
                let guidePass = guide.guideState.state35.pass

                if(!guidePass) {
                    // 如果没有触发过引导事件state35，
                    // 立即触发该引导事件
                    guide.handleState('state35');

                    // 监听state35的点击回调，
                    // 立即显示驱赶驻场艺人弹窗
                    GameModule.homeGuide.on('Fire_state35', () => {
                        AlertManager.showArtistOperationAlert(this.buildingInfo, this.uid, this.isSelf, this.artistData);

                        // 同时显示state36的引导驱赶艺人提示
                        setTimeout(() => {
                            guide.handleState('state36');
                        }, 400);
                    })
                } else {
                    // 如果已经触发过引导事件state35，
                    // 跳过引导系统，直接显示弹窗内容
                    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;
            }
        }
    },
});
