local BirthdayDataManager = BaseClass("BirthdayDataManager")
local Localization = CS.GameEntry.Localization
local rapidjson = require("rapidjson")
local __init = function(self)
  self.birthday_function = nil
  self.isSelfBirthdayFuncOpen = false
  self.isBirthdayDataNeedSetTip = false
  self.isBirthdayDataInfoOpenNeedSetTip = false
  self.isSelfBirthdayDayOpen = false
  self.isBirthdayLetterRewardGetTip = false
  self.curFormat = nil
  self.curFormatExpiredTime = nil
  self.ageListTab = nil
  self.birthdayHistories = nil
  self.birthdayHistoriesTab = nil
  self:AddListener()
end
local __delete = function(self)
  self:RemoveListener()
  self.birthday_function = nil
  self.isSelfBirthdayFuncOpen = nil
  self.isBirthdayDataNeedSetTip = nil
  self.isBirthdayDataInfoOpenNeedSetTip = nil
  self.isSelfBirthdayDayOpen = nil
  self.isBirthdayLetterRewardGetTip = nil
  self.curFormat = nil
  self.curFormatExpiredTime = nil
  self.ageListTab = nil
  self.birthdayHistories = nil
  self.birthdayHistoriesTab = nil
end

function BirthdayDataManager:AddListener()
  EventManager:GetInstance():AddListener(EventId.MainLvUp, self.OnMainLvUpMsg)
  EventManager:GetInstance():AddListener(EventId.OnPassDay, self.OnPassDayMsg)
  EventManager:GetInstance():AddListener(EventId.OnFinishHandleInitMsg, self.OnFinishHandleInitMsg)
  EventManager:GetInstance():AddListener(EventId.BirthdaySetPanelOpenGuidServerRecord, self.OnGuidServerRecord)
  EventManager:GetInstance():AddListener(EventId.GF_item_refreshed, self.OnItemRefreshed)
  EventManager:GetInstance():AddListener(EventId.OnLetterRewardGet, self.OnLetterRewardGet)
  EventManager:GetInstance():AddListener(EventId.GF_enter_game, self.OnGFEnterGame)
  EventManager:GetInstance():AddListener(EventId.OnRewardGetPanelClose, self.OnRewardViewColse)
end

function BirthdayDataManager:RemoveListener()
  EventManager:GetInstance():RemoveListener(EventId.MainLvUp, self.OnMainLvUpMsg)
  EventManager:GetInstance():RemoveListener(EventId.OnPassDay, self.OnPassDayMsg)
  EventManager:GetInstance():RemoveListener(EventId.OnFinishHandleInitMsg, self.OnFinishHandleInitMsg)
  EventManager:GetInstance():RemoveListener(EventId.BirthdaySetPanelOpenGuidServerRecord, self.OnGuidServerRecord)
  EventManager:GetInstance():RemoveListener(EventId.GF_item_refreshed, self.OnItemRefreshed)
  EventManager:GetInstance():RemoveListener(EventId.OnLetterRewardGet, self.OnLetterRewardGet)
  EventManager:GetInstance():RemoveListener(EventId.GF_enter_game, self.OnGFEnterGame)
  EventManager:GetInstance():RemoveListener(EventId.OnRewardGetPanelClose, self.OnRewardViewColse)
end

function BirthdayDataManager:InitData(initMsg)
  if initMsg.birthday_function then
    self.birthday_function = initMsg.birthday_function
  end
  if initMsg.birthdayHistories then
    self.birthdayHistories = initMsg.birthdayHistories
    self.birthdayHistoriesTab = nil
  end
end

function BirthdayDataManager:UpdateData(updateMsg)
  if updateMsg then
    if self.birthday_function == nil then
      self.birthday_function = {
        age = nil,
        birthday = nil,
        modifyTime = nil,
        receiveYear = nil,
        displayType = nil
      }
    end
    if updateMsg.age then
      self.birthday_function.age = updateMsg.age
    end
    if updateMsg.birthday then
      self.birthday_function.birthday = updateMsg.birthday
    end
    if updateMsg.modifyTime then
      self.birthday_function.modifyTime = updateMsg.modifyTime
    end
    if updateMsg.displayType then
      self.birthday_function.displayType = updateMsg.displayType
    end
  end
  self:TryRefreshBirthdaySetNeedTip()
  self:TryRefreshBirthdayDataInfoOpenNeedSetTip()
  self:TryRefreshIsSelfBirthdayDayOpen()
  self:TryRefreshBirthdayLetterRewardGetTip()
