local this = {}
local m_favorabilityMsg = "Program/FlyMsg/pre_favorabilityMsg.prefab"

function this:initFavorabilityMsg()
  self.favorabilityQueue = L_Struct.getQueue()
  self._favorabilityParams = {}
end

function this:showFavorabilityMsg(str)
  self.favorabilityQueue:enQueue(str)
  if self.favorabilityQueue:getLength() > 1 then
    return
  end
  self:doFavorabilityMsg()
end

function this:doFavorabilityMsg()
  local str = self.favorabilityQueue:getFirst()
  if str == nil then
    return
  end
  local tweenSize = {
    1,
    0.9,
    0.8
  }
  local tweenPos = {
    350,
    450,
    560
  }
  local go = self.pool:spawn(m_favorabilityMsg)
  local trans = go.transform
  local tweenEase = go:GetComponent(typeof(C_LAnimationCurveHolder)).curves[0]
  self._favorabilityParams[go] = self._favorabilityParams[go] or {}
  local oneParam = self._favorabilityParams[go]
  if oneParam.customTween == nil then
    oneParam.customTween = trans:GetComponent(typeof(C_LCustomTween))
  end
  if oneParam.txt == nil then
    oneParam.txt = trans:Find("AnimRoot/textNode/txtMsg"):GetComponent(typeof(C_LTextMeshProUGUI))
  end
  if oneParam.addTxt == nil then
    oneParam.addTxt = trans:Find("AnimRoot/textNode/addMsg"):GetComponent(typeof(C_LTextMeshProUGUI))
  end
  if oneParam.moveRootTween == nil then
    oneParam.moveRootTween = self.favorabilityMoveRoot.gameObject:PickComponent(typeof(C_LCustomTween))
  end
  if oneParam.csGroup == nil then
    oneParam.csGroup = trans:GetComponent(typeof(C_CanvasGroup))
  end
  if oneParam.anim == nil then
    oneParam.anim = trans:Find("AnimRoot"):GetComponent(typeof(C_Animation))
  end
  oneParam.customTween:DoKill_NoGCAlloc()
  trans:SetParent(self.favorabilityRoot)
  L_Vector3.setLocalPos(trans, L_Vector3.getTemp(0, 350))
  L_Vector3.setSize(trans, 1)
  local showTips = string.split(str, "|")
  oneParam.txt.text = showTips[1] or ""
  oneParam.addTxt.text = showTips[2] or ""
  oneParam.playingHide = false
  L_TimerManager:newOrResetTimer(go, "doNext", function()
    self.favorabilityQueue:deQueue()
    self:doFavorabilityMsg()
  end, 0.5)
  L_TimerManager:newOrResetTimer(go, "doHide", function()
    oneParam.playingHide = true
    oneParam.anim:Play("anim_favormsg_hide")
  end, 2)
  L_TimerManager:newOrResetTimer(go, "doDespawn", function()
    self.pool:despawn(go)
  end, 2.27)
  local childCount = self.favorabilityRoot.childCount
  for i = 0, childCount - 1 do
    local child = self.favorabilityRoot:GetChild(i)
    local posIndex = childCount - i
    if tweenPos[posIndex] ~= nil then
      local childParam = self._favorabilityParams[child.gameObject]
      if childParam.moveTween ~= nil then
        childParam.moveTween:Kill()
      end
      if childParam.scaleTween ~= nil then
        childParam.scaleTween:Kill()
      end
      childParam.moveTween = child:DOLocalMoveY(tweenPos[posIndex], 0.33)
      childParam.moveTween:SetEase(tweenEase)
      local scale = L_Vector3.new(tweenSize[posIndex], tweenSize[posIndex], tweenSize[posIndex])
      childParam.scaleTween = child:DOScale(scale, 0.33)
      childParam.scaleTween:SetEase(tweenEase)
    end
  end
end

function this:disposeFavorabilityMsg()
  for i, v in pairs(self._favorabilityParams) do
    if v.moveTween then
      v.moveTween:Kill()
    end
    if v.scaleTween then
      v.scaleTween:Kill()
    end
    L_TimerManager:clearTimer(i)
    self.pool:despawn(i)
  end
  self._favorabilityParams = {}
end

return this
