local this = {}
local m_normMsg = "Program/FlyMsg/pre_normalMsg.prefab"

function this:initNormalMsg()
  self.normalQueue = L_Struct.getQueue()
  self._normalMsgParams = {}
  self._lengthParam = {}
  self.normalMoveRoot = self.normalRoot:Find("normalMoveRoot"):GetComponent(typeof(C_RectTransform))
  self.normalMoveTween = self.normalMoveRoot.gameObject:PickComponent(typeof(C_LCustomTween))
end

function this:showNormalMsgByKey(str, param)
  local msg = L_WordsTpl:getValue(str, param)
  self:showNormalMsg(msg)
end

function this:showNormalMsg(str, overrideLen)
  if not self.normalQueue then
    return
  end
  local curStr = self.normalQueue:getFirst()
  if curStr == str then
    return
  end
  self.normalQueue:enQueue(str)
  if not self._lengthParam[str] then
    self._lengthParam[str] = {}
  end
  if overrideLen and type(overrideLen) == "number" and 0 < overrideLen then
    table.insert(self._lengthParam[str], overrideLen)
  else
    table.insert(self._lengthParam[str], 1)
  end
  if 1 < self.normalQueue:getLength() then
    return
  end
  self:doNormalMsg()
end

function this:doNormalMsg()
  local str = self.normalQueue:getFirst()
  if str == nil then
    return
  end
  if not self.normalMoveRoot then
    return
  end
  local go = self.pool:spawn(m_normMsg)
  local animation = go:GetComponent(typeof(C_Animation))
  local trans = go.transform
  self._normalMsgParams[go] = self._normalMsgParams[go] or {}
  local oneParam = self._normalMsgParams[go]
  if oneParam.customTween == nil then
    oneParam.customTween = trans:GetComponent(typeof(C_LCustomTween))
  end
  if oneParam.txt == nil then
    oneParam.txt = trans:Find("txtMsg"):GetComponent(typeof(C_LRichText))
  end
  if oneParam.csGroup == nil then
    oneParam.csGroup = trans:GetComponent(typeof(C_CanvasGroup))
  end
  local childCount = self.normalMoveRoot.childCount
  if 0 < childCount then
    local child = self.normalMoveRoot:GetChild(0)
    child.gameObject:SetActive(false)
    self.pool:despawn(child)
  end
  self:removeTimers(oneParam)
  oneParam.timers = {}
  oneParam.txt.richText = str
  trans:SetParent(self.normalMoveRoot)
  L_Vector3.setLocalPos(trans, L_Vector3.getTemp(0, 100))
  L_Vector3.setSize(trans, 1)
  animation:Play("anim_msg_show")
  L_AudioUtil.playSESound("notification")
  local duration = 1
  if self._lengthParam[str] and 0 < #self._lengthParam[str] then
    duration = self._lengthParam[str][1]
    table.remove(self._lengthParam[str], 1)
  end
  table.insert(oneParam.timers, Timer.once(duration, function()
    self.normalQueue:deQueue()
    self:doNormalMsg()
    animation:Play("anim_msg_hide")
  end, self, go))
  table.insert(oneParam.timers, Timer.once(duration + 0.5, function()
    self.pool:despawn(go)
  end, self, go))
end

function this:removeTimers(oneParam)
  if oneParam and oneParam.timers ~= nil then
    for _, timer in pairs(oneParam.timers) do
      timer:destroy()
    end
  end
end

function this:disposeNormalMsg()
  for i, v in pairs(self._normalMsgParams or L_Const.emptyTable) do
    self:removeTimers(v)
    self.pool:despawn(i)
  end
  self._normalMsgParams = {}
  self.normalMoveRoot = nil
  self.normalMoveTween = nil
end

return this
