local this = class("pageAchievement", G_UIPageBase)
local _achievementGroupTpl = L_GameTpl:getAchievementGroupTpl()
local _achievementTpl = L_GameTpl:getAchievementTpl()

function this.bind()
  return {
    tonNum = "",
    yinNum = "",
    jinNum = "",
    txt_achievementNum = "",
    scrollList_achievementGroup = {
      moduleName = "pages/achievement/cellAchievementGroupItem"
    },
    snapToItemIndex_achievementGroup = 0,
    moveToItemIndex_achievementGroup = -2,
    moduleCommonTop = {
      moduleName = "modulePages/moduleCommonTop"
    }
  }
end

function this.methods()
  return {
    scrollList_achievementGroup = {
      onClick_item = function(self, cell)
        L_UI:open("pageAchievementGroup", {
          groupId = cell.bind.id
        })
      end
    }
  }
end

function this:check(options, callback)
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.achievement)
  callback(result)
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  L_AchievementStore:listenCallFunc(L_AchievementStore.event.refreshAchievementList, self.onEvent_refreshAchievementList, self)
  local data = {
    name = L_WordsTpl:getValue("ui_pageAchievementGroup"),
    helpBtnBool = false,
    mainBtnBool = false,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end,
    helpBtn = function()
    end,
    mainBtn = function()
      L_UI:backToRoot()
    end
  }
  self.modules.moduleCommonTop:initModule(data)
  self:initAchievementGroupList()
  self:initCupReward()
end

function this:close(options)
  this.super.close(self, options)
  L_AchievementStore:unListenCallFunc(L_AchievementStore.event.refreshAchievementList, self.onEvent_refreshAchievementList)
end

function this:initAchievementGroupList(selectId)
  local achievementGroup_config = _achievementGroupTpl.data
  local list = {}
  for i, v in pairs(achievementGroup_config) do
    local id = _achievementGroupTpl:getId(v)
    local finishCount = L_AchievementStore:getFinishCountOfGroup(id)
    local tplList = _achievementTpl:getTplListByGroupId(id)
    table.insert(list, {
      configData = v,
      img_icon = _achievementGroupTpl:getAchiRes(v),
      txt_name = _achievementGroupTpl:getAchiName(v),
      id = id,
      txt_progress = string.format("%s/%s", finishCount, #tplList)
    })
  end
  table.sort(list, function(a, b)
    return a.id < b.id
  end)
  local count = #list
  for i = 1, count do
    table.insert(list, table.clone(list[i]))
  end
  for i, v in pairs(list) do
    v.index = i - 1
  end
  self.listData = list
  self.bind.scrollList_achievementGroup:clear()
  self.bind.scrollList_achievementGroup:insert_array(list)
  self.bind.snapToItemIndex_achievementGroup = 0
  self.bind.moveToItemIndex_achievementGroup = -2
end

function this:initCupReward()
  local list = L_AchievementStore:getEachLevelFinishNum()
  local achievementTpl = L_GameTpl:getAchievementTpl()
  local achievement_dic_whole = achievementTpl:getEachLevelNum()
  self.bind.tonNum = string.format("%s/%s", list[1] or 0, achievement_dic_whole[1]) or 0
  self.bind.yinNum = string.format("%s/%s", list[2] or 0, achievement_dic_whole[2]) or 0
  self.bind.jinNum = string.format("%s/%s", list[3] or 0, achievement_dic_whole[3]) or 0
  local whole_num = 0
  local complete_num = 0
  for i, v in pairs(achievement_dic_whole) do
    local num = list[i] or 0
    whole_num = whole_num + v
    complete_num = complete_num + num
  end
  self.bind.txt_achievementNum = string.format("%s/%s", complete_num, whole_num)
end

function this:onEvent_refreshAchievementList()
  self:initCupReward()
  for i, v in pairs(self.modules.scrollList_achievementGroup) do
    v:refreshFinishRate()
  end
end

return this
