local this = class("moduleGetPet", G_UIModuleBase)
local petGradeTpl = L_GameTpl:getPetGradeTpl()
local GifPath = "UI/Texture/PetPixelIcon/tex_icon_pet_%s_%d.png"

function this:bind()
  return {
    txt_name = "",
    grade = "",
    icon_grade_rank = "",
    tips = "",
    module_pixelIcon = {
      moduleName = "pages/pet/modulePetPixelIcon"
    },
    img_icon = "",
    img_icon_effect = "",
    img_base = ""
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      self:shortKeyClick()
    end
  }
end

function this:open(options)
  self.super.open(self, options)
  if not options then
    return
  end
  self.gameObject:SetActive(true)
  self.bindComponents.txt_rayCast.raycastTarget = true
  self.bindComponents.anim:Stop()
  self.bindComponents.anim:Play("anim_petget_tips_show")
  L_AudioUtil.playSound("Play_SFX_System_HUD_General_Qibo_Obtain_Left")
  self.options = options
  if options.pet then
    self.pet = options.pet
  end
  self:initPage()
  self:showCutKey()
end

function this:showCutKey()
  if not L_DeviceTpl:getIsPc() then
    return
  end
  L_ShortCutManager:addListener(L_ShortCutConst.event.PetGet, self.shortKeyClick, self)
  L_ShortCutManager:addShortKeyFlag(self, "I", L_Const.AnchorType.Middle, self.bindComponents.ShortKey, L_Vector3.new(-211, 0))
end

function this:shortKeyClick()
  self.bindComponents.anim:Stop()
  self.bindComponents.anim:Play("anim_petget_tips_hide")
  L_PetManager:getTempRenderTexture(function(tempTex)
    L_UI:open("pageGetPet", {
      pet = self.pet,
      showType = L_Const.getPetShowType.short,
      texture = tempTex
    })
    self:doClose()
  end)
end

function this:close()
  self.bindComponents.txt_rayCast.raycastTarget = false
  L_TimerManager:clearTimer(self)
  if L_DeviceTpl:getIsPc() then
    L_ShortCutManager:removeListener(L_ShortCutConst.event.PetGet, self.shortKeyClick, self)
    L_ShortCutManager:clearShortKeyFlag(self)
  end
  if self.closeCB then
    self.closeCB(self.options)
  end
end

function this:setCloseCB(closeCB)
  self.closeCB = closeCB
end

function this:initPage()
  if table.isEmpty(self.pet) then
    return
  end
  local property = L_PetStore:getPetOriginalProperty(self.pet)
  self.property = property
  local gradeTpl = petGradeTpl:getTplById(property.gradeRank)
  local petTpl = L_GameTpl:getPetTpl()
  self.bind.icon_grade_rank = petGradeTpl:getGradeScore(gradeTpl)
  self.bind.grade = tostring(property.grade)
  self.bind.txt_name = string.format("Lv.%d %s", self.pet.lv, self.pet.pet_name)
  local length = self.bindComponents.txt_petName.preferredWidth
  local size_x = math.clamp(length - 104 + 470, 470, 570)
  self.bindComponents.img_bgTrans.sizeDelta = L_Vector3.new(size_x, 60)
  self.modules.module_pixelIcon:setGuid(self.pet.guid)
  self.modules.module_pixelIcon:outLineShow(false)
  local tpl = petTpl:getTplById(self.pet.id)
  self.bind.img_icon = petTpl:getPetPixelIcon(tpl, self.pet:isSpecialPet())
  self.bind.img_base = petGradeTpl:getPetPixelBase(gradeTpl)
  self._initFrameIcon = self.bind.img_icon
  self._curFrame = 1
  self._defaultPixelId = string.match(self._initFrameIcon, "%a*%d+")
  local iconPath = string.format(GifPath, self._defaultPixelId, self._curFrame)
  self.bind.img_icon_effect = iconPath
  self:openTickTimer()
end

function this:openTickTimer()
  self.initTime = 6
  local func = function()
    self.initTime = self.initTime - 1
    if self.initTime > 0 then
      self.bind.tips = L_WordsTpl:getValue("ui_moduleGetPet", {
        [0] = self.initTime
      })
    else
      self.bind.tips = ""
      self.bindComponents.anim:Stop()
      self.bindComponents.anim:Play("anim_petget_tips_hide")
      self:doClose()
    end
  end
  L_TimerManager:newOrResetTimer(self, "timeTick", func, 1, -1)
  func()
end

function this:doClose()
  self:close()
end

return this
