/**
 * 可偷菜的用户信息
 */

cc.Class({
    extends: cc.Component,

    properties: {
        listInfoPrefab: {
            default: null,
            type: cc.Prefab,
            serializable: true,
        },
        listContainer: {
            default: null,
            type: cc.Node
        },
        value_set: {
            default: null,
            visible: false
        },
        scroll_view: { //获取scrollview组件
            type: cc.ScrollView,
            default: null,
        },

        HIGH: 80,   //每一项的高度
        PAGE_NUM: 4,  //每一页4个项

        opt_item_set: []

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

    },
    start() {
        // this.onOpentList();
        this.scroll_view.node.on("scroll-ended", this.on_scroll_ended.bind(this), this);//监听scrollview事件

        this.opt_item_set = [];
        //每次加载3页
        for (var i = 0; i < this.PAGE_NUM * 3; i++) {
            let _info = cc.instantiate(this.listInfoPrefab);
            _info.active = false;
            this.listContainer.addChild(_info);
            this.opt_item_set.push(_info);
        }
        this.start_y = this.listContainer.y;//初始化起始y坐标
        this.start_index = 0; //100项数据里面的起始数据记录索引
        // this.load_recode(this.start_index);

        GlobalD.GameData.onGetCanStealUserList(0, 10, (list) => {
            console.log("==========", list);
            this.onShowUserList(list);
        });
    },

    //显示列表
    onShowUserList(list) {
        this.value_set = Object.assign([], list);
        this.load_recode(this.start_index);
    },

    load_recode: function (start_index) {
        this.start_index = start_index;
        if (this.opt_item_set.length <= 0) return;
        for (var i = 0; i < this.PAGE_NUM * 3; i++) {
            if (this.value_set.length <= i) {
                this.opt_item_set[i].active = false;
                continue;
            }
            this.opt_item_set[i].active = true;
            //处理item
            let _infoScript = this.opt_item_set[i].getComponent("userListInfoItem");
            let _item = this.value_set[i];
            _infoScript.setInfo({item:_item});
        }
    },

    load_scroll_recode: function () {
        //向下加载数据
        //当开始位置比value_set的长度小则代表没加载完
        if (this.start_index + this.PAGE_NUM * 3 < this.value_set.length &&
            this.listContainer.y >= this.start_y + this.PAGE_NUM * 2 * this.HIGH)//content超过2个PAGE的高度
        {
            //_autoScrolling在引擎源码中负责处理scrollview的滚动动作
            if (this.scroll_view._autoScrolling) { //等自动滚动结束后再加载防止滚动过快，直接跳到非常后的位置
                this.scroll_view.elastic = false; //关闭回弹效果 美观
                return;
            }
            var down_loaded = this.PAGE_NUM;
            this.start_index += down_loaded;

            if (this.start_index + this.PAGE_NUM * 3 > this.value_set.length) {
                //超过数据范围的长度
                var out_len = this.start_index + this.PAGE_NUM * 3 - this.value_set.length;
                down_loaded -= out_len;
                this.start_index -= out_len;
            }
            this.load_recode(this.start_index);
            this.listContainer.y -= down_loaded * this.HIGH;
            return;
        }
        //向上加载
        if (this.start_index > 0 && this.listContainer.y <= this.start_y) {
            if (this.scroll_view._autoScrolling) {
                this.scroll_view.elastic = false;
                return;
            }
            var up_loaded = this.PAGE_NUM;
            this.start_index -= up_loaded;
            if (this.start_index < 0) {
                up_loaded += this.start_index;
                this.start_index = 0;
            }
            this.load_recode(this.start_index);
            this.listContainer.y += up_loaded * this.HIGH;
        }
    },
    on_scroll_ended: function () {
        this.load_scroll_recode();
        this.scroll_view.elastic = true; //加载结束后自动滚动回弹开启
    },
    update(dt) {
        // this.load_scroll_recode();
    },

    /**
      * 显示偷菜可偷列表
      */
    onOpen() {
        this.node.active = true;
    },
    onClose() {
        this.node.active = false;
    },

});
