local MonthCardNewManager = BaseClass("MonthCardNewManager")
local __init = function(self)
  self.golloesMonthCard = nil
  self.expiredFormationData = nil
  self:AddListener()
end
local __delete = function(self)
  self.golloesMonthCard = nil
  self.expiredFormationData = nil
  self:RemoveListener()
end
local AddListener = function(self)
  EventManager:GetInstance():AddListener(EventId.OnPackageInfoUpdated, self.ResetGolloesMonthCardPack)
end
local ResetGolloesMonthCardPack = function()
  local manager = DataCenter.MonthCardNewManager
  if manager.golloesMonthCard then
    manager.golloesMonthCard.packageData = GiftPackageData.get(manager.golloesMonthCard.monthCardId)
  end
end
local RemoveListener = function(self)
  EventManager:GetInstance():RemoveListener(EventId.OnPackageInfoUpdated, self.ResetGolloesMonthCardPack)
end
local InitGolloesMonthCard = function(self, message)
  if not self.golloesMonthCard then
    self.golloesMonthCard = MonthCardNewInfo.New()
    local allLines = DataCenter.RechargeManager:GetAllLines()
    for i, lineData in pairs(allLines) do
      local tempType = lineData.type
      if tempType == WelfareTagType.MonthCard then
        local tempId = lineData.para1
        self.golloesMonthCard:SetMonthCardId(tempId)
      end
    end
  end
  if not message then
    return
  end
  if not message.golloesMonthCard then
    return
  end
  self.golloesMonthCard:ParseData(message.golloesMonthCard)
end
local UpdateMonthCardData = function(self, message)
  if not message then
    return
  end
  if message.golloesMonthCard then
    if not self.golloesMonthCard then
      self.golloesMonthCard = MonthCardNewInfo.New()
    end
    self.golloesMonthCard:ParseData(message.golloesMonthCard)
  end
  if message.gold then
    LuaEntry.Player.gold = message.gold
    EventManager:GetInstance():Broadcast(EventId.UpdateGold)
  end
  EventManager:GetInstance():Broadcast(EventId.MonthCardInfoUpdated)
  EventManager:GetInstance():Broadcast(EventId.RefreshWelfareRedDot)
end
local GetGolloesMonthCard = function(self)
  return self.golloesMonthCard
end
local CheckIfMonthCardActive = function(self)
  if self.golloesMonthCard and self.golloesMonthCard:IsBought() then
    return true
  else
    return false
  end
end
local CheckIfHasGolloesGift = function(self)
  if not self:CheckIfMonthCardActive() then
    return false
  end
  return not self.golloesMonthCard:IsTodayClaimed()
end
local GetGolloesHeadBg = function(self)
end
local CheckIfGolloesMonthCardAvailable = function(self)
  return self.golloesMonthCard and self.golloesMonthCard.packageData
end
local ShowSubscriptionBubble = function(self)
  local weekCardTag = WelfareController.getShowTagInfoByType(WelfareTagType.WeekCard)
  local monthCardTag = WelfareController.getShowTagInfoByType(WelfareTagType.MonthCard)
  if weekCardTag and not weekCardTag:isShow() then
    return
  end
  if monthCardTag and not monthCardTag:isShow() then
    return
  end
  local weekCard = DataCenter.WeekCardManager:GetWeekCardList()
  local monthCard = DataCenter.MonthCardNewManager:GetGolloesMonthCard()
  local hasActiveSubscription = false
  local hasCanClaim = false
  if weekCard and 0 < #weekCard then
    for i, v in pairs(weekCard) do
      v:RefreshStatus()
      local status = v:GetStatus()
      if status ~= WeekCardPackageStatus.CanBuy then
        hasActiveSubscription = true
      end
      if status == WeekCardPackageStatus.CanClaim then
        hasCanClaim = true
      end
      if hasCanClaim then
        break
      end
    end
  end
  if hasCanClaim then
    return true
  end
  if monthCard then
    hasActiveSubscription = hasActiveSubscription or monthCard:IsBought()
    if monthCard:IsBought() then
      hasCanClaim = not monthCard:IsTodayClaimed()
    end
  end
  if hasCanClaim then
    return true
  end
  if hasActiveSubscription then
    return false
  end
  local todayShow = DataCenter.SecondConfirmManager:GetTodayCanShowSecondConfirm(TodayNoSecondConfirmType.ShowSubscriptionBubble)
  if todayShow then
    return true
  end
  return false
end

