local this = {}

function this:createCsEntityEnd(csEntityId, entityType)
  self.playerUnit:createLuaEntity(csEntityId, entityType)
end

function this:disposeCsEntityEnd(csEntityId)
  self.playerUnit:destroyLuaEntity(csEntityId)
end

function this:getPlayerUnit()
  return self.playerUnit
end

function this:setMainControlEntityId(csEntityId)
  self.playerUnit:setMainControl(csEntityId)
  self:sendEvent(self.event.setMainControl, self.playerUnit:getMainControlEntity())
end

function this:getMainControlEntity(bHero)
  if not self.playerUnit then
    return
  end
  if self.playerUnit:getIsRidingMount() and bHero then
    return self.playerUnit:getMainHero()
  end
  return self.playerUnit:getMainControlEntity()
end

function this:getMainHeroEntity()
  if not self.playerUnit then
    return
  end
  return self.playerUnit:getMainHero()
end

function this:getMainControlEntityPos()
  if not self.playerUnit then
    return
  end
  return self.playerUnit:getMainControlEntityPosition()
end

function this:getMainControlEntityRot()
  if not self.playerUnit then
    return
  end
  return self.playerUnit:getMainControlEntityRotation()
end

function this:getMainControlIsFree()
  if not self.playerUnit then
    return
  end
  local mainControl = self:getMainControlEntity()
  local result = true
  if mainControl then
    local state = mainControl:getStateComponent()
    result = state:getCurStateType() == L_Const.stateType.free
  end
  return result
end

function this:getMainControl3CState()
  if not self.playerUnit then
    return
  end
  local mainControl = self:getMainControlEntity()
  local fsm = mainControl:getFsmComponent()
  local curState = fsm:getCurFsmType()
  return curState
end

function this:isInMounting()
  if not self.playerUnit then
    return
  end
  return self.playerUnit:getIsRidingMount()
end

function this:inMountFly()
  if not self.playerUnit then
    return
  end
  return self.playerUnit:getIsRidingMount() and self.playerUnit:getMountStatus() == C_WorldMapPlayerMountStatusType.WmpmstFly
end

function this:inKiBoTransformMainControl()
  if not self.playerUnit then
    return
  end
  return self.playerUnit:getMainControlType() == C_EPlayerMainControlType.KiBoTransform
end

function this:refreshHeroEntity(csEntity)
  local heroEntity = self.playerUnit._entityDic[csEntity]
  if heroEntity then
    heroEntity.goModel = heroEntity:getCsEntity().model.gameObject
    heroEntity.modelControl = heroEntity.goModel:GetComponent(typeof(C_ModelControl))
  end
end

function this:createCustomPlayerModel(forceModWardrobes)
  local configId = L_GameUtil.getDefaultHeroId()
  local unitId = L_GameUtil.getUnitIdByRole(configId, L_Const.roleType.hero)
  local heroModelPath = L_GameUtil.getAvatarModelPathByUnitId(unitId, L_Const.avatarModelIndex.UnitModel)
  local objHandle = C_LuaUtility.InstantiateSync(heroModelPath)
  local obj = objHandle.gameObject
  obj.name = string.concat("hero_", configId)
  local modelCtrl = obj:GetComponent(typeof(C_ModelControl))
  local avatarData = L_ModStore:getPlayerModData(L_PlayerStore:getSex())
  local avatarCtrl = AzurWorld.avatarMgr:SetAvatar(modelCtrl, avatarData)
  return obj, avatarCtrl
end

function this:startRideMountVoice(petGuid)
  local petItem = L_PetStore:getPetItem(petGuid)
  if petItem then
    L_AudioUtil.stopSound(self.petIntoRideVoiceWrap)
    self.petIntoRideVoiceWrap = L_PetStore:playPetVoiceSound(petItem.id, L_PetConst.PetSystemVoiceType.IntoRide)
  end
end

function this:stopRideMountVoice()
  L_AudioUtil.stopSound(self.petIntoRideVoiceWrap)
end

return this
