local this = class("moduleActivityBanner", G_UIModuleBase)
local WIDTH_CELLACTIVEITY = 523
local commonBannerTpl = L_GameTpl:getCommonBannerTpl()
local gameEventTpl = L_GameTpl:getGameEventsTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {
    curIndex = 1,
    autoTimer = nil,
    isMoving = false
  }
  self.funcMap = {
    [1] = self.jumpToSystem,
    [2] = self.jumpToEventSid,
    [3] = self.jumpToNet,
    [4] = function()
    end
  }
  self.checkOpenMap = {
    [1] = self.checkOpenSystem,
    [2] = self.checkOpenGameEvent
  }
end

function this.bind()
  return {
    loopListBanner = {
      moduleName = "pages/Main/cellActivity"
    },
    loopListBannerPoint = {
      moduleName = "pages/Main/cellActivityPoint"
    },
    snapToItemIndex = 0
  }
end

function this.methods()
  return {
    onSnapFinished = function(self, index)
      self:openAutoPlay()
    end,
    onDrag = function(self, isDragging)
      if isDragging then
        self:closeAutoPlay()
      end
    end
  }
end

function this:close(options)
  this.super.close(self, options)
  self:closeAutoPlay()
  self.bindComponents.list_activity:DOKill()
end

function this:hide()
  self.isHide = true
end

function this:show()
  if self.isHide then
    self.isHide = false
    self.bind.snapToItemIndex = self.realIndex + 1
  end
end

function this:initModule()
  self.realIndex = 0
  self.isHide = false
  self:initBannerList()
  self.updateTimer = Timer.repeated(0.33, self.onScrollBox, self)
  self:openAutoPlay()
  L_GameEventStore:listenCallFunc(L_GameEventStore.event.gameEventSync, self.initBannerList, self)
end

