local this = class("moduleBattleStatistics", G_UIModuleBase)
local kiBoDuelTpl = L_GameTpl:getKiboDuelTpl()

function this.bind()
  return {
    statisticsActive = true,
    statisticsToggleIsOn = false,
    statisticsList = {
      moduleName = "pages/petDuel/cellTopStatistics"
    },
    statisticsSmallList = {
      moduleName = "pages/petDuel/cellTopStatisticsSmall"
    },
    tabList = {
      moduleName = "modulePages/tabList/moduleNewCommonTabList"
    }
  }
end

function this.methods()
  return {
    onClick_fold = function(self)
      self.bind.statisticsToggleIsOn = false
      self:setContentInfo(false)
    end,
    onClick_expend = function(self)
      self.bind.statisticsToggleIsOn = true
      self:setContentInfo(true)
    end
  }
end

function this:open()
  L_KiBoDuelManager:addListener(L_KiBoDuelManager.event.restartKiBoDuel, self.restartKiBoDuel, self)
  L_ShortCutManager:addListener(L_ShortCutConst.event.PetDuelStatistics, self.onPetDuelStatistics, self)
  local tabList = {
    L_Const.commonTag.INFO_DAMAGE,
    L_Const.commonTag.INFO_TAKEN,
    L_Const.commonTag.INFO_HEAL
  }
  self.modules.tabList:setData(tabList, handler(self, self.onSelectTabId))
  self.petGuids = L_KiBoDuelManager:getPetGuids()
  self.pets = {}
  self:disposeTimer()
  self.timer = Timer.repeated(0.1, function()
    self:refreshInfo()
  end, self)
  self.bind.statisticsToggleIsOn = L_KiBoDuelManager:getIsShowStatistics()
end

function this:show()
end

function this:close()
  L_KiBoDuelManager:removeListener(L_KiBoDuelManager.event.restartKiBoDuel, self.restartKiBoDuel, self)
  L_ShortCutManager:removeListener(L_ShortCutConst.event.PetDuelStatistics, self.onPetDuelStatistics, self)
  self:disposeTimer()
end

function this:disposeTimer()
  if self.timer ~= nil then
    Timer.remove(self.timer)
    self.timer = nil
  end
end

function this:refreshInfo()
  if not self.isBind then
    return
  end
  self:refreshStatisticsData()
  if #self.bind.statisticsList == #self.pets then
    for i = 1, #self.bind.statisticsList do
      self.bind.statisticsList:change(i, self.pets[i])
    end
  else
    self.bind.statisticsList:clear()
    self.bind.statisticsList:insert_array(self.pets)
  end
  if #self.bind.statisticsSmallList == #self.pets then
    for i = 1, #self.bind.statisticsSmallList do
      self.bind.statisticsSmallList:change(i, self.pets[i])
    end
  else
    self.bind.statisticsSmallList:clear()
    self.bind.statisticsSmallList:insert_array(self.pets)
  end
  if not self.tabId then
    self.tabId = L_Const.commonTag.INFO_DAMAGE
  end
  self.modules.tabList:setSelectTabId(self.tabId)
end

