local this = class("moduleBossHpList", G_UIModuleBase)
local _enemyTpl = L_GameTpl:getEnemyTpl()

function this.bind()
  return {
    goContent = false,
    moduleOne = {
      moduleName = "pages/battle/cellBattleBossHp"
    },
    moduleTwoA = {
      moduleName = "pages/battle/cellBattleBossHp"
    },
    moduleTwoB = {
      moduleName = "pages/battle/cellBattleBossHp"
    },
    moduleThreeA = {
      moduleName = "pages/battle/cellBattleBossHp"
    },
    moduleThreeB = {
      moduleName = "pages/battle/cellBattleBossHp"
    },
    moduleThreeC = {
      moduleName = "pages/battle/cellBattleBossHp"
    },
    goModuleOne = false,
    goModuleTwo = false,
    goModuleThree = false
  }
end

function this.methods()
  return {}
end

function this:onEvent_battleStart()
  self:initBoss()
end

function this:onEvent_battleMonsterEntityInfoChange()
  self:initBoss()
end

function this:onEvent_showBossHpNodeStatusChange()
  self.bind.goContent = L_BattleDataManager:getShowBossHpNode()
end

function this:open()
  self.lastBossList = nil
  L_BattleManager:addListener(L_BattleManager.event.battleStart, self.onEvent_battleStart, self)
  L_BattleDataManager:addListener(L_BattleDataManager.event.battleMonsterEntityInfoChange, self.onEvent_battleMonsterEntityInfoChange, self)
  L_BattleDataManager:addListener(L_BattleDataManager.event.showBossHpNodeStatusChange, self.onEvent_showBossHpNodeStatusChange, self)
  self:initBoss()
end

function this:close()
  self.lastBossList = nil
  L_BattleManager:removeListener(L_BattleManager.event.battleStart, self.onEvent_battleStart, self)
  L_BattleDataManager:removeListener(L_BattleDataManager.event.battleMonsterEntityInfoChange, self.onEvent_battleMonsterEntityInfoChange, self)
  L_BattleDataManager:removeListener(L_BattleDataManager.event.showBossHpNodeStatusChange, self.onEvent_showBossHpNodeStatusChange, self)
end

function this:initBoss()
  if not L_BattleDataManager:getShowBossHpNode() then
    self.bind.goContent = false
    return
  end
  local enemyInfo = L_BattleDataManager:getMonsterCsEntityDic()
  local bossList = {}
  for i, v in pairs(enemyInfo) do
    local entity = v
    if entity then
      local tpl = _enemyTpl:getTplById(entity.data.config.id)
      local enemyType = _enemyTpl:getEnemyType(tpl)
      if enemyType == L_Const.enemyType.boss then
        table.insert(bossList, i)
      end
    end
  end
  if not self.lastBossList then
    self.lastBossList = bossList
  else
    local bSame = true
    if #self.lastBossList ~= #bossList then
      bSame = false
    else
      for _, v in ipairs(self.lastBossList) do
        local bExist = false
        for _, v1 in ipairs(bossList) do
          if v1 == v then
            bExist = true
            break
          end
        end
        if not bExist then
          bSame = false
          break
        end
      end
    end
    if bSame then
      return
    else
      self.lastBossList = bossList
    end
  end
  table.sort(bossList, function(a, b)
    return a < b
  end)
  self.bind.goContent = not table.isEmpty(bossList)
  self.bind.goModuleOne = #bossList == 1
  self.bind.goModuleTwo = #bossList == 2
  self.bind.goModuleThree = #bossList == 3
  if #bossList == 1 then
    self.modules.moduleOne:initBoss(bossList[1], 1)
  elseif #bossList == 2 then
    self.modules.moduleTwoA:initBoss(bossList[1], 2)
    self.modules.moduleTwoB:initBoss(bossList[2], 2)
  elseif #bossList == 3 then
    self.modules.moduleThreeA:initBoss(bossList[1], 3)
    self.modules.moduleThreeB:initBoss(bossList[2], 3)
    self.modules.moduleThreeC:initBoss(bossList[3], 3)
  end
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
end

return this
