local this = {}
local m_achievementPopup = "Program/FlyMsg/pre_achievementPopup.prefab"
local QUALITYTOIMG = {
  [L_Const.quality.n] = "Atlas/AchievementPage/tex_achievement_icon_chenjiuton",
  [L_Const.quality.r] = "Atlas/AchievementPage/tex_achievement_icon_chenjiuyin",
  [L_Const.quality.sr] = "Atlas/AchievementPage/tex_achievement_icon_chenjiujin"
}

function this:initAchievement()
  self.achievementQueue = L_Struct.getQueue()
  self._achievementParams = {}
end

function this:showAchievementPopup(popup)
  local priorityShowData = require("ui.manager.priorityShowQueue.flyMsgPriorityShowData").new({
    flyMsgType = L_ShowQueueConst.flyMsgType.achievement,
    uuid = self:getNextUniqueId(),
    params = {popup}
  })
  L_PriorityShowQueueManager:addPriorityShowData(priorityShowData)
end

function this:rawShowAchievementPopup(uuid, popup)
  popup._uuid = uuid
  self.achievementQueue:enQueue(popup)
  if self.achievementQueue:getLength() > 1 then
    return
  end
  self:doAchievementPopup()
end

function this:doAchievementPopup()
  local popup = self.achievementQueue:getFirst()
  if popup == nil then
    return
  end
  local achievementTpl = L_GameTpl:getAchievementTpl()
  local tpl = achievementTpl:getTplById(popup.id)
  local _end = function(go)
    self._achievementParams[go] = self._achievementParams[go] or {}
    local goParam = self._achievementParams[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.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.img_quality == nil then
      goParam.img_quality = L_CommonUtil.findTranByName("img_quality", trans):GetComponent(typeof(C_LImage))
    end
    goParam.txt_name.text = achievementTpl:getAchiName(tpl)
    goParam.txt_condition.text = achievementTpl:getAchiDesc(tpl)
    goParam.img_quality:LoadSprite(QUALITYTOIMG[achievementTpl:getAchiLevel(tpl)])
    if goParam.csGroup == nil then
      goParam.csGroup = trans:GetComponent(typeof(Unity.CanvasGroup))
    end
    goParam.csGroup.alpha = 1
    L_TimerManager:newOrResetTimer(go, "doNext", function()
      goParam.customTween:DOFade_NoGCAlloc(goParam.csGroup, 0, 0.5, function()
        self.achievementQueue:deQueue()
        self.pool:despawn(go)
      end)
      self.achievementQueue:deQueue()
      self:doAchievementPopup()
      self:sendEvent(self.event.flyMsgPopupEnd, L_ShowQueueConst.flyMsgType.achievement, popup._uuid)
    end, 2.5)
  end
  self.pool:asyncSpawn(m_achievementPopup, _end)
end

function this:disposeAchievement()
  for i, v in pairs(self._achievementParams) 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._achievementParams = {}
end

return this
