const DWTool = require('../utils/DWTool');
const Notikey = require('../utils/GameEnum').GameNotificationKey;
const HomeApi = require("../net/HomeApi");

cc.Class({

    extends: cc.Component,

    properties: {

        noActorTipLabel: cc.Label,

        confirmButton: cc.Button,

        scrollView: cc.ScrollView,

        _otherActorItem: cc.Prefab,

    },

    onLoad() {

        this.artists = [];
        this.itemPool = new cc.NodePool();
        this.getNetwoekData();
    },

    init(artists) {

        this.artists = artists;

        if (this.artists === undefined || this.artists.length === 0) {
            this.noActorTipLabel.node.active = true;
            this.scrollView.node.active = false;
        } else {
            this.noActorTipLabel.node.active = false;
            this.scrollView.node.active = true;

            this.setNodePool().then(this.layout.bind(this));
        }
    }, 

    setNodePool() {
        return new Promise((resolve, reject) => {
        
            DWTool.loadResPrefab("./prefabs/other_actor_item")
                .then((prefab) => {
                    this._otherActorItem = prefab;
                    for (let index = 0; index < 21; index++) {
                        let item = cc.instantiate(this._otherActorItem);
                        this.itemPool.put(item);
                    }
                    resolve();
                });
        });
    },

    getNetwoekData() {

        HomeApi.friendGetArtists(this.uid, (responseData) => {
            console.log(responseData);
            this.init(responseData.list);
        }, (err) => {
            console.log(err);
        })
    },

    layout() {

        for (let i = 0; i < this.artists.length; i++) {
            let item = null;
            if (this.itemPool.size() > 0) {
                item = this.itemPool.get();
            } else {
                item = cc.instantiate(this._otherActorItem);
            }

            this.scrollView.content.addChild(item);
            item.getComponent('OtherActorItem').updateItem(this.artists[i], i);
        }
    },


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

});