const ListAdapter = require('../utils/ListViewAdapter');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
const GameModule = require("../utils/GameModule");

cc.Class({
    extends: cc.Component,

    properties: {
        scrollView: cc.ScrollView,
        content: cc.Node,
        item: cc.Prefab,
        itemScriptName: 'LevelHomeItem',

        //
        topItem: cc.Prefab,

        bottomItem: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.isMoving = false;

        this.listAdapter = new ListAdapter(this.scrollView);
        this.listAdapter.shouldRemove = false;

        // this.scrollView.content.removeAllChildren();

        // let top = cc.instantiate(this.topItem);
        // this.topItemHeight = top.height; // get total content height
        // this.content.addChild(top);
        // top.setPosition(0, -(this.topItemHeight * 0.5));
        // let topItemScript = top.getComponent('LevelHomeTop');
        // topItemScript.updateItem();

        // this.dataList = [1,2,3,4,5,6,7,8];
        // this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName, this.topItemHeight);

        // this.bottomItemNode = cc.instantiate(this.bottomItem);
        // this.bottomItemHeight = this.bottomItemNode.height; // get total content height
        // this.content.addChild(this.bottomItemNode);
        // this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
        // let itemScript = this.bottomItemNode.getComponent('LevelHomeBottom');
        // itemScript.updateItem();

        this.setupEvent();
    },

    setupEvent() {

        this.scrollView.node.on(cc.Node.EventType.TOUCH_END, (event) => {

            if (this.isMoving) {
                this.isMoving = false;
                return;
            }

            var touchPosition = event.getLocation();
            touchPosition = this.scrollView.node.parent.convertToNodeSpaceAR(touchPosition);
            this.clickAddMoney(touchPosition);
        }, this);

        this.scrollView.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
            this.isMoving = true;
        }, this);

        // GameEvent.on();
        // refreshStarEnterRoom

        GameEvent.on(GameNotificationKey.StarEnterRoom, this, (starId, roomId) => {
            this.refreshStarEnterRoom(starId, roomId);
        });

        // this.scrollView.node.on("scrolling", (event) => {
        //
        //     if (this.scrollView._isOutOfBoundary()) {
        //
        //     if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限
        //
        //         this.scrollView._outOfBoundaryAmount.y = 0;
        //         this.scrollView._adjustContentOutOfBoundary();
        //
        //     } else {  // 超出下面的界限
        //
        //         if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) {
        //
        //             if (this.recordScrollViewPosition) {
        //                 this.scrollView.content.setPosition(this.recordScrollViewPosition);
        //                 return;
        //             } else {
        //                 this.recordScrollViewPosition = this.scrollView.getContentPosition();
        //             }
        //
        //         } else {
        //             this.recordScrollViewPosition = null;
        //         }
        //     }
        // }
        // }, this);
    },

    clickAddMoney(position) {
        GameEvent.fire(GameNotificationKey.ClickAddMoney, position);
    },

    start () {

    },

    configBuildings(dataList, isUnlock = false) {

        this.dataList = dataList;

        // this.listAdapter.updateItems(dataList, this.item, this.itemScriptName, this.topItemHeight);

        if (isUnlock) {
            this.listAdapter.addItemsToDataList(this.dataList[0],true);

            //上面的方法会重新计算cotent总高度，所以需要再加回底部的高度
            this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
            this.content.height += this.bottomItemHeight;
            this.scrollView.scrollToOffset(this.scrollPosition, 0.0);
        } else {
            this.scrollView.content.removeAllChildren();

            let top = cc.instantiate(this.topItem);
            this.topItemHeight = top.height; // get total content height
            this.content.addChild(top);
            top.setPosition(0, -(this.topItemHeight * 0.5));
            let topItemScript = top.getComponent('LevelHomeTop');
            topItemScript.updateItem();

            this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName, this.topItemHeight);

            this.bottomItemNode = cc.instantiate(this.bottomItem);
            this.bottomItemHeight = this.bottomItemNode.height; // get total content height
            this.content.addChild(this.bottomItemNode);
            this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
            let itemScript = this.bottomItemNode.getComponent('LevelHomeBottom');
            itemScript.updateItem();

            //上面的方法会重新计算cotent总高度，所以需要再加回底部的高度
            this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
            this.content.height += this.bottomItemHeight;

            this.scrollView.scrollToBottom(0.1);

            // console.log('shenme before ' + this.scrollView.getContentPosition());
            this.scrollPosition = this.scrollView.getContentPosition();
        }
    },

    refreshStarEnterRoom(starId, roomId) {

        if (roomId <= 0) {
            return;
        }

        for (let i = 0; i < this.dataList.length; ++i) {
            let room = this.dataList[i];
            if (room.roomId == roomId) {
                if (room.roomStars == undefined) {
                    room.roomStars = [];
                }
                room.roomStars.push(starId);
            }
        }

        this.listAdapter.updateDataList(this.dataList, roomId);

        var totalStar = 0;
        this.dataList.forEach(n => {
            if (n.isUnlocked) {
                if (n.roomStars.length > 0) {
                    totalStar += n.roomStars.length;
                }
            }
        });
        GameModule.userInfo.starMt = totalStar > 0 ? Math.pow(2,totalStar) : 1;
        GameModule.userInfo.refreshSecondText();
        GameEvent.fire(GameNotificationKey.UpBuildingLevel);
    },

    // update (dt) {},
    onScrollEvent(sender, event) {
        this.listAdapter.update();
    },
});
