local this = {}
local m_activityTaskPopup = "Program/FlyMsg/pre_activityTaskPopup.prefab"
local m_poseInvitePopup = "Program/FlyMsg/pre_poseInvitePopup.prefab"

function this:initActivity()
  self.activityTaskQueue = L_Struct.getQueue()
  self.actionInviteQueue = L_Struct.getQueue()
  self._activityParams = {}
end

function this:showActivityTaskPopup(popup)
  local result = L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.activation)
  if not result then
    return
  end
  local priorityShowData = require("ui.manager.priorityShowQueue.flyMsgPriorityShowData").new({
    flyMsgType = L_ShowQueueConst.flyMsgType.activity,
    uuid = self:getNextUniqueId(),
    params = {popup}
  })
  L_PriorityShowQueueManager:addPriorityShowData(priorityShowData)
end

function this:rawShowActivityTaskPopup(uuid, popup)
  popup._uuid = uuid
  self.activityTaskQueue:enQueue(popup)
  if self.activityTaskQueue:getLength() > 1 then
    return
  end
  self:doActivityTaskPopup()
end

function this:doActivityTaskPopup()
  local popup = self.activityTaskQueue:getFirst()
  if popup == nil then
    return
  end
  local taskDescribe = popup.describe
  local taskActivityReward = popup.reward
  local _end = function(go)
    self._activityParams[go] = self._activityParams[go] or {}
    local goParam = self._activityParams[go]
    local trans = go.transform
    if goParam.rectTrans == nil then
      goParam.rectTrans = trans:GetComponent(typeof(C_RectTransform))
    end
    if goParam.customTween == nil then
      goParam.customTween = trans:GetComponent(typeof(C_LCustomTween))
    end
    goParam.customTween:DoKill_NoGCAlloc()
    trans:Reset(self.normalRoot)
    trans.localScale = Unity.Vector3.one
    goParam.rectTrans.anchoredPosition = Unity.Vector2(0, -161)
    if goParam.starList == nil then
      goParam.starList = trans:Find("txt_name"):Find("starList")
    end
    if popup.maxStage > 1 then
      for i = 1, 5 do
        if goParam["star_" .. i] == nil then
          goParam["star_" .. i] = goParam.starList:Find("star_" .. i).gameObject
        end
        local star = goParam["star_" .. i]
        star:ActiveTrans(false)
      end
      for i = 1, popup.maxStage do
        if goParam["star_" .. i] == nil then
          goParam["star_" .. i] = goParam.starList:Find("star_" .. i).gameObject
        end
        local star = goParam["star_" .. i]
        star:ActiveTrans(true)
        local icon = goParam.starList:Find("star_" .. i):GetComponent(typeof(C_LImage))
        if i < popup.curStage then
          icon:LoadSprite("Page/BSActivityTask/tex_activity_icon_hy_1")
        else
          icon:LoadSprite("Page/BSActivityTask/tex_activity_icon_hy_2")
        end
      end
    else
      for i = 1, 5 do
        if goParam["star_" .. i] == nil then
          goParam["star_" .. i] = goParam.starList:Find("star_" .. i).gameObject
        end
        local star = goParam["star_" .. i]
        star:ActiveTrans(false)
      end
    end
    if goParam.txt_name == nil then
      goParam.txt_name = trans:Find("txt_name"):GetComponent(typeof(C_LTextMeshProUGUI))
    end
    if goParam.txt_condition == nil then
      goParam.txt_condition = trans:Find("txt_condition"):GetComponent(typeof(C_LTextMeshProUGUI))
    end
    if goParam.txt_reward == nil then
      goParam.txt_reward = trans:Find("txt_reward"):GetComponent(typeof(C_LTextMeshProUGUI))
    end
    goParam.txt_name.text = taskDescribe
    goParam.txt_condition.text = string.format("<size=45><color=#111A22>%s/</color></size>", tostring(popup.count))
    for i = 1, popup.maxStage do
      if i < popup.curStage then
        if i >= popup.maxStage then
          goParam.txt_condition.text = goParam.txt_condition.text .. string.format("<color=#1ab7fc>%d</color>", popup.targetCount)
        else
          goParam.txt_condition.text = goParam.txt_condition.text .. string.format("<color=#1ab7fc>%d-</color>", popup.targetCount)
        end
      elseif i >= popup.maxStage then
        goParam.txt_condition.text = goParam.txt_condition.text .. string.format("%d", popup.targetCount)
      else
        goParam.txt_condition.text = goParam.txt_condition.text .. string.format("%d-", popup.targetCount)
      end
    end
    goParam.txt_reward.text = taskActivityReward
    if goParam.csGroup == nil then
      goParam.csGroup = trans:GetComponent(typeof(Unity.CanvasGroup))
    end
    goParam.csGroup.alpha = 1
    C_AudioManager.Play("Play_SFX_System_HUD_Daily_Active")
    L_TimerManager:newOrResetTimer(go, "doNext", function()
      goParam.customTween:DOFade_NoGCAlloc(goParam.csGroup, 0, 0.5, function()
        self.activityTaskQueue:deQueue()
        self.pool:despawn(go)
      end)
      self.activityTaskQueue:deQueue()
      self:doActivityTaskPopup()
      self:sendEvent(self.event.flyMsgPopupEnd, L_ShowQueueConst.flyMsgType.activity, popup._uuid)
    end, 2.5)
  end
  self.pool:asyncSpawn(m_activityTaskPopup, _end)
end

function this:showPoseInvite(popup)
  self.actionInviteQueue:enQueue(popup)
  if self.actionInviteQueue:getLength() > 1 then
    return
  end
  self:doActionInvitePopup()
end

function this:doActionInvitePopup()
end

function this:disposeActivity()
  for i, v in pairs(self._activityParams) do
    if L_CommonUtil.isValid(v.customTween) then
      v.customTween:DoKill_NoGCAlloc()
      v.customTween = nil
    end
    L_TimerManager:clearTimer(i)
    self.pool:despawn(i)
  end
  self._activityParams = {}
end

return this
