local base = require("Scene.LWBattle.ParkourBattle.Monster.MonsterImpl.MonsterObj")
local ColliderMonsterBase = BaseClass("ColliderMonsterBase", base)
local Const = require("Scene.LWBattle.Const")
local BattleColliderUtils = CS.BattleColliderUtils

function ColliderMonsterBase:Init(logic, mgr, guid, x, y, monsterMeta)
  base.Init(self, logic, mgr, guid, x, y, monsterMeta)
  self.dontCollid = {}
  self.collidCnt = monsterMeta.collide_count
end

function ColliderMonsterBase:OnLoadComplete()
  BattleColliderUtils.AddMonsterCollider(self.viewHandle, self.guid, LayerMask.GetMask("Member"))
end

function ColliderMonsterBase:OnUpdate(deltaTime)
  base.OnUpdate(self, deltaTime)
  if self.logic.battleType and (not (self.logic.battleType ~= Const.ParkourBattleType.Defense or self.airDropping) or self.forceUpdateReversePos) then
    self:UpdateReversePos(deltaTime)
  end
end

function ColliderMonsterBase:UpdateReversePos(deltaTime)
  local pos = self:GetPosition()
  local add = self.logic:GetMoveSpeedZ() * deltaTime
  local z = pos.z - add
  self.y = z
  if self.transform then
    self.transform:Set_position(pos.x, 0, z)
  else
    self.curWorldPos.z = z
  end
end

function ColliderMonsterBase:OnCollisionViewHandle(colliderCount, startIndex, resultList)
  for i = 1, colliderCount do
    local index = startIndex + i
    local targetObjId = resultList[index]
    self:AttackTarget(targetObjId)
  end
end

function ColliderMonsterBase:AttackTarget(targetObjId)
  if self.dontCollid[targetObjId] ~= nil then
    return
  end
  base.DoColliderEffect(self)
  self.dontCollid[targetObjId] = 1
  local tar = DataCenter.LWBattleManager.logic:GetUnit(targetObjId)
  local collide_damage = self.monsterMeta.collide_damage
  if tar and 0 < (tar.curBlood or 0) and #collide_damage == 3 and collide_damage[1] == 2 then
    self:CollidEffect(tar)
    local params = self:GetDealDamageParamTable()
    params.attacker = self
    params.defender = tar
    params.bulletMeta = {}
    params.damageMultiplier = 1
    params.hitPoint = tar:GetPosition()
    params.hitDir = nil
    params.whiteTime = 0.2
    params.stiffTime = nil
    params.hitBackDistance = nil
    params.hitEff = nil
    params.skill = nil
    params.exType = collide_damage[2]
    params.exValue = collide_damage[3]
    params.isCritical = nil
    params.isSplash = nil
    self.battleMgr:DealDamage(params)
    self.collidCnt = self.collidCnt - 1
    if self.collidCnt == 0 then
      self:Death()
    end
  end
end

function ColliderMonsterBase:DestroyView()
  base.DestroyView(self)
  BattleColliderUtils.RemoveMonsterCollider(self.guid)
end

function ColliderMonsterBase:DestroyData()
  self.dontCollid = nil
  self.collidCnt = nil
  base.DestroyData(self)
end

function ColliderMonsterBase:CollidEffect(tar)
  self.battleMgr:ShowEffectObj(Const.ParkourCollidEffectPath, tar:GetPosition(), nil, 1, nil)
end

return ColliderMonsterBase
