local this = class("modulePetFrameIcon", G_UIModuleBase)
local MaxFrame = 8
local IconPath = "UI/Icon/PetPixelIcon/tex_icon_pet_%s_%d.png"
local _petTpl = L_GameTpl:getPetTpl()

function this.bind()
  return {
    img_icon = "",
    modulePetFlashEft = {
      moduleName = "pages/pet/modulePetFlashEft"
    }
  }
end

function this.methods()
  return {}
end

function this:open()
end

function this:close()
  L_TimerManager:clearTimer(self)
end

function this:refreshView()
  if not self.isBind then
    return
  end
  L_TimerManager:clearTimer(self)
  self.bind.img_icon = ""
  self.modules.modulePetFlashEft:setModulePetFlashEftData(self.bind.guid)
  if math.isEmpty(self.bind.guid) then
    return
  end
  local pet = L_PetStore:getPetItem(self.bind.guid)
  local tpl = _petTpl:getTplById(pet.id)
  self._initFrameIcon = _petTpl:getPetPixelIcon(tpl, pet:isSpecialPet())
  self._curFrame = 1
  self._defaultPixelId = string.match(self._initFrameIcon, "%a*%d+")
  self.bind.img_icon = self._initFrameIcon
  local offSetPos = _petTpl:getPetPixelPosition(tpl, pet:isSpecialPet())
  if offSetPos == nil then
    errorf(string.format("petId: %d, _petTpl:getPetPixelPosition() == nil", pet.id))
  else
    self.bindComponents.rect_img_icon.anchoredPosition = L_Vector3.new(offSetPos[1], offSetPos[2])
  end
  if self.bind.autoPlay then
    self:playPixelGif()
  end
end

function this:setGuid(guid, autoPlay)
  self.bind.guid = guid
  self.bind.autoPlay = autoPlay
  self:refreshView()
end

function this:playPixelGif()
  L_TimerManager:newOrResetTimer(self, "playGif", function()
    self._curFrame = self._curFrame % MaxFrame + 1
    local iconPath = string.format(IconPath, self._defaultPixelId, self._curFrame)
    self.bind.img_icon = iconPath
  end, 0.1, -1)
end

return this