function MonthCardNewManager:CheckExistInMonthlyCardCfg(itemId)
  if itemId == nil then
    return false
  end
  if self.MonthlyCardIdList == nil then
    self.MonthlyCardIdList = {}
    LocalController:instance():visitTable("monthcard", function(id, lineData)
      local id = tonumber(lineData:getValue("id")) or 0
      table.insert(self.MonthlyCardIdList, id)
    end)
  end
  for i = 1, #self.MonthlyCardIdList do
    if self.MonthlyCardIdList[i] == itemId then
      return true
    end
  end
  return false
end

function MonthCardNewManager:GetExpiredFormationData()
  return self.expiredFormationData
end

function MonthCardNewManager:SetExpiredFormationData(data)
  self.expiredFormationData = data
end

function MonthCardNewManager:AbandonExpiredFormation()
  UIManager:GetInstance():DestroyWindow(UIWindowNames.UIExpiredMonthlyCardRecover)
  UIUtil.ShowMessage(CS.GameEntry.Localization:GetString("weekcard_squad_save_desc_6"), 1, "weekcard_squad_save_button_4", "", function()
    UIManager:GetInstance():DestroyWindow(UIWindowNames.LWBuyDiamond, {anim = false})
    local buildingData = DataCenter.BuildManager:GetBuildingDatasByBuildingId(BuildingTypes.LW_BUILD_PARKINGLOT_FOUR)[1]
    UIManager:GetInstance():OpenWindow(UIWindowNames.UIHeroPVPFormation, {anim = true}, EnterHeroSquadPanelWay.ExpiredMonthlyCard, buildingData.uuid)
  end, nil, nil, "weekcard_squad_save_title_3")
end

function MonthCardNewManager:RecoverExpiredFormation(data)
  if data == nil then
    return
  end
  UIManager:GetInstance():DestroyWindow(UIWindowNames.UIExpiredMonthlyCardRecover)
  if data.complete == 0 then
    UIUtil.ShowMessage(CS.GameEntry.Localization:GetString("weekcard_squad_save_desc_4"), 1, "weekcard_squad_save_button_4", "", function()
      UIManager:GetInstance():DestroyWindow(UIWindowNames.LWBuyDiamond, {anim = false})
      local buildingData = DataCenter.BuildManager:GetBuildingDatasByBuildingId(BuildingTypes.LW_BUILD_PARKINGLOT_FOUR)[1]
      UIManager:GetInstance():OpenWindow(UIWindowNames.UIHeroPVPFormation, {anim = true}, EnterHeroSquadPanelWay.ExpiredMonthlyCard, buildingData.uuid)
    end, nil, nil, "weekcard_squad_save_title_1")
  elseif data.complete == 1 then
    UIUtil.ShowMessage(CS.GameEntry.Localization:GetString("weekcard_squad_save_desc_3"), 1, "weekcard_squad_save_button_3", "", function()
      UIManager:GetInstance():DestroyWindow(UIWindowNames.LWBuyDiamond, {anim = false})
      UIManager:GetInstance():OpenWindow(UIWindowNames.LWCityDefence)
    end, nil, nil, "weekcard_squad_save_title_1")
  end
  DataCenter.ArmyFormationDataManager:UpdateArmyFormationListData(data.formation)
  EventManager:GetInstance():Broadcast(EventId.ArmyFormatUpdate)
  EventManager:GetInstance():Broadcast(EventId.GF_hero_squad_saved, data.formation.index)
end

MonthCardNewManager.__init = __init
MonthCardNewManager.__delete = __delete
MonthCardNewManager.UpdateMonthCardData = UpdateMonthCardData
MonthCardNewManager.InitGolloesMonthCard = InitGolloesMonthCard
MonthCardNewManager.GetGolloesMonthCard = GetGolloesMonthCard
MonthCardNewManager.CheckIfMonthCardActive = CheckIfMonthCardActive
MonthCardNewManager.GetGolloesHeadBg = GetGolloesHeadBg
MonthCardNewManager.CheckIfHasGolloesGift = CheckIfHasGolloesGift
MonthCardNewManager.CheckIfGolloesMonthCardAvailable = CheckIfGolloesMonthCardAvailable
MonthCardNewManager.AddListener = AddListener
MonthCardNewManager.RemoveListener = RemoveListener
MonthCardNewManager.ResetGolloesMonthCardPack = ResetGolloesMonthCardPack
MonthCardNewManager.ShowSubscriptionBubble = ShowSubscriptionBubble
return MonthCardNewManager
