
cc.Class({
    extends: cc.Component,

    properties: {

        floors: [cc.Node],

        pillars:[cc.Node],

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // for (var i = 0; i < this.floors.length; i++) {
        //     this.floors[i].active = false;
        // }

        // this.upgrade.on('touchend',()=>{
        //     // this.node.runAction(cc.sequence(cc.scaleTo(0.1,1.1,1.1),cc.scaleTo(0,1,1)));
        //     this.showJumpAnimation();
        // })

        // this.node.on('touchend',()=>{
        //     // this.node.runAction(cc.sequence(cc.scaleTo(0.1,1.1,1.1),cc.scaleTo(0,1,1)));
        //     this.showUpgradeAnimation();
        // })
    },

    start() {
        // this.showUpgradeAnimation(this.floors);
    },

    showUpgradeAnimation() {

        this.floorListAnimation(this.floors);
        this.floorListAnimation(this.pillars);

        // this.pillarListAnimation();
    },

    floorListAnimation(floors){
        for (var i = 0; i < floors.length; i++) {
            floors[i].active = false;
        }

        let delayDivider = 0.2;
        let delayOffset = 0;
        let startYOffset = 300;
        let yDivider = 20;

        this.floorAnimation(floors[0], startYOffset);
        for (var i = 1; i < floors.length; i++) {
            let floor = floors[i];
            this.scheduleOnce(() => {
                this.floorAnimation(floor, startYOffset + yDivider * i);
            }, delayOffset + delayDivider * i);
        }
    },

    floorAnimation(floor, startY) {
        floor.active = true;
        floor.y += startY;
        // let down = cc.sequence(this.getEaseAnimation(cc.moveBy(0.2,0,startY)),this.getEaseAnimation(cc.moveBy(0.2,0,-startY)));
        let down = this.getEaseAnimation(cc.moveBy(0.2, 0, -startY));
        let scale1 = this.getEaseAnimation(cc.scaleTo(0.2, 1.1, 1.1));
        let scale2 = this.getEaseAnimation(cc.scaleTo(0.2, 1, 1));
        floor.runAction(cc.sequence(down, scale1, scale2));
    },

    pillarListAnimation(){

        for (var i = 0; i < this.pillars.length; i++) {
            this.pillars[i].active = false;
        }

        let delayDivider = 0.1;
        let delayOffset = 0.2;
        let startYOffset = 300;
        let yDivider = 0;
        let rotateOffset = 20;
        let moveXOffset = 0;

        for (var i = 0; i < this.pillars.length; i++) {
            let item = this.pillars[i];
            let startY = startYOffset + yDivider * i;
            let rotate = (rotateOffset + Math.random()*20)*(i>0?-1:1);
            let moveX = (moveXOffset + Math.random()*10)*(i>1?-1:1);
            this.scheduleOnce(() => {
                this.pillarAnimation(item, startY,rotate,10);
            }, delayOffset + delayDivider * i);
        }
    },

    pillarAnimation(pillar,startY,rotate,moveX){
        pillar.active = true;
        pillar.y += startY;
        // let down = cc.sequence(this.getEaseAnimation(cc.moveBy(0.2,0,startY)),this.getEaseAnimation(cc.moveBy(0.2,0,-startY)));
        let down = this.getEaseBackAnimation(cc.moveBy(0.2, 0, -startY));
        let rotate1 = this.getEaseBackAnimation(cc.rotateBy(0.2,rotate, rotate));
        let rotate2 = this.getEaseBackAnimation(cc.rotateBy(0.2, -rotate, -rotate));
        let move1 = this.getEaseAnimation(cc.moveBy(0.2,0,moveX));
        let move2 = this.getEaseAnimation(cc.moveBy(0.2,0,-moveX));

        let scale1 = this.getEaseBackAnimation(cc.scaleTo(0.2, 1.1, 1.1));
        let scale2 = this.getEaseBackAnimation(cc.scaleTo(0.2, 1, 1));

        let spawn1 = cc.spawn(rotate1,scale1);
        let spawn2 = cc.spawn(rotate2,scale2);



        pillar.runAction(cc.sequence(down, spawn1, spawn2));
    },

    getEaseAnimation(action) {

        return action.easing(cc.easeQuadraticActionInOut());
        // return action;
    },

    getEaseBackAnimation(action){
        return action.easing(cc.easeBackInOut(2));
    },

    showJumpAnimation() {

        for (var i = 0; i < this.floors.length; i++) {
            let floor = this.floors[i];
            this.scheduleOnce(() => {
                this.floorAnimation(floor, startYOffset + yDivider * i);
            }, delayOffset + delayDivider * i);
        }

        for (var i = 0; i < this.pillars.length; i++) {
            let item = this.pillars[i];
            let startY = startYOffset + yDivider * i;
            let rotate = (rotateOffset + Math.random()*20)*(i>0?-1:1);
            let moveX = (moveXOffset + Math.random()*10)*(i>1?-1:1);
            this.scheduleOnce(() => {
                this.pillarAnimation(item, startY,rotate,10);
            }, delayOffset + delayDivider * i);
        }
    }

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