/**
 * 生成对应的列表信息
 */

var listInfoType = cc.Enum({
    none: 0,
    cnt: 1,
    snb: 2
})

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个项

        listType: {
            default: listInfoType.none,
            type: cc.Enum(listInfoType),
        },


        snbData: {
            default: null,
            visible: false
        },
        snbPage: 1,
        snbLimit: 8,
        snbIndex: 0,

        cntData: {
            default: null,
            visible: false
        },
        cntPage: 1,
        cntLimit: 8,
        cntIndex: 0,


        TitleSPriteNode: {
            type: cc.Node,
            default: null,
        },

        snbSprite: {
            type: cc.SpriteFrame,
            default: null,
        },

        cntSprite: {
            type: cc.SpriteFrame,
            default: null,
        },

        cntOutContainer: {
            type: cc.Node,
            default: null,
        },

        outPanelContainer: {
            type: cc.Node,
            default: null,
        },

        outCNTValue: {
            type: cc.Label,
            default: null,
        },
        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);
    },
    //更新cntList
    onCntUpdateList(list) {
        this.cntData = list;
    },
    //切换数据
    onShowList(type, list, isActive) {
        // console.log("onShowList:", type, list);
        //获取余额
        GlobalD.GameData.onCntCanWithdrawBalance((data) => {
            // console.log("站内收益余额:", GlobalD.GameData.CNTDrawBalance);
            this.outCNTValue.string = "CNT余额：" + Math.floor(GlobalD.GameData.CNTDrawBalance * 100) / 100;
        });
        this.node.active = isActive;
        if (type == "snbList") {
            this.snbData = list;
            this.cntIndex = this.start_index;
            this.value_set = Object.assign([], this.snbData);
            this.start_index = this.snbIndex;
            this.listType = listInfoType.snb;
            this.TitleSPriteNode.getComponent(cc.Sprite).spriteFrame = this.snbSprite;

            this.cntOutContainer.active = false;
        } else if (type == "cntList") {
            this.cntData = list;
            this.snbIndex = this.start_index;

            this.value_set = Object.assign([], this.cntData);
            this.start_index = this.cntIndex;
            this.listType = listInfoType.cnt;

            this.TitleSPriteNode.getComponent(cc.Sprite).spriteFrame = this.cntSprite;

            this.cntOutContainer.active = true;
        }
        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;
            let _infoScript = this.opt_item_set[i].getComponent("dappListInfoItem");
            let _item = this.value_set[i];

            if (this.listType == listInfoType.snb) {
                let _temp = {
                    listItemName: _item.tranName + _item.tranAmount + "个",
                    listItemDate: _item.createTime,
                    remainder: _item.afterSnb,
                    amount: _item.isAdd == 1 ? '+' + _item.tranSnb : '-' + _item.tranSnb,
                }
                _infoScript.setInfo(_temp);
            } else if (this.listType == listInfoType.cnt) {
                let _temp = {
                    listItemName: "兑换后的cnt数：" + Math.floor(_item.now_amount * 100) / 100,
                    listItemDate: _item.create_time,
                    remainder: Math.floor(_item.amount * 100) / 100,
                    amount: '',
                }
                _infoScript.setCntInfo(_temp);
            }

        }
    },

    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();
    },


    onClose() {
        this.node.active = false;
    },

    openOutPannel() {
        this.outPanelContainer.active = true;
        this.outPanelContainer.getComponent("CntInfo").setInfo();
    }
});