function this:initBannerList()
  local activityConf = {}
  local activityPointList = {}
  for _, tpl in pairs(commonBannerTpl:getAllData()) do
    if commonBannerTpl:getShow(tpl) == 1 then
      local openTime = L_TimeUtil.getDateTimeStamp(commonBannerTpl:getStartTime(tpl))
      local closeTime = L_TimeUtil.getDateTimeStamp(commonBannerTpl:getEndTime(tpl))
      local nowTime = L_TimeUtil.getServerTime()
      local open = true
      if self.checkOpenMap[commonBannerTpl:getType(tpl)] then
        open = self.checkOpenMap[commonBannerTpl:getType(tpl)](self, commonBannerTpl:getParam(tpl))
      end
      if openTime <= nowTime and closeTime >= nowTime and open then
        local tmpConf = {
          img_activity = commonBannerTpl:getPic(tpl),
          clickBannerCallback = self.funcMap[commonBannerTpl:getType(tpl)],
          activityId = commonBannerTpl:getId(tpl),
          order = commonBannerTpl:getOrder(tpl),
          param = commonBannerTpl:getParam(tpl)
        }
        table.insert(activityConf, tmpConf)
        table.insert(activityPointList, {
          activityId = commonBannerTpl:getId(tpl),
          order = commonBannerTpl:getOrder(tpl)
        })
      end
    end
  end
  table.sort(activityConf, function(a, b)
    return a.order < b.order
  end)
  if activityConf ~= nil and #activityConf == 1 then
    table.insert(activityConf, table.clone(activityConf[1]))
  end
  table.sort(activityPointList, function(a, b)
    return a.order < b.order
  end)
  self.bindComponents.enableScroll = true
  if #activityConf < 8 then
    if #activityConf < 2 then
      self.bindComponents.enableScroll = false
    else
      local count = #activityConf
      while #activityConf < 8 do
        for i = 1, count do
          table.insert(activityConf, table.clone(activityConf[i]))
        end
      end
    end
  end
  self.bind.loopListBanner:clear()
  self.bind.loopListBanner:insert_array(activityConf)
  self.bindComponents.trans_imgBanner.gameObject:SetActive(#activityConf ~= 0)
  self.bind.loopListBannerPoint:clear()
  self.bind.loopListBannerPoint:insert_array(1 < #activityPointList and activityPointList or {})
  self:setPointListActive()
end

function this:setPointListActive()
  local index = self.data.curIndex
  for i = 1, #self.bind.loopListBannerPoint do
    local id = self.bind.loopListBanner:getValue(index, "activityId")
    local active = id == self.bind.loopListBannerPoint:getValue(i, "activityId")
    self.bind.loopListBannerPoint:change(i, {
      go_active = active,
      go_normal = not active
    })
  end
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.list_activityPoint)
end

function this:openAutoPlay()
  if self.data.autoTimer or #self.bind.loopListBanner <= 1 then
    return
  end
  local _run = function()
    self.bind.snapToItemIndex = self.realIndex + 1
  end
  local interval = L_GameTpl:getGameConstTpl():getValue("COMMON_BANNER_STAYTIME") / 1000
  self.data.autoTimer = Timer.repeated(interval, _run)
end

function this:realSnapToItemIndex(snapIndex)
  local index = snapIndex
  if index == 0 then
    index = #self.bind.loopListBanner
  else
    index = index % #self.bind.loopListBanner
    index = index == 0 and #self.bind.loopListBanner or index
  end
  return index
end

function this:onScrollBox()
  local loopListViewRoot = self.bindComponents.loopListBanner
  if loopListViewRoot == nil then
    return
  end
  local index = loopListViewRoot.CurSnapNearestItemIndex
  if index == nil then
    return
  end
  self.realIndex = index
  local snapIndex = index + 1
  local curIndex = self:realSnapToItemIndex(snapIndex)
  self.data.curIndex = curIndex
  self:setPointListActive()
end

function this:jumpToSystem(param)
  if string.isEmpty(param) then
    return
  end
  L_JumpMgr:jumpTo(tonumber(param))
end

function this:jumpToEventSid(param)
  if string.isEmpty(param) then
    return
  end
  local id = tonumber(param)
  local tpl = gameEventTpl:getTplById(id)
  local eType = gameEventTpl:getType(tpl)
  if eType == L_Const.ActivityType.AT_DAILY_SIGN then
    local sevenDaySignGameEventData = L_GameEventStore:getDataByType(L_Const.ActivityType.AT_DAILY_SIGN)
    if sevenDaySignGameEventData and sevenDaySignGameEventData:canParticipateIn() then
      L_UI:open("pageActivitySevenDaySign", {
        gameEventType = L_Const.ActivityType.AT_DAILY_SIGN
      })
    else
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_completeTask_01"))
      return
    end
  else
    L_UI:open("pageGameAct", {
      gameEventId = tonumber(param)
    })
  end
end

function this:jumpToNet(param)
  if string.isEmpty(param) then
    return
  end
  CS.UnityEngine.Application.OpenURL(param)
end

function this:checkOpenSystem(param)
  if string.isEmpty(param) then
    return true
  end
  return L_JumpMgr:checkJumpFuncOpen(tonumber(param))
end

function this:checkOpenGameEvent(param)
  if string.isEmpty(param) then
    return true
  end
  local activityGameEventData = L_GameEventStore:getGameEventData(tonumber(param))
  if activityGameEventData and activityGameEventData:canParticipateIn() then
    return true
  else
    return false
  end
end

function this:closeAutoPlay()
  if self.data.autoTimer == nil then
    return
  end
  self.data.autoTimer:destroy()
  self.data.autoTimer = nil
end

function this:close()
  this.super.close(self)
  self.isHide = false
  if self.updateTimer ~= nil then
    self.updateTimer:destroy()
    self.updateTimer = nil
  end
  L_GameEventStore:unListenCallFunc(L_GameEventStore.event.gameEventSync, self.initBannerList)
end

return this
