var UserInformationApi = require('../net/UserInformationApi');
var {UserInformationType} = require('../utils/GameEnum');
var TimelineItem = require("UserInformationTimelineItem");

cc.Class({
    extends: cc.Component,

    properties: {
        emptyNode: cc.Node,

        scrollViewNode: cc.ScrollView,

        timelinePrefab: cc.Prefab,

        itemArray: [TimelineItem],
    },

    // LIFE-CYCLE CALLBACKS:

    init() {

    },

    onLoad () {

    },

    onDisable() {
        for (let item of this.itemArray) {
            item.node.active = false;
        }

        this.scrollViewNode.scrollToTop();
        this.emptyNode.active = false;
    },

    start () {

    },

    loadUserTimeline(uid) {
        this.uid = uid;
        this.getNetworkData();
    },

    //获取个人信息
    getNetworkData() {
        UserInformationApi.getTimelineList(this.uid, (responseData)=> {
            // console.log("timeline responseData: " + JSON.stringify(responseData));
            this.configData(responseData);
        }, (error) => {
            console.log('error' + error);
            this.loadDataError();
        });
    },

    configData(responseData) {
        for (let item of this.itemArray) {
            item.node.active = false;
        }

        this.dataList = responseData.list;
        if (this.dataList == undefined || this.dataList.length == 0) {
            this.emptyNode.active = true;
            this.scrollViewNode.node.active = false;
        } else {
            this.emptyNode.active = false;
            this.scrollViewNode.node.active = true;
            this.loadItemData()
        }
    },

    loadDataError() {
        for (let item of this.itemArray) {
            item.node.active = false;
        }

        this.emptyNode.active = true;
        this.scrollViewNode.node.active = false;
    },

    loadItemData() {
        for (var i = 0; i < this.dataList.length; i++) {
            let item = null;
            if (this.itemArray[i]) {
                item = this.itemArray[i];
                item.node.active = true;
            } else {
                item = cc.instantiate(this.timelinePrefab);
                item = item.getComponent('UserInformationTimelineItem');
                item.node.parent = this.scrollViewNode.content;
                this.itemArray.push(item);
            }
            let itemModel = this.dataList[i];
            item.configData(itemModel);
        }
    }

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