var GameStates = require('GameStates');
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.node.lifeNum = Math.floor(this.node.enemyIndex/3)+2
        if(this.node.enemyIndex==12 || this.node.enemyIndex==13)
        this.node.lifeNum = 10
        if(this.node.enemyIndex==14 || this.node.enemyIndex==15)
        this.node.lifeNum = 20
        if(this.node.enemyIndex==16){
            this.node.lifeNum = 99
            this.leftChild = {}
            this.rightChild = {}
            this.defenceAnimation = {}
            this.bossTimer = setInterval(function(){
                if(this.node){
                    if(!this.defenceAnimation.name)
                    this.defenceAnimation = cc.instantiate(this.node.parent.parent.getComponent('GameMode').defenceAnimation)
                    this.defenceAnimation.parent = this.node
                    this.defenceAnimation.scale = 1.5
                    this.defenceAnimation.lifeNum = 10000
                    //召唤
                    if(this.leftChild.name){
                        var pos = this.leftChild.position
                        var pos1 = this.node.parent.parent.getComponent('GameMode').Player.position
                        var x = (pos1.x-pos.x)/(pos1.y-pos.y)*(-1620)+pos1.x
                        this.leftChild.runAction(cc.moveTo(5,x,-2000))
                    }
                    if(this.rightChild.name){
                        var pos = this.rightChild.position
                        var pos1 = this.node.parent.parent.getComponent('GameMode').Player.position
                        var x = (pos1.x-pos.x)/(pos1.y-pos.y)*(-1620)+pos1.x
                        this.rightChild.runAction(cc.moveTo(5,x,-2000))
                    }
                    var enemyIndex = Math.floor(Math.random()*16)
                    var newEnemy = cc.instantiate(this.node.parent.parent.getComponent('GameMode').Enemy[enemyIndex])
                    newEnemy.parent = this.node.parent
                    newEnemy.scale = 1.5
                    newEnemy.addComponent('EnemyConfig')
                    newEnemy.haveParent = 1
                    if(enemyIndex<12)
                    newEnemy.x =  - 250-40
                    else if(enemyIndex<14)
                    newEnemy.x = -250-80
                    else
                    newEnemy.x = -250-160
                    // newEnemy.y = this.node.parent.parent.getComponent('GameMode').frameArr[5][0].y
                    newEnemy.y = 240
                    newEnemy.enemyIndex = enemyIndex
                    newEnemy.scale = 0.6
                    if(enemyIndex>11)
                    newEnemy.scale = 0.7
                    if(enemyIndex>13)
                    newEnemy.scale = 1
                    this.leftChild = newEnemy
                    var enemyIndex1 = Math.floor(Math.random()*15)
                    var newEnemy1 = cc.instantiate(this.node.parent.parent.getComponent('GameMode').Enemy[enemyIndex1])
                    newEnemy1.parent = this.node.parent
                    newEnemy1.scale = 1.5
                    newEnemy1.addComponent('EnemyConfig')
                    newEnemy1.haveParent = 1
                    newEnemy1.x =  250
                    // newEnemy1.y = this.node.parent.parent.getComponent('GameMode').frameArr[5][0].y
                    newEnemy1.y = 240
                    newEnemy1.enemyIndex = enemyIndex1
                    newEnemy1.scale = 0.6
                    if(enemyIndex1>11)
                    newEnemy1.scale = 0.7
                    if(enemyIndex1>13)
                    newEnemy1.scale = 1
                    this.rightChild = newEnemy1
                }
            }.bind(this),20000)
        }
        this.icePrefab = cc.instantiate(this.node.parent.parent.getComponent('GameMode').Ice)
        this.icePrefab.parent = this.node.children[0]
        this.icePrefab.width = 120
        this.icePrefab.height = 120
        if(this.node.enemyIndex>15){
            this.icePrefab.width = 300
            this.icePrefab.height = 300
        }
        if(this.node.enemyIndex>11 && this.node.enemyIndex<14){
            this.icePrefab.width = 240
            this.icePrefab.height = 240
        }
        if(this.node.enemyIndex>13 && this.node.enemyIndex<16){
            this.icePrefab.width = 240
            this.icePrefab.height = 240
        }
        this.icePrefab.active = false
        this.isPause = 1
        setTimeout(function(){
            if(!this.node)
            return
            this.node.isArrive = 1
        }.bind(this),3000)
        setTimeout(function(){
            if(!this.node)
            return
        }.bind(this),3000+Math.random()*1000)
        this.moveEnemyArr = [2,3,7,8,11,12,15,16]
        this.fireEnemyArr = [1,3,5,7,9,11,13,15,16]
        this.skillTime = 10000
        if(GameStates.levelNum == 16){
            this.skillTimer = setTimeout(function(){
                var position = this.node.position
                var bezier = [cc.v2(position.x+1000*(Math.floor(Math.random()*2)-0.5), position.y+1000*(Math.floor(Math.random()*2)-0.5)-200), cc.v2(position.x+1000*(Math.floor(Math.random()*2)-0.5), position.y+1000*(Math.floor(Math.random()*2)-0.5)-200), position];
                this.node.runAction(cc.bezierTo(3,bezier))
                if(this.node.enemyIndex==16){
                    this.node.children[0].getComponent(dragonBones.ArmatureDisplay).playAnimation('attack')
                    setTimeout(function(){
                        this.node.children[0].getComponent(dragonBones.ArmatureDisplay).playAnimation('idle')
                    }.bind(this),2000)
                }
                this.skillTime = 16000
            }.bind(this),5000)            
        }
        this.energyTimer = setInterval(function(){
            var ranNum = Math.random()
            if(this.moveEnemyArr.indexOf(this.node.enemyIndex)!=-1){
                if(ranNum>0.5){
                    var position = this.node.position
                    var bezier = [cc.v2(position.x+1000*(Math.floor(Math.random()*2)-0.5), position.y+1000*(Math.floor(Math.random()*2)-0.5)-200), cc.v2(position.x+1000*(Math.floor(Math.random()*2)-0.5), position.y+1000*(Math.floor(Math.random()*2)-0.5)-200), position];
                    this.node.runAction(cc.bezierTo(3,bezier))
                    if(this.node.enemyIndex==16){
                        this.node.children[0].getComponent(dragonBones.ArmatureDisplay).playAnimation('attack')
                        setTimeout(function(){
                            this.node.children[0].getComponent(dragonBones.ArmatureDisplay).playAnimation('idle')
                        }.bind(this),2000)
                    }
                }
            }
            if(this.fireEnemyArr.indexOf(this.node.enemyIndex)!=-1){
                if(ranNum>=0.5)
                return
                if(this.node.parent.parent.getComponent('GameMode').isTool0)
                return
                var EnemyBullet = cc.instantiate(this.node.parent.parent.getComponent('GameMode').EnemyBullet)
                EnemyBullet.parent = this.node.parent
                EnemyBullet.addComponent('EnemyBullet')
                EnemyBullet.zIndex = this.node.zIndex + 1
                EnemyBullet.position = this.node.position
                EnemyBullet.runAction(cc.moveBy(4,0,-2000))
                if(this.node.enemyIndex==16){
                    EnemyBullet.x = this.node.x-60
                    var EnemyBullet1 = cc.instantiate(this.node.parent.parent.getComponent('GameMode').EnemyBullet)
                    EnemyBullet1.parent = this.node.parent
                    EnemyBullet1.addComponent('EnemyBullet')
                    EnemyBullet1.zIndex = this.node.zIndex + 1
                    EnemyBullet1.position = this.node.position
                    EnemyBullet1.x = this.node.x+160
                    EnemyBullet1.runAction(cc.moveBy(4,0,-2000))
                    this.node.children[0].getComponent(dragonBones.ArmatureDisplay).playAnimation('attack')
                    setTimeout(function(){
                        this.node.children[0].getComponent(dragonBones.ArmatureDisplay).playAnimation('idle')
                    }.bind(this),1000)
                }
            }
        }.bind(this),this.skillTime)
    },
    onCollisionEnter: function (other, self) {
        if(other.node.name=='PlayerBullet'||other.node.name=='Skill0')
        this.node.lifeNum--
        else
        this.node.lifeNum-=10
        if(this.node.lifeNum<0)
        this.node.lifeNum = 0
        this.node.children[0].runAction(cc.sequence(cc.tintTo(0.1,255,0,0),cc.tintTo(0.1,255,255,255)))
        if(this.node.enemyIndex==16){
            this.node.parent.parent.getComponent('GameMode').EnemyNumLabel.string = this.node.lifeNum
        }   
        if(this.node.lifeNum<=0){
            //动画
            var enemyAnimation = cc.instantiate(this.node.parent.parent.getComponent('GameMode').Animation[5])
            enemyAnimation.parent = this.node.parent
            enemyAnimation.position = this.node.position
            if(this.node.haveParent){                
                this.node.parent.parent.getComponent('GameMode').addScore(this.node,this.node.enemyIndex+1)  
                this.node.destroy() 
            }
            // setTimeout(function(){
                if(!this.node || this.node.haveParent)
                return
                GameStates.enemyNum -- 
                if(this.node.enemyIndex<16){
                    this.node.parent.parent.getComponent('GameMode').EnemyNumLabel.string = GameStates.enemyNum
                }    
                if(GameStates.levelNum==17){
                    this.node.parent.parent.getComponent('GameMode').EnemyNumLabel.node.getChildByName('max').active = true
                    this.node.parent.parent.getComponent('GameMode').EnemyNumLabel.string = ''
                }  
                this.node.parent.parent.getComponent('GameMode').addScore(this.node,this.node.enemyIndex+1)        
                if(GameStates.enemyNum<=0){
                    this.node.parent.parent.getChildByName('UI').getChildByName('UI_Life').runAction(cc.sequence(cc.fadeIn(0.1),cc.fadeOut(0.1),cc.fadeIn(0.1),cc.fadeOut(0.1),cc.fadeIn(0.1),cc.fadeOut(0.1),cc.fadeIn(0.1),cc.fadeOut(0.1),cc.fadeIn(0.1)))
                    this.node.parent.parent.getComponent('GameMode').GameOver(1)                    
                }
                cc.audioEngine.play(this.node.parent.parent.getComponent('GameMode').sound[6],false)
                this.node.destroy()
            // }.bind(this),200)            
        }
    },
    onDestroy(){
        clearInterval(this.energyTimer)
        if(this.bossTimer)
        clearInterval(this.bossTimer)
        clearTimeout(this.skillTimer)
    },

    update (dt) {
        if(this.node.enemyIndex==16 && (!this.leftChild.name) && (!this.rightChild.name) && this.defenceAnimation ){
            if(this.defenceAnimation.name)
            this.defenceAnimation.destroy()
        }
        if(this.node.parent.parent.getComponent('GameMode').isTool0 && this.node.isArrive){
            this.isPause = 0
            this.node.pauseAllActions()
            this.icePrefab.active = true
        }
        else{
            if(this.isPause)
            return
            this.isPause = 1
            this.icePrefab.active = false
            this.node.resumeAllActions()
        }
        if(this.node.y<-1000){
            this.node.destroy()
        }
    },
});
