local this = class("pageGameAct", G_UIPageBase)
local _GameEventTpl = L_GameTpl:getGameEventsTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {}
end

local GameEventModules = {
  [L_Const.ActivityType.AT_SIGN] = "moduleAttendance",
  [L_Const.ActivityType.AT_STAMINA_BOX_UP] = "",
  [L_Const.ActivityType.AT_DAILY_TASK] = "moduleGameActivityTask"
}

function this.bind()
  return {
    activity_list = {
      moduleName = "pages/gameAct/cellGameActivityItem"
    },
    moduleCommonTop = {
      moduleName = "modulePages/moduleCommonTop"
    },
    gameActModule = {
      type = "toggleModule",
      moduleAttendance = {
        assetName = "UI/Pages/GameAct/Modules/moduleAttendance",
        moduleName = "pages/gameAct/moduleAttendance"
      },
      moduleGameActivityTask = {
        assetName = "UI/Pages/GameAct/Modules/moduleGameActivityTask",
        moduleName = "pages/gameAct/moduleGameActivityTask"
      }
    },
    gameActModuleName = "",
    active_signBtn = false
  }
end

function this.methods()
  return {
    onClick_sign = function(self)
      L_UI:open("pageActivitySevenDaySign", {
        gameEventType = L_Const.ActivityType.AT_DAILY_SIGN
      })
    end,
    activity_list = {
      onClick = function(self, data)
        print("\229\189\147\229\137\141\230\180\187\229\138\168\239\191\189?", data.id)
        self:refreshActivityList(data.id)
      end
    }
  }
end

function this:check(options, callback)
  if L_DungeonManager:getIsInTrainTaskDungeon() then
    callback(false)
    return
  end
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.gameAct)
  callback(result)
end

function this:preOpen(options)
  options = options or {}
  if options.jumpParams then
    self.gameEventId = tonumber(options.jumpParams[1])
  else
    self.gameEventId = options.gameEventId or 0
  end
  L_GameEventStore:req_gameEventDatas(function()
    self:initPage()
  end)
  L_GameEventStore:listenCallFunc(L_GameEventStore.event.gameEventSync, self.initPage, self)
  local nowTime = L_TimeUtil:getServerTime()
  local nextDayTime = L_TimeUtil.getNextDayRefreshTime()
  local gapTime = nextDayTime - nowTime
  L_TimerManager:newOrResetTimer(self, "daychange", function()
    L_GameUtil.showCommonTip({
      txtContent = L_WordsTpl:getValue("notice_daily_data_refresh"),
      closeCallback = function()
        L_UI:backToRoot()
      end,
      hideConfirm = true,
      hideCancel = true
    })
  end, gapTime)
  local data = {
    name = L_WordsTpl:getValue("ui_hintActivity_01"),
    helpBtnBool = false,
    mainBtnBool = true,
    closeCurPage = function()
      L_UI:close(self.pageName)
    end,
    helpBtn = function()
    end,
    mainBtn = function()
      L_UI:backToRoot()
    end
  }
  self.modules.moduleCommonTop:initModule(data)
end

function this:close(options)
  L_GameEventStore:unListenCallFunc(L_GameEventStore.event.gameEventSync, self.initPage)
  L_TimerManager:clearTimer(self)
end

function this:initPage()
  local act_data = L_GameEventStore:getAllGameEventDatas()
  self.actData = {}
  for i, v in pairs(act_data) do
    if v:isShow() then
      local tpl = _GameEventTpl:getTplById(v.id)
      local name = L_Lang:get(_GameEventTpl:getName(tpl))
      local isShowInList = _GameEventTpl:getShow(tpl) == 1
      if isShowInList then
        table.insert(self.actData, {
          id = v.id,
          show_received_icon = v.id == self.actId,
          act_name = name,
          act_name_selected = name,
          reddotName = string.format(L_ReddotManager.DotDef.GameAct_New, v.id),
          sort = _GameEventTpl:getSort(tpl)
        })
      end
    end
  end
  table.sort(self.actData, function(a, b)
    return a.sort < b.sort
  end)
  self.bind.activity_list:clear()
  if #self.actData > 0 then
    self.bind.activity_list:insert_array(self.actData)
    self.actId = not math.isEmpty(self.gameEventId) and self.gameEventId or self.actData[1].id
  end
  self:refreshActivityList(self.actId)
  self.bind.active_signBtn = false
  local sevenDaySignGameEventData = L_GameEventStore:getDataByType(L_Const.ActivityType.AT_DAILY_SIGN)
  if sevenDaySignGameEventData and sevenDaySignGameEventData:canParticipateIn() then
    self.bind.active_signBtn = true
    local sevenDaySignGameEventId = sevenDaySignGameEventData:getActivityId()
    L_ReddotManager:registerReddot(self.bindComponents.trans_signEventRedDot, string.format(L_ReddotManager.DotDef.GameAct_EventsInCenter, sevenDaySignGameEventId))
  end
end

function this:refreshActivityList(id)
  self:initModulePage(id)
  for i = 1, #self.bind.activity_list do
    if self.bind.activity_list:getValue(i, "id") == id then
      self.bind.activity_list:change(i, {show_received_icon = true})
    else
      self.bind.activity_list:change(i, {show_received_icon = false})
    end
  end
end

function this:initModulePage(id)
  if id then
    local tpl = _GameEventTpl:getTplById(id)
    local eType = _GameEventTpl:getType(tpl)
    self.bind.gameActModuleName = GameEventModules[eType]
    self.modules.gameActModule[GameEventModules[eType]]:setActId(id)
  else
    self.bind.gameActModuleName = ""
  end
end

return this
