local this = {}
local m_rewardMsg = "Program/FlyMsg/pre_rewardMsg.prefab"
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this:initRewardMsg()
  self.rewardMsgQueue = L_Struct.getQueue()
  self._rewardMsgParams = {}
  self.rewardMoveY = 0
  self.goRewardTip = self.rewardRoot:Find("imgRewardTip").gameObject
  self.rewardMoveRoot = self.rewardRoot:Find("rectArea/rewardMoveRoot"):GetComponent(typeof(C_RectTransform))
  self.rewardMoveTween = self.rewardMoveRoot.gameObject:PickComponent(typeof(C_LCustomTween))
  self.curUid = -1
  self.lastUid = -1
end

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

function this:rawShowRewardPopup(uuid, popup)
  popup._uuid = uuid
  self.rewardMsgQueue:enQueue(popup)
  if self.rewardMsgQueue:getLength() > 1 then
    return
  end
  self:doRewardMsg()
end

function this:showItemServerRewards(sData, ignoreQueueCheck)
  for _, v in ipairs(sData) do
    if v.itemnum > 0 then
      self:showCommonRewardMsg(v.itemtype, v.itemid, v.itemnum, nil, true, ignoreQueueCheck)
    end
  end
end

function this:showRewardMsg(icon, name, quality, count, iconFilter, ignoreQueueCheck)
  local data = {
    icon = icon,
    name = name,
    count = count,
    quality = quality or L_Const.quality.n,
    iconFilter = iconFilter or ""
  }
  if ignoreQueueCheck then
    self:doRewardMsgByRewardData(data)
  else
    self:showRewardPopup(data)
  end
end

function this:showCommonRewardMsg(itemType, itemId, itemNum, itemName, isPlayAudio, ignoreQueueCheck)
  local parse = L_ItemTplManager:getItemConfig(itemType, itemId)
  if table.isEmpty(parse) then
    return
  end
  local iconFilter
  if itemType == L_Const.resType.commonItem then
    iconFilter = _commonItemTpl:getFilterIcon(_commonItemTpl:getTplById(itemId))
  end
  itemName = string.isEmpty(itemName) and parse.name or itemName
  self:showRewardMsg(parse.icon, itemName, parse.quality, itemNum, iconFilter, ignoreQueueCheck)
  if isPlayAudio then
    L_PlayerManager:playVoice(L_Const.cvType.FoundRes)
  end
end

function this:doRewardMsgByRewardData(rewardData)
  if rewardData == nil then
    return
  end
  self.goRewardTip:ActiveTrans(true)
  local go = self.pool:spawn(m_rewardMsg)
  go.transform:Reset(self.rewardRoot, true)
  local oneParam = self:getRwardOneParam(go)
  self:fillRewardData(oneParam, rewardData)
  self:fillRewardTween(oneParam, rewardData)
end

function this:doRewardMsg()
  local rewardData = self.rewardMsgQueue:getFirst()
  if rewardData == nil then
    return
  end
  self.goRewardTip:ActiveTrans(true)
  local go = self.pool:spawn(m_rewardMsg)
  go.transform:Reset(self.rewardRoot, true)
  local oneParam = self:getRwardOneParam(go)
  self:fillRewardData(oneParam, rewardData)
  self:fillRewardTween(oneParam, rewardData)
end

function this:doNextRewardMsg()
  self.rewardMsgQueue:deQueue()
  self:doRewardMsg()
end

function this:getRwardOneParam(go)
  local oneParam = self._rewardMsgParams[go]
  if oneParam == nil then
    local animRoot = go.transform:Find("AnimRoot")
    oneParam = {
      go = go,
      txtDescribe = animRoot:Find("txtMsg"):GetComponent(typeof(C_LRichText)),
      txtCount = animRoot:Find("txtMsg/txtCount"):GetComponent(typeof(C_LRichText)),
      csGroup = animRoot:GetComponent(typeof(C_CanvasGroup)),
      imgRes = animRoot:Find("imgRes"):GetComponent(typeof(C_LImage)),
      imgIconFilter = animRoot:Find("imgRes/imgIconFilter"):GetComponent(typeof(C_LImage)),
      imgQuality = animRoot:Find("imgQuality"):GetComponent(typeof(C_LImage)),
      rootTween = go:GetComponent(typeof(C_LCustomTween)),
      anim = go:GetComponent(typeof(C_Animation))
    }
    self._rewardMsgParams[go] = oneParam
  end
  return oneParam
end

function this:fillRewardData(oneParam, rewardData)
  local tpl_rarity = L_RarityTpl:getTplById(rewardData.quality)
  oneParam.txtDescribe:LoadColorStyle(L_RarityTpl:getColor(tpl_rarity))
  oneParam.txtCount:LoadColorStyle(L_RarityTpl:getColor(tpl_rarity))
  local img_quality = L_RarityTpl:getIconByType(rewardData.quality, L_Const.qualityPathType.dropReward)
  oneParam.imgQuality:LoadSprite(img_quality)
  oneParam.imgRes:LoadSprite(rewardData.icon)
  oneParam.imgIconFilter:LoadSprite(rewardData.iconFilter or "")
  oneParam.txtDescribe.richText = rewardData.name
  if rewardData.count then
    oneParam.txtCount.richText = string.format(" x %s", rewardData.count)
  else
    oneParam.txtCount.richText = ""
  end
end

local TOP_IN = -35
local OFFSET_Y = -45

function this:fillRewardTween(oneParam, rewardData)
  L_Vector3.setAnchored(oneParam.go.transform, L_Vector3.getTemp(0, TOP_IN, 0))
  if self.lastReward then
    local trans = self.lastReward.go.transform
    trans:SetParent(self.rewardMoveRoot)
    L_Vector3.setAnchored(trans, L_Vector3.getTemp(0, TOP_IN - self.rewardMoveY, 0))
    self.rewardMoveY = self.rewardMoveY + OFFSET_Y
    self.rewardMoveTween:DoKill_NoGCAlloc()
    self.rewardMoveTween:DOLocalMoveY_NoGCAlloc(self.rewardMoveY, 0.333, nil, Tweening.Ease.OutCubic)
  else
    self.rewardMoveY = 0
    L_Vector3.setAnchored(self.rewardMoveRoot, L_Vector3.getTemp(0, self.rewardMoveY, 0))
  end
  self.lastReward = oneParam
  L_TimerManager:newOrResetTimer(oneParam.go, "doNext", function()
    self:doNextRewardMsg()
    self:sendEvent(self.event.flyMsgPopupEnd, L_ShowQueueConst.flyMsgType.rewardMsg, rewardData._uuid)
  end, 0.165)
  L_TimerManager:newOrResetTimer(oneParam.go, "doHide", function()
    oneParam.anim:Play("anim_rewardmsg_hide")
    L_TimerManager:newOrResetTimer(oneParam.go, "doDespawn", function()
      self.pool:despawn(oneParam.go)
      if self.lastReward == oneParam then
        self.lastReward = nil
        self.goRewardTip:ActiveTrans(false)
      end
    end, 0.28)
  end, 1.5)
end

function this:disposeRewardMsg()
  for i, v in pairs(self._rewardMsgParams) do
    L_TimerManager:clearTimer(i)
    self.pool:despawn(i)
  end
  self._rewardMsgParams = {}
  self.lastReward = nil
  self.goRewardTip = nil
  self.rewardMoveRoot = nil
  self.rewardMoveTween = nil
end

return this
