local this = class("entity", L_EcsConst.alive)
local _handle = require(L_R.world.core .. "entity.common.entityHandle")

function this:ctor(...)
  this.super.ctor(self, ...)
  self.csEntity = nil
  self.entityData = nil
  self.entityHandle = _handle.new()
end

function this:bindCsEntity(cEntity)
  assert(cEntity, "\231\148\159\230\136\144\231\154\132\232\191\153\228\184\170\231\142\169\230\132\143\230\156\137\233\151\174\233\162\152  \230\137\190\233\187\145\233\148\133")
  self.csEntity = cEntity
  self.csEntityId = cEntity.data.entityId
  self.UUID = cEntity.data.UUID
  if cEntity.model then
    self.goModel = cEntity.model.gameObject
    self.modelControl = self.goModel:GetComponent(typeof(C_ModelControl))
  end
  self.entityHandle:initialize(cEntity)
  self:bindGameObject(self.csEntity.transform.gameObject)
end

function this:initialize(data)
  self.entityData = data
  self.entityData:initialize(self)
end

function this:getId()
  return -1
end

function this:getUnitId()
  return -1
end

function this:getCsEntityId()
  return self.csEntityId
end

function this:getEntityData()
  return self.entityData
end

function this:getEntityHandle()
  return self.entityHandle
end

function this:activeTrans(flag)
  if L_CommonUtil.isValid(self.csEntity) and L_CommonUtil.isValid(C_EntityManager.GetEntity(self.csEntityId)) then
    AzurWorld.visbleMgr:SetEntityVisibleForLua(flag, self.csEntity.data.entityId, L_Const.entityHideSource.LuaUI, L_Const.entityHideChannle.Visible)
  end
end

function this:setFsmLeave(flag)
  AzurWorld.visbleMgr:SetEntityVisibleForLua(not flag, self.csEntity.data.entityId, L_Const.entityHideSource.LuaUI, L_Const.entityHideChannle.Logic)
end

function this:setTransfer3CStop(flag)
  self.csEntity.data.characterControlProperty:EnableMovement(not flag)
end

function this:UpdatePhysicsVolume()
  self.csEntity.state:UpdatePhysicsVolume()
end

function this:putIn(pos, rot)
  if pos then
    self:setPosition(pos)
  end
  if rot then
    self:setRotation(rot)
  end
end

function this:setPosition(pos)
  self.csEntity:SafeSetPosition(pos)
end

function this:SafeSetPosition(pos)
  self.csEntity:SafeSetPosition(pos)
end

function this:setGroundPosition(pos)
  self.csEntity:SetPosition(pos)
end

function this:setRotation(rot)
  self.csEntity:SetAngles(rot)
end

function this:setSize(size)
  self.csEntity:SetSize(size)
end

function this:setSelfTimeScale(timeScale)
  self.entityData:setSelfTimeScale(timeScale)
end

function this:reset()
end

function this:onDestroy()
  this.super.onDestroy(self)
  self.csEntity = nil
  self.agent = nil
  self.animator = nil
  self.modelControl = nil
end

function this:getCsEntity()
  return self.csEntity
end

function this:getGoModel()
  return self.csEntity.model.gameObject
end

function this:getAnimator(isCreate)
  if self.animator == nil and isCreate and self.goModel then
    self.animator = self.modelControl:GetComponentByType(typeof(C_PlayableAnimator))
  end
  return self.animator
end

function this:getModelControl()
  if self.goModel ~= self:getGoModel() then
    self.modelControl = self:getGoModel():GetComponent(typeof(C_ModelControl))
  end
  return self.modelControl
end

function this:addEventC2LHandler(callback)
  self.entityHandle:addEventC2LHandler(callback)
end

function this:removeEventC2LHandler(callback)
  self.entityHandle:removeEventC2LHandler(callback)
end

function this:getAnimatorComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.animator, isCreate)
end

function this:getStateComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.state, isCreate)
end

function this:getAiComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.ai, isCreate)
end

function this:getLookAtIKComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.lookAtIK, isCreate)
end

function this:getFsmComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.fsm, isCreate)
end

function this:getVoiceComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.voice, isCreate)
end

function this:getModelControlComponent(isCreate)
  return self:getComponent(L_EcsConst.componentType.modelControl, isCreate)
end

return this