end

function BirthdayDataManager.OnMainLvUpMsg()
  local self = DataCenter.BirthdayDataManager
  if self.isSelfBirthdayFuncOpen then
    return
  end
  self:TryRefreshIsSelfBirthdayFuncOpen()
  self:TryRefreshBirthdaySetNeedTip()
  self:TryRefreshBirthdayDataInfoOpenNeedSetTip()
  self:TryRefreshIsSelfBirthdayDayOpen()
end

function BirthdayDataManager.OnPassDayMsg()
  local self = DataCenter.BirthdayDataManager
  self:TryRefreshIsSelfBirthdayFuncOpen()
  self:TryRefreshBirthdaySetNeedTip()
  self:TryRefreshBirthdayDataInfoOpenNeedSetTip()
  self:TryRefreshIsSelfBirthdayDayOpen()
end

function BirthdayDataManager.OnFinishHandleInitMsg()
  local self = DataCenter.BirthdayDataManager
  self:TryRefreshIsSelfBirthdayFuncOpen()
  self:TryRefreshBirthdaySetNeedTip()
  self:TryRefreshBirthdayDataInfoOpenNeedSetTip()
  self:TryRefreshIsSelfBirthdayDayOpen()
end

function BirthdayDataManager.OnGuidServerRecord()
  local self = DataCenter.BirthdayDataManager
  self:TryRefreshBirthdaySetNeedTip()
  self:TryRefreshBirthdayDataInfoOpenNeedSetTip()
end

function BirthdayDataManager.OnItemRefreshed(itemdata)
  if itemdata == nil then
    return
  end
  local self = DataCenter.BirthdayDataManager
  if itemdata.goods.type == GOODS_TYPE.GOODS_TYPE_160 and itemdata.para1 == LetterGroupTypeStr.Birthday then
    self:TryRefreshBirthdayLetterRewardGetTip()
  end
end

function BirthdayDataManager.OnLetterRewardGet(msg)
  local isBirthday = false
  if msg and msg.itemId then
    local itemData = DataCenter.ItemData:GetItemById(msg.itemId)
    if itemData and itemData.goods.type == GOODS_TYPE.GOODS_TYPE_160 and itemData.para1 == LetterGroupTypeStr.Birthday then
      isBirthday = true
    end
  end
  if isBirthday == false then
    return
  end
  local self = DataCenter.BirthdayDataManager
  self:TryRefreshBirthdayLetterRewardGetTip()
  self:TryTipUseBirthdayRedPacket(msg)
end

function BirthdayDataManager.OnGFEnterGame()
  local self = DataCenter.BirthdayDataManager
  self:TryRefreshBirthdayLetterRewardGetTip()
end

function BirthdayDataManager:TryRefreshIsSelfBirthdayFuncOpen()
  if self.isSelfBirthdayFuncOpen == false then
    self.isSelfBirthdayFuncOpen = self:GetCurIsSelfBirthdayFuncOpen()
    if self.isSelfBirthdayFuncOpen then
      EventManager:GetInstance():Broadcast(EventId.OnSelfBirthdayFuncOpen)
    end
  end
end

function BirthdayDataManager:TryRefreshBirthdaySetNeedTip()
  local curData = self:GetCurBirthdaySetNeedTip()
  if curData ~= self.isBirthdayDataNeedSetTip then
    self.isBirthdayDataNeedSetTip = curData
    EventManager:GetInstance():Broadcast(EventId.OnSelfBirthdaySetNeedTipStatusChange, curData)
  end
end

function BirthdayDataManager:TryRefreshBirthdayDataInfoOpenNeedSetTip()
  local curData = self:GetCurBirthdayDataInfoOpenNeedSetTip()
  if curData ~= self.isBirthdayDataInfoOpenNeedSetTip then
    self.isBirthdayDataInfoOpenNeedSetTip = curData
    EventManager:GetInstance():Broadcast(EventId.OnBirthdayDataInfoOpenNeedSetTipStatusChange, curData)
  end
