local FirstPayManager = BaseClass("FirstPayManager", CEventable)
local Localization = CS.GameEntry.Localization
local OnUnlockArea = function(self, landId)
  if self.firstPayLandId == nil then
    self.firstPayLandId = LuaEntry.DataConfig:TryGetNum("first_pay_lw", "k7")
  end
  if landId == self.firstPayLandId then
    local pack, rechargeId = self:GetFirstPayPack()
    if pack ~= nil then
      DataCenter.UIPopWindowManager:Push(UIWindowNames.UIHeroExhibitPanel, {anim = false}, 50009, {50009}, nil, true)
      DataCenter.UIPopWindowManager:Push(UIWindowNames.UIFirstPay, {
        anim = false,
        UIMainAnim = UIMainAnimType.AllHide
      }, {delay = 0.5})
      DataCenter.SecondConfirmManager:SetTodayNoShowSecondConfirm(TodayNoSecondConfirmType.FirstPayShowHero, false)
      PostEventLog.Track(PostEventLog.Defines.PlayFirstPayTimeline, {
        param1 = tostring(DataCenter.MonopolyManager.player.curId)
      })
    end
  end
end

function FirstPayManager:OnFirstPayBuildAdd()
end

local DeleteDelayTimer = function(self)
  if self.delayTimer ~= nil then
    self.delayTimer:Stop()
    self.delayTimer = nil
  end
end
local AddDelayTimer = function(self)
  DeleteDelayTimer(self)
  local curTime = UITimeManager:GetInstance():GetServerTime()
  local delayTime = (self.endTime - curTime) / 1000 + 1
  if 0 < delayTime then
    self.delayTimer = TimerManager:GetInstance():DelayInvoke(function()
      self:UpdateBuildingState()
    end, delayTime)
    self.delayTimer:Start()
  end
end
local SetFirstPayBuildFixingState = function(self, building, state)
  if not IsNull(CS.SceneManager.World) and building then
    local buildData = building
    local build = CS.SceneManager.World:GetBuildingByPoint(buildData.pointId)
    if build and not IsNull(build.gameObject) then
      local effectParent = build.gameObject.transform:Find("ModelGo/EffectGo")
      local fixingEffect = build.gameObject.transform:Find("ModelGo/EffectGo/Eff_vehicle_weixiu")
      if not IsNull(effectParent) and not IsNull(fixingEffect) then
        effectParent.gameObject:SetActive(state)
        fixingEffect.gameObject:SetActive(state)
        if state then
          AddDelayTimer(self)
        end
      end
    end
  end
end
local OnBuildInView = function(self, uid)
  local bUuid = tonumber(uid)
  local buildData = DataCenter.BuildManager:GetBuildingDataByUuid(bUuid)
  if buildData and buildData.itemId == BuildingTypes.LW_FIRST_PAY then
    local status = false
    local curTime = UITimeManager:GetInstance():GetServerTime()
    if self.status == FirstPayState.Repairing and curTime < self.endTime then
      status = true
    end
    SetFirstPayBuildFixingState(self, buildData, status)
  end
end
local UpdateBuildingState = function(self)
  local buildData = DataCenter.BuildManager:GetFunbuildByItemID(BuildingTypes.LW_FIRST_PAY)
  if buildData then
    local status = false
    local curTime = UITimeManager:GetInstance():GetServerTime()
    if self.status == FirstPayState.Repairing and curTime < self.endTime then
      status = true
    end
    SetFirstPayBuildFixingState(self, buildData, status)
  else
    DeleteDelayTimer(self)
  end
end
local OnBuildOutView = function(self, bUuid)
  local bUuid = tonumber(bUuid)
  local buildData = DataCenter.BuildManager:GetBuildingDataByUuid(bUuid)
  if buildData and buildData.itemId == BuildingTypes.LW_FIRST_PAY then
    DeleteDelayTimer(self)
  end
end
local __init = function(self)
  self.status = FirstPayState.DontHaveBuilding
  self.time = 0
  self.endTime = 0
  self.viewRewards = nil
  self.viewExtraxRewards = nil
  self.is_new = false
end
local __delete = function(self)
  DeleteDelayTimer(self)
  self.viewRewards = {}
  self.viewExtraxRewards = {}
  self.is_new = false
end
local IsNewFirstPay = function(self)
  if self.is_new then
    return self.is_new
  end
  return false