function this:refreshStatisticsData()
  local dataType = self:getDataType(self.tabId)
  local statisticsData = L_KiBoDuelManager:getPetStatistics(dataType, 9999999)
  local playerMaxDamge, playerMaxDamgeTaken, playerMaxHealing = self:getMaxValues(statisticsData.playerData)
  local enemyMaxDamge, enemyMaxDamgeTaken, enemyMaxHealing = self:getMaxValues(statisticsData.enemyData)
  local maxDamge = playerMaxDamge + enemyMaxDamge
  local maxDamgeTaken = playerMaxDamgeTaken + enemyMaxDamgeTaken
  local maxHealing = playerMaxHealing + enemyMaxHealing
  local playerPets = {}
  for k, v in ipairs(statisticsData.playerData) do
    playerPets[v.guid] = v
  end
  local enemyPets = {}
  for _, v in ipairs(statisticsData.enemyData) do
    table.insert(enemyPets, v)
  end
  self.pets = {}
  for k, v in ipairs(self.petGuids) do
    local item = {
      isPlayer = true,
      slotId = v.slotId,
      guid = v.guid,
      is_trial = v.is_trial or false,
      id = v.id,
      value = 0,
      tag = self.tabId
    }
    table.insert(self.pets, item)
    for _, pet in pairs(playerPets) do
      if pet.guid == item.guid then
        item.value = self:getValue(pet)
      end
    end
    if self.tabId == L_Const.commonTag.INFO_DAMAGE then
      item.maxValue = maxDamge
    elseif self.tabId == L_Const.commonTag.INFO_TAKEN then
      item.maxValue = maxDamgeTaken
    elseif self.tabId == L_Const.commonTag.INFO_HEAL then
      item.maxValue = maxHealing
    end
  end
  local duelId = L_PetDuelStore:getCurChallengeLevelId()
  local tpl = kiBoDuelTpl:getTplById(duelId)
  local enemies = kiBoDuelTpl:getEnemy(tpl)
  local selectedPets = {}
  for configId, _ in pairs(enemies) do
    local item = {
      isPlayer = false,
      is_trial = false,
      id = configId,
      value = 0,
      tag = self.tabId
    }
    table.insert(self.pets, item)
    for k, pet in pairs(enemyPets) do
      if not table.containsValue(selectedPets, k) and pet.petConfigId == configId then
        item.value = self:getValue(pet)
        item.guid = pet.guid
        table.insert(selectedPets, k)
      end
    end
    if self.tabId == L_Const.commonTag.INFO_DAMAGE then
      item.maxValue = maxDamge
    elseif self.tabId == L_Const.commonTag.INFO_TAKEN then
      item.maxValue = maxDamgeTaken
    elseif self.tabId == L_Const.commonTag.INFO_HEAL then
      item.maxValue = maxHealing
    end
  end
  table.sort(self.pets, function(a, b)
    return a.value > b.value
  end)
  for k, v in ipairs(self.pets) do
    v.index = k
  end
end

function this:getValue(pet)
  if self.tabId == L_Const.commonTag.INFO_DAMAGE then
    return pet.damgeValue
  elseif self.tabId == L_Const.commonTag.INFO_TAKEN then
    return pet.damgeTakenValue
  elseif self.tabId == L_Const.commonTag.INFO_HEAL then
    return pet.healingValue
  end
  return 0
end

function this:getMaxValues(data)
  local maxDamge = 0
  local maxDamgeTaken = 0
  local maxHealing = 0
  for _, v in pairs(data) do
    maxDamge = maxDamge + v.damgeValue
    maxDamgeTaken = maxDamgeTaken + v.damgeTakenValue
    maxHealing = maxHealing + v.healingValue
  end
  return maxDamge, maxDamgeTaken, maxHealing
end

function this:getDataType(tabId)
  local dataType = C_ERecordValueType.Damage
  if tabId == L_Const.commonTag.INFO_DAMAGE then
    dataType = C_ERecordValueType.Damage
  elseif tabId == L_Const.commonTag.INFO_TAKEN then
    dataType = C_ERecordValueType.ReceiveDamage
  elseif tabId == L_Const.commonTag.INFO_HEAL then
    dataType = C_ERecordValueType.Heal
  end
  return dataType
end

function this:restartKiBoDuel()
  self.tabId = nil
  L_KiBoDuelManager:setIsShowStatistics(self.bind.statisticsToggleIsOn)
end

function this:onSelectTabId(tabId)
  if self.tabId == tabId then
    return
  end
  self.tabId = tabId
  self:refreshInfo()
end

function this:onPetDuelStatistics()
  self.bind.statisticsToggleIsOn = not self.bind.statisticsToggleIsOn
  self:setContentInfo(self.bind.statisticsToggleIsOn)
end

function this:setContentInfo(isOn)
  if isOn then
    self.tabId = self.tabId or L_Const.commonTag.INFO_DAMAGE
    self.bind.statisticsList:clear()
    self:refreshInfo()
  else
    self.bind.statisticsSmallList:clear()
    self:refreshInfo()
  end
end

function this:showStatistics(isShow)
  self.bind.statisticsActive = isShow
end

return this
