local this = class("pagePetDuelStatistics", G_UIPageBase)

function this.bind()
  return {
    active_playerTitle = true,
    active_enemyTitle = true,
    active_noneInfo = false,
    statisticsList = {
      moduleName = "pages/petDuel/cellPetStatistics"
    }
  }
end

function this.methods()
  return {
    onClick_closeBtn = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_bg = function(self)
      L_UI:close(self.pageName)
    end
  }
end

function this:preOpen(options)
  self:initPage()
end

function this:initPage()
  local statisticsData = L_KiBoDuelManager:getPetStatistics(C_ERecordValueType.None, 9999999)
  if #statisticsData.enemyData == 0 and #statisticsData.playerData == 0 then
    self.bind.active_playerTitle = false
    self.bind.active_enemyTitle = false
    self.bind.active_noneInfo = true
    self.bind.statisticsList:clear()
    return
  end
  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 length = #statisticsData.playerData
  if length < #statisticsData.enemyData then
    length = #statisticsData.enemyData
  end
  local data = {}
  for i = 1, length do
    table.insert(data, {
      playerData = statisticsData.playerData[i],
      enemyData = statisticsData.enemyData[i]
    })
    if data[i].playerData ~= nil then
      data[i].playerData.maxDamge = maxDamge
      data[i].playerData.maxDamgeTaken = maxDamgeTaken
      data[i].playerData.maxHealing = maxHealing
    end
    if data[i].enemyData ~= nil then
      data[i].enemyData.maxDamge = maxDamge
      data[i].enemyData.maxDamgeTaken = maxDamgeTaken
      data[i].enemyData.maxHealing = maxHealing
    end
  end
  self.bind.statisticsList:clear()
  self.bind.statisticsList:insert_array(data)
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

return this