end

function BirthdayDataManager:TryRefreshIsSelfBirthdayDayOpen()
  local curData = self:GetCurIsSelfBirthdayDayOpen()
  if curData ~= self.isSelfBirthdayDayOpen then
    self.isSelfBirthdayDayOpen = curData
    EventManager:GetInstance():Broadcast(EventId.OnSelfBirthdayDayStatusChange, curData)
    if curData then
      self:TryRequestGetBirthdayCard()
    end
  end
end

function BirthdayDataManager:TryRefreshBirthdayLetterRewardGetTip()
  local curData = self:GetCurBirthdayLetterRewardGetTip()
  if curData ~= self.isBirthdayLetterRewardGetTip then
    self.isBirthdayLetterRewardGetTip = curData
    EventManager:GetInstance():Broadcast(EventId.OnBirthdayLetterRewardGetTipStatusChange, curData)
  end
end

function BirthdayDataManager:TryRequestGetBirthdayCard()
  if self.isSelfBirthdayFuncOpen == false then
    return
  end
  if self.birthday_function == nil or self.curFormat == nil then
    return
  end
  local curData = self.isSelfBirthdayDayOpen
  if curData == false then
    return
  end
  local curYear = self.curFormat.year
  local getYear = self.birthday_function.receiveYear
  if getYear == nil or curYear > getYear then
    SFSNetwork.SendMessage(MsgDefines.UserBirthdayCard)
  end
end

function BirthdayDataManager:CheckIsSameTime(timeStr)
  self:TryRefreshCurFormat()
  local isSame = false
  if string.IsNullOrEmpty(timeStr) then
    return isSame
  end
  local timeArr = string.string2array_i_oneSep(timeStr, "-")
  if #timeArr == 2 and self.curFormat.month == tonumber(timeArr[1]) and self.curFormat.day == tonumber(timeArr[2]) then
    isSame = true
  end
  return isSame
end

function BirthdayDataManager:CheckIsPassSetShowArea(playerUid, allianceId, showArea)
  local isPass = false
  if showArea then
    if showArea == BirthdayShowArea.OnlySelf then
      if playerUid == LuaEntry.Player.uid then
        isPass = true
      end
    elseif showArea == BirthdayShowArea.Alliance then
      if playerUid == LuaEntry.Player.uid then
        isPass = true
      elseif allianceId and allianceId == LuaEntry.Player.allianceId then
        isPass = true
      end
    elseif showArea == BirthdayShowArea.All then
      isPass = true
    end
  end
  return isPass
end

function BirthdayDataManager:GetCurIsSelfBirthdayFuncOpen()
  local isOpen = false
  local configDays = LuaEntry.DataConfig:TryGetNum("player_birthday", "k1", 0)
  local cityLv = LuaEntry.DataConfig:TryGetNum("player_birthday", "k2", 0)
  local serverRange = LuaEntry.DataConfig:TryGetNum("player_birthday", "k9", 0)
  local openServerDay = UITimeManager:GetInstance():GetOpenServerDayByOpenServerZero()
  local isCoppaLimit = CoppaUtil.IsCoppaLimit()
  local isServerOpen = self:CheckServerIsOpen(serverRange)
  if configDays <= openServerDay and cityLv <= DataCenter.BuildManager.MainLv and isCoppaLimit == false and isServerOpen then
    isOpen = true
  end
  return isOpen
end

function BirthdayDataManager:CheckServerIsOpen(serverRange)
  local selfServerId = LuaEntry.Player:GetSourceServerId()
  if CS.NetworkURLConfig.IsPressureTest then
    if serverRange == 1 then
      if selfServerId <= 46 then
        return true
      end
    elseif serverRange == 2 then
      return true
    end
  elseif CS.NetworkURLConfig.IsOnline then
    if serverRange == 1 then
      if selfServerId <= 68 then
        return true
      end
    elseif serverRange == 2 then
      return true
    end
  elseif serverRange == 1 or serverRange == 2 then
    return true
  end
  return false
