//Augment Module
var ITW = (function(itw){
    "use strict";
    //aliases
    var managers, protot, _this, _super;
    itw.managers = itw.managers || Object.create(null);
    managers = itw.managers;

    //privates - initialize on constructor function
    var _stageWidth;
    var _waves;
    var _waveTimer, _waveChance, _waveObstacleDist;
    var _wavesPaused;

    managers.WaveManager = function(){
        this.super.call(this);
        _this = this;
        _super = _this.super;

        var _init = function(){
            // init stuff here
            _stageWidth = ITW.SceneMgr.getStageWidth();
            _waves = [];

            _this.spawnWave( 1 );
            _this.spawnWave( 1 );
            // _this.spawnWave( 1 );
            _this.spawnWave( 2 );
            _this.spawnWave( 2 );
            // _this.spawnWave( 2 );

            var initPosX = EHDI.NumberUtil.randomRange( _stageWidth * 0.1, _stageWidth * 0.3 );
            for( var i = 0; i < 2; i++ ) {
                _this.resetWave( initPosX );
                initPosX += EHDI.NumberUtil.randomRange( _stageWidth * 0.3, _stageWidth * 0.5 );
            }

            _waveTimer = 0;
            _waveChance = 0.6;
            _waveObstacleDist = _stageWidth * 0.85;
            _wavesPaused = false;
        }
        _init();
    }

    protot = managers.WaveManager.prototype = Object.create(EHDI.aka.Container.prototype);
    protot.constructor = managers.WaveManager;
    protot.super = EHDI.aka.Container;

    protot.loop = function( dt ){
        if( ITW.GameMgr.getPaused() === true ) return;
        if( ITW.GameMgr.getGameOver() === true ) return;

        if( _waveObstacleDist > _stageWidth * 0.6 ){
            _waveObstacleDist -= dt * ( ITW.GameMgr.getSpeed() * 0.35 );
        }
        else{
            _waveTimer += dt;

            if( _waveTimer > 0.35 ){
                _waveTimer = 0;
                _this.resetWave();
            }
        }
    }

    protot.spawnWave = function( type ){
        var waveObj = new ITW.components.Wave( type );
        _this.addChild( waveObj );
        _waves.push( waveObj );
    }

    protot.resetWave = function( posX ){
        if( typeof posX === "number" && EHDI.NumberUtil.randomRange(0, _waveChance) > 0.3 ){
            _waveChance -= 0.1;
            return;
        }

        var randomIndex;
        do{
            randomIndex = Math.floor( EHDI.NumberUtil.randomRange(0, _waves.length) );
            if( _waves[ randomIndex ].x > 0 ){
                randomIndex = -1;
            }
        }while( randomIndex == -1 );

        var waveObj = _waves[ randomIndex ];
        waveObj.resetState( posX );

        _waveObstacleDist = waveObj.x + waveObj.width;
        _waveChance += 0.1;
    }

    protot.pauseWaves = function( state ){
        for ( var i = 0; i < _waves.length; i++ ) {
            _waves[i].pauseTimeline( state );
        }
    }

    protot.destroy = function(){
      while(_waves.length > 0)_waves.pop().destroy({children: true});
      _super.prototype.destroy.call(this, {children: true});
    }

    return itw;
}(ITW || Object.create(null)));