var NULL = require('./helper/null').NULL
var BehaviorContainer = require('./behavior-container').BehaviorContainer

function BehaviorSystem () {
  this._init()
}

BehaviorSystem.prototype = {

  _init: function () {
    this._children = []
  },

  enable: function (gameObject) {
    if (gameObject.behaviors === NULL) {
      var container = new BehaviorContainer(gameObject, this)
      container.id = this._children.push(container) - 1
      gameObject.behaviors = container
    }
    return gameObject
  },

  disable: function (gameObject) {
    var index = (gameObject.behaviors !== NULL) && (gameObject.behaviors.id >= 0)
    if (index) {
      gameObject.behaviors = NULL
      this._children[index] = NULL
    }
    return gameObject
  },

  processAll: function (methodName) {
    var i = 0
    var len = this._children.length

    for (; i < len; i++) {
      var child = this._children[i]
      if (child !== NULL) {
        child.process(methodName)
      }
    }
  }
}

module.exports = {
  BehaviorSystem: BehaviorSystem
}

var NULL = require('./helper/null').NULL
var BehaviorContainer = require('./behavior-container').BehaviorContainer

function BehaviorSystem () {
  this._init()
}

BehaviorSystem.prototype = {

  _init: function () {
    this._children = []
  },

  enable: function (gameObject) {
    if (gameObject.behaviors === NULL) {
      var container = new BehaviorContainer(gameObject, this)
      container.id = this._children.push(container) - 1
      gameObject.behaviors = container
    }
    return gameObject
  },

  disable: function (gameObject) {
    if (gameObject.behaviors !== NULL) {
      var index = gameObject.behaviors.id
      gameObject.behaviors = NULL
      this._children[index] = NULL
    }
    return gameObject
  },

  processAll: function (methodName) {
    var i = 0
    var len = this._children.length

    for (; i < len; i++) {
      var child = this._children[i]
      if (child !== NULL) {
        child.process(methodName)
      }
    }
  }
}

module.exports = {
  BehaviorSystem: BehaviorSystem
}

fix .disable