local GenDefenseMonsterTask = BaseClass("GenDefenseMonsterTask")
local Const = require("Scene.LWBattle.Const")
GenDefenseMonsterTask.GenType = {FixPoint = 1, Range = 2}

function GenDefenseMonsterTask:__init(mgr, bornMeta)
  self.mgr = mgr
  self.lastTickTime = 0
  self.bornMeta = bornMeta
  self.genNum = 0
  self.initDefenseOffsetZ = 0
  if mgr.logic and mgr.logic.defenseOffsetZ then
    self.initDefenseOffsetZ = mgr.logic.defenseOffsetZ
  end
  local spl = string.split(bornMeta.para, "|")
  if bornMeta.type == 2 then
    self.type = GenDefenseMonsterTask.GenType.Range
    self.limit = tonumber(spl[2])
    self.interval = tonumber(spl[3]) / 1000
    self.r1 = tonumber(spl[4])
    self.r2 = tonumber(spl[5])
    local splMon = string.split(bornMeta.monster, ",")
    self.monsterArr = {}
    for k, v in pairs(splMon) do
      local innerSpl = string.split(v, "|")
      local randomParam = {
        id = tonumber(innerSpl[1]),
        p = tonumber(innerSpl[2])
      }
      table.insert(self.monsterArr, randomParam)
    end
  elseif bornMeta.type == 3 then
    self.type = GenDefenseMonsterTask.GenType.FixPoint
    self.limit = tonumber(spl[2])
    self.interval = tonumber(spl[3]) / 1000
    local splMon = string.split(bornMeta.monster, ",")
    self.monsterArr = {}
    for k, v in pairs(splMon) do
      local innerSpl = string.split(v, "|")
      local randomParam = {
        id = tonumber(innerSpl[1]),
        p = tonumber(innerSpl[2])
      }
      table.insert(self.monsterArr, randomParam)
    end
  end
end

function GenDefenseMonsterTask:__delete()
end

function GenDefenseMonsterTask:Update()
  local now = Time.time
  if now - self.lastTickTime > self.interval then
    self.lastTickTime = now
    self:DoLogic()
  end
end

function GenDefenseMonsterTask:DoLogic()
  self.genNum = self.genNum + 1
  if self.genNum > self.limit then
    self.mgr:RemoveTask(self)
    return
  end
  if self.type == GenDefenseMonsterTask.GenType.FixPoint then
    self:FixPointsGen()
  elseif self.type == GenDefenseMonsterTask.GenType.Range then
    self:RangeGen()
  end
end

function GenDefenseMonsterTask:RangeGen()
  local y = self.mgr.logic.team:GetPositionZ()
  local x = Const.ParkourSceneCenter
  local dir = Vector3.Normalize(Vector3.New(math.random(-10000, 10000), 0, math.random(-10000, 20000)))
  local len = math.random(self.r2, self.r1)
  local genPos = Vector3.New(x, 0, y + 10) + dir * len
  local r = math.random(0, 10000)
  local mId = 0
  for _, v in ipairs(self.monsterArr) do
    mId = v.id
    if r > v.p then
      break
    end
  end
  if 0 < mId then
    local m = self.mgr:CreateMonster(genPos.x, genPos.z, mId)
    if m then
      m:ModifyPosY(genPos.z + self.initDefenseOffsetZ)
      m:Load()
    end
  end
end

function GenDefenseMonsterTask:FixPointsGen()
  local spl = string.split(self.bornMeta.coord, ",")
  local x = tonumber(spl[1])
  local y = tonumber(spl[2])
  local r = math.random(0, 10000)
  local mId = 0
  for _, v in ipairs(self.monsterArr) do
    mId = v.id
    if r > v.p then
      break
    end
  end
  if 0 < mId then
    local m = self.mgr:CreateMonster(x, y, mId)
    if m then
      m:ModifyPosY(y + self.initDefenseOffsetZ)
      m:Load()
    end
  end
end

return GenDefenseMonsterTask
