local this = class("sevenDaySignGE", L_GameEventStore.baseGameEventCls)

function this:ctor(eventId, type, data)
  this.super.ctor(self, eventId, type, data)
  self:listenCallFunc()
  L_TimerManager:newOrResetTimer(self, "checkIfDeltaTimeOverOneDay", function()
    if not self:checkIfDeltaTimeOverOneDay() then
      return
    end
    self:tryAutoOpenPageActivitySevenDaySign()
  end, 2, -1)
end

function this:initData()
  self.data = self.data
  self.data.count = 0
  self.data.lastSignTime = nil
  self.data.rewardCount = 0
  self.hasSyncEventData = false
end

function this:listenCallFunc()
  L_GameEventStore:listenCallFunc(L_GameEventStore.event.gameEventSync, self.tryAutoOpenPageActivitySevenDaySign, self)
  L_GameStore:listenCallFunc(L_GameStore.event.enterGame, self.tryAutoOpenPageActivitySevenDaySign, self)
  L_SystemUnlockStore:listenCallFunc(L_SystemUnlockStore.event.updateSystem, self.tryAutoOpenPageActivitySevenDaySign, self)
  L_UI:addListener(L_UI.pageEvent.closed, self.onEvent_closedPage, self)
end

function this:onEvent_closedPage(pageName)
  if pageName == "pageActivitySevenDaySign" then
    self.isOpened = true
  end
  if pageName == "loading" and not self.isOpened then
    self:tryAutoOpenPageActivitySevenDaySign()
  end
end

function this:populate(protoData)
  local data = protoData.daily_sign
  self.data.count = data.count or self.data.count
  self.data.lastSignTime = data.time or self.data.lastSignTime
  self.data.rewardCount = data.reward_count or self.data.rewardCount
  self.hasSyncEventData = true
end

function this:getData()
  return self.data
end

function this:req_activityDailySign(callback)
  L_Net:sendMessage(MsgGenCode.CSProtoActivityDailySign, {
    u32 = self.id
  }, function(rsp_data, errorCode)
    if errorCode ~= L_Const.errorCode.ErrCodeSucc then
      return
    end
    if callback then
      callback(rsp_data)
    end
  end)
end

function this:checkIfIsLoadingWorldScene()
  return C_WorldSource.isLoadReady == false
end

function this:checkIfDeltaTimeOverOneDay()
  local timeNow = L_TimeUtil.getServerTime()
  local keyString = self:getLastSignTimeStampkey()
  local lastOpenTime = C_PlayerPrefsUtility.GetInt(keyString)
  return not L_TimeUtil.isSameDay(timeNow, lastOpenTime)
end

function this:checkIfCanReceiveReward()
  if self.data:isValid() == false or self.hasSyncEventData == false then
    return false
  end
  if self.data.rewardCount >= self.data.count then
    return false
  end
  return true
end

function this:tryAutoOpenPageActivitySevenDaySign()
  if self.isOpened then
    return
  end
  if not L_GameStore:isEntered() then
    return
  end
  if self:checkIfDeltaTimeOverOneDay() == false and self:checkIfCanReceiveReward() == false then
    return
  end
  if self:checkIfCanReceiveReward() == false then
    return
  end
  if self:checkIfIsLoadingWorldScene() then
    return
  end
  if L_UI:checkPageShown("pageActivitySevenDaySign", true) then
    return
  end
  local keyString = self:getLastSignTimeStampkey()
  local timeNow = L_TimeUtil.getServerTime()
  C_PlayerPrefsUtility.SetInt(keyString, timeNow)
  L_UI:open("pageActivitySevenDaySign", {
    gameEventType = L_Const.ActivityType.AT_DAILY_SIGN
  })
end

function this:getLastSignTimeStampkey()
  return string.format("sevenDaySignAutoOpen_%d_%d", self.id, L_PlayerStore:getPlayerId())
end

function this:dispose()
  L_GameEventStore:unListenCallFunc(L_GameEventStore.event.gameEventSync, self.tryAutoOpenPageActivitySevenDaySign, self)
  L_GameStore:unListenCallFunc(L_GameStore.event.enterGame, self.tryAutoOpenPageActivitySevenDaySign, self)
  L_SystemUnlockStore:unListenCallFunc(L_SystemUnlockStore.event.updateSystem, self.tryAutoOpenPageActivitySevenDaySign, self)
  self.isOpened = false
  L_UI:removeListener(L_UI.pageEvent.closed, self.onEvent_closedPage, self)
  L_TimerManager:clearTimer(self)
end

function this:checkRedDot()
  return self.data.count > self.data.rewardCount
end

return this
