local this = class("moduleCapture", G_UIModuleBase)

function this.bind()
  return {
    hudList = {
      moduleName = "pages/battle/cellCatchHud"
    },
    leftList = {
      moduleName = "pages/battle/cellCatchLeftRightHud"
    },
    rightList = {
      moduleName = "pages/battle/cellCatchLeftRightHud"
    }
  }
end

function this.methods()
  return {}
end

function this:created(...)
  this.super.created(self, ...)
end

function this:open()
  self:init_hudList()
  self.bind.leftList:clear()
  self.bind.rightList:clear()
  self:generateCurPetsList()
  L_BattleDataManager:addListener(L_BattleDataManager.event.petCatchNotice, self.onEvent_petCatch, self)
  self.updateHandle = handler(self, self.update)
  C_UpdateSource.AddUpdateEventHandler(self.updateHandle, "moduleCapture.update")
end

function this:close()
  self:removeListener()
  C_UpdateSource.RemoveUpdateEventHandler(self.updateHandle)
end

function this:removeListener()
  L_BattleDataManager:removeListener(L_BattleDataManager.event.petCatchNotice, self.onEvent_petCatch, self)
end

function this:generateCurPetsList()
  local monsters = C_EntityManager.GetSelectMonsterEntities()
  local leftDataList = {}
  local rightDataList = {}
  for i = 0, monsters.Count - 1 do
    local monster = monsters[i]
    local monsterDataLeft = {monster = monster}
    local monsterDataRight = {monster = monster}
    table.insert(leftDataList, monsterDataLeft)
    table.insert(rightDataList, monsterDataRight)
  end
  self.bind.leftList:insert_array(leftDataList)
  self.bind.rightList:insert_array(rightDataList)
end

function this:onEvent_petCatch(args)
end

function this:processMonsterList(monsters, petList, tag)
  for i, v in pairs(petList) do
    local item = v
    if item and item.bind.monster then
      do
        local index = -1
        local isInMonsters = false
        for j = 0, monsters.Count - 1 do
          if item.bind.monster == monsters[j] then
            isInMonsters = true
            index = j
            break
          end
        end
        if isInMonsters then
          if item.bind.playAnimation == "anim_sidebtn_hide" then
            L_TimerManager:stopTimer(self, "playHideAni" .. tag .. tostring(index))
            item.bind.gameObj = true
            item.bind.playAnimation = "anim_sidebtn_show"
          elseif item.bind.gameObj ~= isInMonsters then
            item.bind.gameObj = true
            item.bind.playAnimation = "anim_sidebtn_show"
          end
        elseif item.bind.gameObj ~= isInMonsters and item.bind.playAnimation ~= "anim_sidebtn_hide" then
          item.bind.playAnimation = "anim_sidebtn_hide"
          L_TimerManager:newOrResetTimer(self, "playHideAni" .. tag .. tostring(index), function()
            item.bind.gameObj = false
            item.bind.playAnimation = ""
          end, 0.5)
        end
      end
    end
  end
end

function this:RefreshChangePet()
  C_EntityManager.SortLeftAndRight()
  self:processMonsterList(C_EntityManager.GetLeftMonsters(), self.modules.leftList, "left")
  self:processMonsterList(C_EntityManager.GetRightMonsters(), self.modules.rightList, "right")
end

function this:init_hudList()
  self.hudDataList = {}
  local monsters = C_EntityManager.GetSelectMonsterEntities()
  for i = 0, monsters.Count - 1 do
    table.insert(self.hudDataList, {
      monster = monsters[i]
    })
  end
  self.bind.hudList:clear()
  self.bind.hudList:insert_array(self.hudDataList)
end

function this:update()
  self:RefreshChangePet()
end

function this:refresh()
end

return this
