local this = {}
local PrePath_mountRidingUnlock = "Program/FlyMsg/pre_mountRidingUnlock.prefab"
local _petTpl = L_GameTpl:getPetTpl()

function this:initMountRidingUnlock()
  self.mountRidingUnlockQueue = L_Struct.getQueue()
  self._mountRidingParams = {}
end

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

function this:rawShowMountRidingUnlock(uuid, popup)
  popup._uuid = uuid
  self.mountRidingUnlockQueue:enQueue(popup)
  if self.mountRidingUnlockQueue:getLength() > 1 then
    return
  end
  self:doMountRidingUnlock()
end

function this:doMountRidingUnlock()
  local popup = self.mountRidingUnlockQueue:getFirst()
  if popup == nil then
    return
  end
  local _end = function(go)
    self._mountRidingParams[go] = self._mountRidingParams[go] or {}
    local goParam = self._mountRidingParams[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(-28, 224)
    if goParam.txt_name == nil then
      goParam.txt_name = L_GameUtil.findTranByName(go, "txt_name"):GetComponent(typeof(C_LTextMeshProUGUI))
    end
    if goParam.img_icon == nil then
      goParam.img_icon = L_GameUtil.findTranByName(go, "img_icon"):GetComponent(typeof(C_LImage))
    end
    local tpl = _petTpl:getTplById(popup.petId)
    goParam.img_icon:LoadSprite(_petTpl:getPetPixelIcon(tpl))
    goParam.txt_name.text = L_WordsTpl:getValue("petsaddle_title_talent", {
      name = _petTpl:getName(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.mountRidingUnlockQueue:deQueue()
        self.pool:despawn(go)
        self:doMountRidingUnlock()
        self:sendEvent(self.event.flyMsgPopupEnd, L_ShowQueueConst.flyMsgType.mountRidingUnlock, popup._uuid)
      end)
    end, 2.5)
  end
  self.pool:asyncSpawn(PrePath_mountRidingUnlock, _end)
end

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

return this