end

function BirthdayDataManager:GetCurBirthdaySetNeedTip()
  local isNeedTip = false
  if self.isSelfBirthdayFuncOpen and not self:GetBirthdayGuidServerRecordHaveSet() and self.birthday_function == nil then
    isNeedTip = true
  end
  return isNeedTip
end

function BirthdayDataManager:GetCurBirthdayDataInfoOpenNeedSetTip()
  local isNeedTip = false
  if self.isSelfBirthdayFuncOpen and not self:GetBirthdayGuidServerRecordInfoOpenHaveSet() and self.birthday_function == nil then
    isNeedTip = true
  end
  return isNeedTip
end

function BirthdayDataManager:GetCurIsSelfBirthdayDayOpen()
  local isOpen = false
  if self.isSelfBirthdayFuncOpen and self.birthday_function and not string.IsNullOrEmpty(self.birthday_function.birthday) and self:CheckIsSameTime(self.birthday_function.birthday) then
    isOpen = true
  end
  return isOpen
end

function BirthdayDataManager:GetCurBirthdayLetterRewardGetTip()
  local isHaveTip = false
  local birthdayGroupType = LetterGroupTypeStr.Birthday
  local items = DataCenter.ItemData:GetItemsByType(GOODS_TYPE.GOODS_TYPE_160)
  if items and 0 < #items then
    for _, item in ipairs(items) do
      if item.para1 == birthdayGroupType then
        local otherParamData = item:GetOtherParamTab()
        if otherParamData.state == nil or toInt(otherParamData.state) == 0 then
          isHaveTip = true
          break
        end
      end
    end
  end
  return isHaveTip
end

