local this = {}
local playerColor = C_LuaUtility.ParseHtmlStringColor(L_Const.colorHtml.blue010)
local enemyColor = C_LuaUtility.ParseHtmlStringColor(L_Const.colorHtml.red009)
local resultMsg = "Program/FlyMsg/pre_petlDuelKillResult.prefab"
local petTpl = L_GameTpl:getPetTpl()

function this:initPetDuelResultMsg()
  self.petDuelResultQueue = L_Struct.getQueue()
  self._petDuelResultParams = {}
end

function this:showPetDuelResultMsg(data)
  self.petDuelResultQueue:enQueue(data)
  self:doPetDuelResultMsg()
end

function this:doPetDuelResultMsg()
  if self.petDuelResultQueue:getLength() == 0 then
    return
  end
  local data = self.petDuelResultQueue:deQueue()
  if data == nil then
    return
  end
  local go = self.pool:spawn(resultMsg)
  local trans = go.transform
  self._petDuelResultParams[go] = self._petDuelResultParams[go] or {}
  local oneParam = self._petDuelResultParams[go]
  if oneParam.anim == nil then
    oneParam.anim = trans:GetComponent(typeof(C_Animation))
  end
  if oneParam.winPetName == nil then
    oneParam.winPetName = trans:Find("animroot/content/bg/txt_winPetName"):GetComponent(typeof(C_LTextMeshProUGUI))
  end
  if oneParam.losePetName == nil then
    oneParam.losePetName = trans:Find("animroot/content/bg/txt_losePetName"):GetComponent(typeof(C_LTextMeshProUGUI))
  end
  self:setResultInfo(oneParam, data)
  trans:SetParent(self.petDuelResultRoot)
  L_Vector3.setLocalPos(trans, L_Vector3.getTemp(0, 0, 0))
  L_Vector3.setSize(trans, 1)
  self:playShowAnimation(oneParam, go, data.isWin)
  local childCount = self.petDuelResultRoot.childCount
  for i = 0, childCount - 1 do
    local child = self.petDuelResultRoot:GetChild(i)
    local childParam = self._petDuelResultParams[child.gameObject]
    if childParam.moveTween ~= nil then
      childParam.moveTween:Kill()
    end
    childParam.moveTween = child:DOLocalMoveY((childCount - i) * 50, 0.33)
    childParam.moveTween:SetEase(Tweening.Ease.OutQuart)
  end
end

function this:playShowAnimation(params, go, isWin)
  if isWin then
    params.anim:Play("anim_petbattle_killtip_B")
  else
    params.anim:Play("anim_petbattle_killtip_R")
  end
  L_TimerManager:newOrResetTimer(go, "doHide", function()
    params.anim:Play("anim_petbattle_killtip_out")
  end, 2.5)
  L_TimerManager:newOrResetTimer(go, "doDespawn", function()
    self.pool:despawn(go)
  end, 2.83)
end

function this:setResultInfo(params, data)
  local enemyTpl = petTpl:getTplById(data.enemy.petConfigId)
  local slotId = L_KiBoDuelManager:getSlotIdByGuid(data.player.guid)
  if data.isWin then
    params.winPetName.text = L_KiBoDuelManager:getPetNameBySlot(slotId)
    params.losePetName.text = petTpl:getName(enemyTpl, false)
    params.winPetName.color = playerColor
    params.losePetName.color = enemyColor
  else
    params.losePetName.text = L_KiBoDuelManager:getPetNameBySlot(slotId)
    params.winPetName.text = petTpl:getName(enemyTpl, false)
    params.winPetName.color = enemyColor
    params.losePetName.color = playerColor
  end
end

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

return this
