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

cc.Class({

    extends: cc.Component,

    properties: {

        content: cc.Node,

        noActorTipLabel: cc.Label,

        confirmButton: cc.Button,

        scrollView: cc.ScrollView,

        _otherActorItem: cc.Prefab,

    },

    onLoad() {
        this.node.height = cc.view.getVisibleSize().height;
        this.contentY = (this.content.height - this.node.height) / 2;

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

        this.content.y = -cc.view.getVisibleSize().height;
        let s = cc.sequence(cc.moveTo(0.2, 0, this.contentY + 20).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20));
        this.content.runAction(s);
    },

    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_artist_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('OtherArtistItem').updateItem(this.artists[i], i, () => {
                this.node.destroy();
            });
        }
    },


    close() {
        if (this.node && this.node.parent) {
            let finish = cc.callFunc(() => {
                this.node.destroy();
            }, this)
            this.content.runAction(cc.sequence(cc.moveTo(0.2, 0, -cc.view.getVisibleSize().height).easing(cc.easeCubicActionIn()), finish));
        }
    }

});