local this = {}
local _exploreTpl = L_GameTpl:getExploreTpl()
local _worldCityTpl = L_GameTpl:getWorldCityTpl()
local PrePath_exploreComplete = "Program/FlyMsg/pre_homeContentUnlock.prefab"

function this:initMapExploreComplete()
  self._mapExploreCompleteQueue = L_Struct.getQueue()
  self._mapExploreCompleteParams = {}
end

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

function this:rawShowMapExploreComplete(uuid, popup)
  popup._uuid = uuid
  self._mapExploreCompleteQueue:enQueue(popup)
  if self._mapExploreCompleteQueue:getLength() > 1 then
    return
  end
  self:doMapExploreComplete()
end

function this:doMapExploreComplete()
  local popup = self._mapExploreCompleteQueue:getFirst()
  if popup == nil then
    return
  end
  local _end = function(go)
    self._mapExploreCompleteParams[go] = self._mapExploreCompleteParams[go] or {}
    local goParam = self._mapExploreCompleteParams[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
    if goParam.img_icon == nil then
      goParam.img_icon = L_GameUtil.findTranByName(go, "img_icon"):GetComponent(typeof(C_LImage))
      goParam.img_icon:LoadSprite("UI/Atlas/Explore/tex_explore_icon_rk01.png")
    end
    goParam.customTween:DoKill_NoGCAlloc()
    trans:Reset(self.normalRoot)
    trans.localScale = Unity.Vector3.one
    goParam.rectTrans.anchoredPosition = Unity.Vector2(0, -67)
    if goParam.txt_name == nil then
      goParam.txt_name = L_GameUtil.findTranByName(go, "txt_name"):GetComponent(typeof(C_LTextMeshProUGUI))
      goParam.txt_name_white = L_GameUtil.findTranByName(go, "txt_name_white"):GetComponent(typeof(C_LTextMeshProUGUI))
    end
    local exploration_id = popup.exploration_id
    local tpl = _exploreTpl:getTplById(exploration_id)
    if tpl then
      local cityTpl = _worldCityTpl:getTplById(_exploreTpl:getRegionId(tpl))
      goParam.txt_name.text = L_WordsTpl:getValue("explore_complete_remind", {
        [0] = _worldCityTpl:getCity(cityTpl),
        [1] = _exploreTpl:getExploreName(tpl)
      })
      goParam.txt_name_white.text = goParam.txt_name.text
    end
    if goParam.ani == nil then
      goParam.ani = trans:GetComponent(typeof(Unity.Animation))
    end
    L_TimerManager:newOrResetTimer(go, "doNext", function()
      goParam.ani:Stop()
      goParam.ani:Play("anim_home_tips_hide")
      L_TimerManager:newOrResetTimer(go, "doHide", function()
        self._mapExploreCompleteQueue:deQueue()
        self.pool:despawn(go)
        self:doMapExploreComplete()
        self:sendEvent(self.event.flyMsgPopupEnd, L_ShowQueueConst.flyMsgType.exploreComplete, popup._uuid)
      end, 0.167)
    end, 2)
  end
  self.pool:asyncSpawn(PrePath_exploreComplete, _end)
end

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

return this
