local this = class("unit")

function this:ctor()
  self.systemDic = {}
end

function this:onUpdate(scale)
  self:callAllSystem(L_EcsConst.triggerName.onUpdate, scale)
end

function this:onDestroy()
  self:callAllSystem(L_EcsConst.triggerName.onDestroy)
end

function this:addSystem(systemType)
  local system = self.systemDic[systemType]
  if system == nil then
    system = require(systemType).new(self)
    self.systemDic[systemType] = system
  end
  return system
end

function this:getSystem(systemType, isCreate)
  local system = self.systemDic[systemType]
  if system == nil and isCreate then
    system = self:addSystem(systemType)
  end
  return system
end

function this:removeSystem(systemType)
  local system = self.systemDic[systemType]
  if system == nil then
    return
  end
  system:onRemove()
  self.systemDic[systemType] = nil
  return system
end

function this:removeAllSystem()
  self:callAllSystem(L_EcsConst.triggerName.onRemove)
  self.systemDic = {}
end

function this:callAllSystem(methodName, ...)
  for _, v in pairs(self.systemDic) do
    if v[methodName] then
      v[methodName](v, ...)
    end
  end
end

return this
