
const DWTool = require("../utils/DWTool");
const AlertManager = require('../utils/AlertManager');

cc.Class({
    extends: cc.Component,

    properties: {
        scrollNode: cc.Node,
        drawPointer: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

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

    update (dt) {

        if (this.scrollNode.children.length < this.iconCount + 2 ){
            return;
        }

        if (this.downMoveSpeed == 0) {
            return;
        }

        if (this.upMoveTotal < this.upMoveMax) {
            this.upScroll();
        } else {
            this.downScroll();
        }
    },

    onDestroy() {
        GameEvent.off("draw_done_action", this);

        GameEvent.off("draw_again", this);
    },


    init(drawData, typeId) {
        this.drawData = drawData;
        this.typeId = typeId;
        this.initData();
        this.setUpStar();
        this.setUpNotification();
        this.node.active = true;
    },

    initData() {
        this.iconCount = 5;
        this.upMoveSpeed = 10;
        this.upMoveTotal = 0;
        this.upMoveMax = 400;

        this.downMoveSpeed = 35;

        this.downMoveTotal = 0;
        // this.downScroolMax = 0;
    },


    downScroll() {
        /// 降速到最后 该减速了
        if (this.downMoveTotal > this.downScroolMax) {
            this.downMoveSpeed = 15;
        } 
        let tempSpeed = this.downMoveSpeed;
        let length = this.StarNodeArr.length;
        for (let i = 0; i < length; i ++) {
            let childNode = this.StarNodeArr[i];
            let childY = childNode.y - this.downMoveSpeed;
            /// 如果最后一个都开始移动啦
            if (childY < this.lastNodeY ) {
                /// 在最上面对应的node 7 -> 0   6 -> 7
                let count = i == (length - 1) ? 0 : (i + 1);
                let starNode = this.StarNodeArr[count];
                /// 如果小于说明已经移动了，直接加上10 + 208就行了
                if (count < i) {
                    childNode.y = starNode.y + 10 + 208;
                } else {
                    childNode.y = starNode.y + 10 + 208 - this.downMoveSpeed;
                }
            } else {
                childNode.y = childY;
                if (this.downMoveSpeed <= 15) {
                    if (childY > 0 && childY < this.downMoveSpeed) {
                        tempSpeed = this.downMoveSpeed - childY;
                        ////滚到了抽奖的正确位置
                    } else if (childY == 0) {
                        tempSpeed = 0;
                        this.scrollToIndex = i;
                        let count = (i + 1) % 5;
                        AlertManager.showDrawSuccessAlert(this.drawData, this.typeId, count);
                    }
                }
            }
        }
        this.downMoveSpeed = tempSpeed;
        this.downMoveTotal += this.downMoveSpeed;
    },

    upScroll() {
        let length = this.StarNodeArr.length;
        for (let i = 0; i < length; i ++) {
            let childNode = this.StarNodeArr[i];
            let childY = childNode.y + this.upMoveSpeed;
            /// 如果第一个都开始移动啦
            if (childY > this.firstNodeY) {
                /// 在最上面对应的node 0 -> 7   6 -> 7
                let count = i == 0 ? (length - 1) : (i - 1);
                let starNode = this.StarNodeArr[count];
                if (count < i) {
                    childNode.y = starNode.y - 10 - 208;
                } else {
                    childNode.y = starNode.y - 10 - 208 + this.downMoveSpeed;
                }
            } else {
                childNode.y = childY;
            }
        }
        this.upMoveTotal += this.upMoveSpeed;
    },




    setUpNotification() {
        GameEvent.on("draw_done_action",this, () => {
            this.node.destroy();
        });
        
        GameEvent.on("draw_again", this,(againDrawSuccessData) => {
            this.drawData.propId = againDrawSuccessData.propId;
            this.drawData.propName = againDrawSuccessData.propName;
            /// 让它重新滚动
            this.initData();
        });
    },

    setUpStar() {
        let scroolNodeHeight = this.scrollNode.height;
        let starHeight = 208;
        let space = 10;
        /// 显示的个数
        let visibleCount = 0;
        let temp = scroolNodeHeight;
        while(temp > 0) {
            let updateLen = visibleCount == 0 ? starHeight / 2 : (10 + starHeight);
            temp -= updateLen;
            visibleCount ++;
        };

        visibleCount = visibleCount * 2 - 1;
        let arrCount = visibleCount + 2;
        this.StarNodeArr = Array(arrCount);
        this.centerCount = Math.floor(arrCount / 2);
        /// 滚动完一圈的长度
        let scrollCycleLength = (visibleCount - 1) * (starHeight + 10);
        this.downScroolMax = scrollCycleLength * 2;
        /// 多创建两个 用来放在最后和最前面
        DWTool.loadResPrefab("./prefabs/draw/drawStarContent")
        .then((result) => {   
            for (let index = 0; index < arrCount; ++index) {
                let item = cc.instantiate(result);
                let y = (this.centerCount - index) * (space + starHeight);
                if (index == arrCount - 1) {
                    this.lastNodeY = y;
                }
                if (index == 0) {
                    this.firstNodeY = y;
                }
                item.setPosition(0, y);
                this.StarNodeArr[index] = item;
                this.scrollNode.addChild(item);
                let count = (index + 1) % 5;
                let script = item.getComponent('DrawStarContent');
                script.init(count);
            };
        });        
    },

});
