
const DWTool = require("../utils/DWTool");
const HomeApi = require("../net/HomeApi");
const { GameNotificationKey } = require("../utils/GameEnum");
const Base64 = require("../lib/Base64").Base64;
const AlertManager = require("../utils/AlertManager");

cc.Class({
    extends: cc.Component,

    properties: {

        content: cc.Node,

        scrollView: cc.ScrollView,

        noArtistTip: cc.Node,

        talentBtn: cc.Button,

        confirmBtn: cc.Button,
    },

    init(buildingInfo, uid, isSelf) {
        this.buildingInfo = buildingInfo;
        this.uid = uid;
        this.isSelf = isSelf;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this._residentList = [];

        this._currentResidentArtist = null;

        this.getNetworkData();

        GameEvent.on(GameNotificationKey.RefreshResidentArtistList, this, () => {
            for (let child of this.scrollView.content.children) {
                child.destroy();
            }
            this.getNetworkData();
        });

        this.confirmEvent = _.debounce(() => {
            if (this._currentResidentArtist && !this._currentResidentArtist.isStationed) {
    
                let reportFormInfo = {};
                reportFormInfo["artistUid"] = this._currentResidentArtist.uid;
                reportFormInfo["buildingId"] = this.buildingInfo.buildingId;
                reportFormInfo["buildingLevel"] = this.buildingInfo.level;
                reportFormInfo["targetUid"] = this.uid;
            
                HomeApi.friendStation(JSON.stringify(reportFormInfo), (responseData) => {
                    this.node.destroy(); 
                    // 入驻成功, 要将现有的金币收取, 并且刷新入驻艺人列表
                    GameEvent.fire(GameNotificationKey.ResidentArtist, this.uid, this.buildingInfo.buildingId);
                    // 触发入驻通知，opt = 5
                    GameEvent.fire(GameNotificationKey.NoticeRoleOpt, this.uid, 5)
                }, (code, msg) => {
                    
                    // 亲密度不够
                    if (code === 814) {
                        AlertManager.showIntimacyBotEnough();
                    } 
                    console.log(msg);
                }); 
         
            } else {
                this.node.destroy();    
            }
        }, 1000, true);

        this.showTalentEvent = _.debounce(() => {
            this.node.destroy(); 
            AlertManager.showTalentAlert();
        }, 1000, true);
    },

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

    start() {

        this.content.y = -cc.view.getVisibleSize().height;

        let bouncesActionArray = [];

        let space = 50;
        let upAction = cc.moveTo(0.25, cc.v2(0, space));
        let downAction = cc.moveBy(0.1, cc.v2(0, -space));

        bouncesActionArray.push(upAction);
        bouncesActionArray.push(downAction);

        this.content.runAction(cc.sequence(bouncesActionArray));
    },

    getNetworkData() {
        
        HomeApi.friendGetArtistsByBuildingId(this.uid, this.buildingInfo.buildingId,  (responseData) => {

            if (responseData.list === undefined || responseData.list.length === 0) {
                this.noArtistTip.active = true;
                this.talentBtn.node.active = true;
                this.confirmBtn.node.active = false;
            } else {
                this.talentBtn.node.active = false;
                this.confirmBtn.node.active = true;
                this._residentList = responseData.list;
                this.layout();
            }

        }, (err) => {
            console.log(err);
        });
    },

    layout() {

        DWTool.loadResPrefab('./prefabs/artist_resident_item')
        .then((result) => {
            
            for (let i = 0; i < this._residentList.length; i++) {
                let item = cc.instantiate(result);
                item.getComponent(`ArtistResidentItem`).init(this.uid, this.buildingInfo, this.isSelf, this._residentList[i], (artistData) => {
                    this._currentResidentArtist = artistData;
                    console.log(this._currentResidentArtist);
                });
                this.scrollView.content.addChild(item);
            }
        
        }).catch((err) => {
            console.log(err);
        });
    },

    confirm() {
        this.confirmEvent();
    },

    showTalent() {
        this.showTalentEvent();
    },

    close() {
        this.node.destroy();
    },

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