end
local InitData = function(self, message)
  local firstPayMessage = message.limit_time_gift
  if not table.IsNullOrEmpty(firstPayMessage) then
    if firstPayMessage.is_new ~= nil then
      self.is_new = firstPayMessage.is_new == 1
    else
      local prevStatus = self.status
      self.status = firstPayMessage.status
      self.time = firstPayMessage.time
      self.endTime = firstPayMessage.end_time
      EventManager:GetInstance():Broadcast(EventId.UpdateFirstPayState)
      if self.status ~= prevStatus then
        self:UpdateBuildingState()
      end
    end
  end
  self:RegisterEvent(EventId.GF_monopoly_new_grid_arrived, self.OnUnlockArea)
  self:RegisterEvent(EventId.FIRST_PAY_BUILD_ADD, self.OnFirstPayBuildAdd)
end
local UpdateData = function(self, message)
  local firstPayMessage = message
  if not table.IsNullOrEmpty(firstPayMessage) then
    if firstPayMessage.is_new ~= nil then
      self.is_new = firstPayMessage.is_new == 1
    else
      local prevStatus = self.status
      self.status = firstPayMessage.status
      self.time = firstPayMessage.time
      self.endTime = firstPayMessage.end_time
      EventManager:GetInstance():Broadcast(EventId.UpdateFirstPayState)
      if self.status ~= prevStatus then
        self:UpdateBuildingState()
      end
    end
  end
end
local GetState = function(self)
  return self.status
end
local GetFixEndTime = function(self)
  return self.endTime
end
local GetRewardList = function(self)
  if table.IsNullOrEmpty(self.viewRewards) then
    local rewardList = LuaEntry.DataConfig:TryGetStr("first_pay_lw", "k5", "")
    self.viewRewards = DataCenter.RewardManager:ParseRewardsStr(rewardList)
  end
  return self.viewRewards
end
local GetExtraRewardList = function(self)
  if table.IsNullOrEmpty(self.viewExtraxRewards) then
    local rewardList = LuaEntry.DataConfig:TryGetStr("first_pay_lw", "k6", "")
    self.viewExtraxRewards = DataCenter.RewardManager:ParseRewardsStr(rewardList)
  end
  return self.viewExtraxRewards
end
local GetFirstPayPack = function(self)
  if self:IsNewFirstPay() then
    local rechargeIds = GiftPackageData.GetRechargeIdListByType(WelfareTagType.FirstCharge)
    if 0 < #rechargeIds then
      local packs = GiftPackageData.GetAllAvailablePackageByRechargeId(rechargeIds[1], false)
      if not table.IsNullOrEmpty(packs) then
        return packs[1], rechargeIds[1]
      end
    end
    return nil
  else
    local packId = LuaEntry.DataConfig:TryGetStr("first_pay_lw", "k2")
    local packData = GiftPackageData.get(tostring(packId))
    return packData
  end
end
local CheckCanShow = function(self)
  local unlocked = DataCenter.LWFunctionUnlockManager:CheckCanShow(LWFunctionUnlockType.MainUI_FirstPay)
  local canShow = false
  local isNew = self:IsNewFirstPay()
  if isNew then
    local firstChargePack = self:GetFirstPayPack()
    if firstChargePack then
      canShow = true
    end
  else
    local firstPayState = self:GetState()
    if firstPayState > FirstPayState.DontHaveBuilding and firstPayState <= FirstPayState.Repairing then
      canShow = true
    end
  end
  return unlocked and canShow
end
FirstPayManager.__init = __init
FirstPayManager.__delete = __delete
FirstPayManager.InitData = InitData
FirstPayManager.UpdateData = UpdateData
FirstPayManager.InitData = InitData
FirstPayManager.GetState = GetState
FirstPayManager.GetFixEndTime = GetFixEndTime
FirstPayManager.GetRewardList = GetRewardList
FirstPayManager.GetExtraRewardList = GetExtraRewardList
FirstPayManager.GetFirstPayPack = GetFirstPayPack
FirstPayManager.OnBuildInView = OnBuildInView
FirstPayManager.OnBuildOutView = OnBuildOutView
FirstPayManager.UpdateBuildingState = UpdateBuildingState
FirstPayManager.IsNewFirstPay = IsNewFirstPay
FirstPayManager.OnUnlockArea = OnUnlockArea
FirstPayManager.CheckCanShow = CheckCanShow
return FirstPayManager