function BirthdayDataManager:TryRefreshCurFormat()
  local curTime = UITimeManager:GetInstance():GetServerTime()
  if self.curFormatExpiredTime == nil or curTime > self.curFormatExpiredTime then
    local serverStartZeroTimeStamp = UITimeManager:GetInstance():GetTodayZeroServerTime(curTime // 1000) * 1000
    self.curFormatExpiredTime = serverStartZeroTimeStamp + 86400000
    local changeDeltaTime = UITimeManager:GetInstance():GetTimezoneOffset()
    local time = math.modf((curTime + changeDeltaTime) / 1000)
    self.curFormat = os.date("!*t", time)
  end
end

function BirthdayDataManager:GetBirthdayGuidServerRecordHaveSet()
  local val = DataCenter.GuideRecordDataManager:GetRecordTabData(GuidServerRecordType.BirthdayDataRecord)
  local haveSet = val ~= nil and val == "1"
  return haveSet
end

function BirthdayDataManager:SendBirthdayGuidServerRecord()
  SFSNetwork.SendMessage(MsgDefines.GuideRecord, GuidServerRecordType.BirthdayDataRecord, "1")
end

function BirthdayDataManager:GetBirthdayGuidServerRecordInfoOpenHaveSet()
  local val = DataCenter.GuideRecordDataManager:GetRecordTabData(GuidServerRecordType.BirthdayFuncAfterOpenInfoViewOpen)
  local haveSet = val ~= nil and val == "1"
  return haveSet
end

function BirthdayDataManager:SendBirthdayGuidServerRecordInfoOpen()
  SFSNetwork.SendMessage(MsgDefines.GuideRecord, GuidServerRecordType.BirthdayFuncAfterOpenInfoViewOpen, "1")
end

function BirthdayDataManager:GetSetData()
  return self.birthday_function
end

function BirthdayDataManager:GetBirthdayDataModifyTimeSpaceDayNum()
  local dayNum = LuaEntry.DataConfig:TryGetNum("player_birthday", "k6", 0)
  return dayNum
end

function BirthdayDataManager:GetIsSelfBirthdayFuncOpen()
  return self.isSelfBirthdayFuncOpen
end

function BirthdayDataManager:GetAgeListTab()
  if self.ageListTab == nil then
    self.ageListTab = {}
    local ageListStr = LuaEntry.DataConfig:TryGetStr("player_birthday", "k8", "")
    self.ageListTab = string.string2array_s(ageListStr, ";", "|")
  end
  return self.ageListTab
end

function BirthdayDataManager:GetVisibleNameByType(type)
  local name = ""
  if type == BirthdayShowArea.OnlySelf then
    name = Localization:GetString("birthday_tips_14")
  elseif type == BirthdayShowArea.Alliance then
    name = Localization:GetString("birthday_tips_13")
  elseif type == BirthdayShowArea.All then
    name = Localization:GetString("birthday_tips_12")
  end
  return name
end

function BirthdayDataManager:GetBirthdayCardReward(msg)
  if self.birthday_function and self.curFormat then
    local curYear = self.curFormat.year
    self.birthday_function.receiveYear = curYear
  end
end

function BirthdayDataManager:TryInitBirthdayHistoriesTab()
  if self.birthdayHistoriesTab == nil then
    self.birthdayHistoriesTab = {}
    if self.birthdayHistories and #self.birthdayHistories > 0 then
      for _, v in ipairs(self.birthdayHistories) do
        self.birthdayHistoriesTab[v] = true
      end
    end
  end
end

function BirthdayDataManager:GetIsHaveBirthdayHistoriey(uid)
  local isHave = false
  self:TryInitBirthdayHistoriesTab()
  if self.birthdayHistoriesTab[uid] then
    isHave = true
  end
  return isHave
end

function BirthdayDataManager:SetBirthdayHistoriey(uid)
  self:TryInitBirthdayHistoriesTab()
  self.birthdayHistoriesTab[uid] = true
end

function BirthdayDataManager:GetIsShowBirthdayIconByUserInfo(userInfo)
  local isShow = false
  if userInfo == nil then
    return isShow
  end
  local isHaveSet = false
  if userInfo.uid == LuaEntry.Player.uid then
    if not string.IsNullOrEmpty(userInfo.birthday) then
      isHaveSet = true
    end
  elseif not string.IsNullOrEmpty(userInfo.birthday) and DataCenter.BirthdayDataManager:CheckIsPassSetShowArea(userInfo.uid, userInfo.allianceId, userInfo.birthdayDisplay) then
    isHaveSet = true
  end
  if isHaveSet then
    isShow = DataCenter.BirthdayDataManager:CheckIsSameTime(userInfo.birthday)
  end
  return isShow
end

function BirthdayDataManager:TryTipUseBirthdayRedPacket(msg)
  if CoppaUtil.IsCoppaLimitWithTips() then
    return
  end
  local targetItemId, targetItemData, targetRedPocketTemp
  if msg and msg.reward then
    local rewardList = msg.reward
    for _, reward in pairs(rewardList) do
      if reward.type and reward.type == RewardType.GOODS then
        local value = reward.value
        local itemId = value.itemId
        local itemData = DataCenter.ItemData:GetItemById(itemId)
        local template = DataCenter.ItemTemplateManager:GetItemTemplate(itemId)
        if itemData and template and template.type == GOODS_TYPE.GOODS_TYPE_139 then
          local redPocketTemp = DataCenter.RedPacketTemplateManager:GetTemplateByGoodsId(itemId)
          if redPocketTemp and redPocketTemp.type == RedPacketType.Birthday then
            targetItemId = itemId
            targetItemData = itemData
            targetRedPocketTemp = redPocketTemp
            break
          end
        end
      end
    end
  end
  if targetItemId == nil then
    return
  end
  self.haveTipUseBirthdayRedPacketTargetItemId = targetItemId
end

function BirthdayDataManager:OnRewardViewColse()
  local self = DataCenter.BirthdayDataManager
  if self.haveTipUseBirthdayRedPacketTargetItemId then
    self.haveTipUseBirthdayRedPacketTargetItemId = nil
    UIUtil.ShowMessage(Localization:GetString("birthday_tips_17"), 1, "birthday_btn_1", nil, function()
      GoToUtil.CloseAllWindows()
      UIManager:GetInstance():OpenWindow(UIWindowNames.UIRedPacketBag)
    end)
  end
end

BirthdayDataManager.__init = __init
BirthdayDataManager.__delete = __delete
return BirthdayDataManager
