local this = {}
local m_petEatFinishMsg = "Program/FlyMsg/pre_petEatFinishMsg.prefab"
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local ColorConfig = {
  [1] = C_LuaUtility.ParseHtmlStringColor("#3AB3FF"),
  [2] = C_LuaUtility.ParseHtmlStringColor("#A264FF"),
  [3] = C_LuaUtility.ParseHtmlStringColor("#EC9900"),
  [4] = C_LuaUtility.ParseHtmlStringColor("#4F525D"),
  [5] = C_LuaUtility.ParseHtmlStringColor("#27AE89")
}
local COLOR_GREY = C_LuaUtility.ParseHtmlStringColor("#4F525D")

function this:initPetEatFinishMsg()
  self.petEatFinishMsgQueue = L_Struct.getQueue()
  self._petEatFinishMsgParams = {}
  self.petEatFinishMoveY = 0
  self.petEatFinishMoveRoot = self.petEatFinishRoot:Find("rectArea/petEatFinishMoveRoot"):GetComponent(typeof(C_RectTransform))
  self.petEatFinishMoveTween = self.petEatFinishMoveRoot.gameObject:PickComponent(typeof(C_LCustomTween))
  self.curUid = -1
  self.lastUid = -1
end

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

function this:rawShowPetEatFinishPopup(uuid, popup)
  popup._uuid = uuid
  self.petEatFinishMsgQueue:enQueue(popup)
  if self.petEatFinishMsgQueue:getLength() > 1 then
    return
  end
  self:doPetEatFinishMsg()
end

function this:showPetEatFinishMsg(petGuid, foodId)
  local data = {petGuid = petGuid, foodId = foodId}
  self:showPetEatFinishPop(data)
end

function this:doPetEatFinishMsg()
  local msgData = self.petEatFinishMsgQueue:getFirst()
  if msgData == nil then
    return
  end
  local go = self.pool:spawn(m_petEatFinishMsg)
  go.transform:Reset(self.petEatFinishRoot, true)
  local oneParam = self:getPetEatFinishOneParam(go)
  self:fillPetEatFinishData(oneParam, msgData)
  self:fillPetEatFinishTween(oneParam, msgData)
end

function this:doNextPetEatFinishMsg()
  self.petEatFinishMsgQueue:deQueue()
  self:doPetEatFinishMsg()
end

function this:getPetEatFinishOneParam(go)
  local oneParam = self._petEatFinishMsgParams[go]
  if oneParam == nil then
    local animRoot = go.transform:Find("AnimRoot")
    oneParam = {
      go = go,
      img_food = animRoot:Find("img_food"):GetComponent(typeof(C_LImage)),
      text_food = animRoot:Find("txtMsg"):Find("text_food"):GetComponent(typeof(C_LTextMeshProUGUI)),
      text_desc = animRoot:Find("txtMsg"):Find("text_desc"):GetComponent(typeof(C_LTextMeshProUGUI)),
      anim = go:GetComponent(typeof(C_Animation))
    }
    self._petEatFinishMsgParams[go] = oneParam
  end
  return oneParam
end

function this:fillPetEatFinishData(oneParam, msgData)
  local petGuid = msgData.petGuid
  local foodId = msgData.foodId
  local commonItemTpl = _commonItemTpl:getTplById(foodId)
  local foodName = _commonItemTpl:getName(commonItemTpl)
  local foodIcon = _commonItemTpl:getIcon(commonItemTpl)
  local foodRarity = _commonItemTpl:getRarity(commonItemTpl)
  local petItem = L_PetStore:getPetItem(petGuid)
  local petName = petItem:getPetName()
  oneParam.img_food:LoadSprite(foodIcon)
  oneParam.text_food.text = foodName
  oneParam.text_food.color = ColorConfig[foodRarity + 1]
  oneParam.text_desc.text = L_WordsTpl:getValue("notice_home_pet_eat", {
    [0] = petName
  })
  oneParam.text_desc.color = COLOR_GREY
end

local TOP_IN = -35
local OFFSET_Y = -45

function this:fillPetEatFinishTween(oneParam, msgData)
  L_Vector3.setAnchored(oneParam.go.transform, L_Vector3.getTemp(0, TOP_IN, 0))
  if self.lastPetEatFinish then
    local trans = self.lastPetEatFinish.go.transform
    trans:SetParent(self.petEatFinishMoveRoot)
    L_Vector3.setAnchored(trans, L_Vector3.getTemp(0, TOP_IN - self.petEatFinishMoveY, 0))
    self.petEatFinishMoveY = self.petEatFinishMoveY + OFFSET_Y
    self.petEatFinishMoveTween:DoKill_NoGCAlloc()
    self.petEatFinishMoveTween:DOLocalMoveY_NoGCAlloc(self.petEatFinishMoveY, 0.333, nil, Tweening.Ease.OutCubic)
  else
    self.petEatFinishMoveY = 0
    L_Vector3.setAnchored(self.petEatFinishMoveRoot, L_Vector3.getTemp(0, self.petEatFinishMoveY, 0))
  end
  self.lastPetEatFinish = oneParam
  L_TimerManager:newOrResetTimer(oneParam.go, "doPetEatFinishNext", function()
    self:doNextPetEatFinishMsg()
    self:sendEvent(self.event.flyMsgPopupEnd, L_ShowQueueConst.flyMsgType.petEatFinishMsg, msgData._uuid)
  end, 0.165)
  L_TimerManager:newOrResetTimer(oneParam.go, "doPetEatFinishHide", function()
    oneParam.anim:Play("anim_rewardmsg_hide")
    L_TimerManager:newOrResetTimer(oneParam.go, "doPetEatFinishDespawn", function()
      self.pool:despawn(oneParam.go)
      if self.lastPetEatFinish == oneParam then
        self.lastPetEatFinish = nil
      end
    end, 0.28)
  end, 1.5)
end

function this:disposePetEatFinishMsg()
  for i, v in pairs(self._petEatFinishMsgParams) do
    L_TimerManager:clearTimer(i)
    self.pool:despawn(i)
  end
  self._petEatFinishMsgParams = {}
  self.lastPetEatFinish = nil
  self.petEatFinishMoveRoot = nil
  self.petEatFinishMoveTween = nil
end

return this
