local this = class("modulePetGeneMutation", G_UIModuleBase)

function this:bind()
  return {
    txt_name = "",
    module_pixelIcon = {
      moduleName = "pages/pet/modulePetPixelIcon"
    },
    toggle_isGeneMutation = false
  }
end

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

function this:initModule(info)
  if not info then
    return
  end
  self.bindComponents.anim:Stop()
  self.bindComponents.anim:Play("anim_petget_tips_show")
  self.info = info
  self:initPage()
  self:showCutKey()
  self.bindComponents.canvasGroup.blocksRaycasts = true
end

function this:showCutKey()
  L_UI:addListener(L_UI.pageEvent.showed, self.onEvent_showed, self)
  if not L_DeviceTpl:getIsPc() then
    return
  end
  L_ShortCutManager:addListener(L_ShortCutConst.event.PetGet, self.shortKeyClick, self)
end

function this:shortKeyClick()
  local curPageName = L_UI:currentPage(L_UI.groupKeys.stack)
  if curPageName ~= "pageMainCity" then
    return
  end
  self.bindComponents.anim:Stop()
  self.bindComponents.anim:Play("anim_petget_tips_hide")
  L_UI:open("pagePetBox", {
    defaultPetGuid = self.info.data.guid
  })
  self:doClose()
end

function this:onEvent_showed(pageName)
  if pageName == "pageMainCity" then
    return
  end
  self.bindComponents.anim:Stop()
  self.bindComponents.anim:Play("anim_petget_tips_hide")
  self:doClose()
end

function this:close()
  L_TimerManager:clearTimer(self)
  if L_DeviceTpl:getIsPc() then
    L_ShortCutManager:removeListener(L_ShortCutConst.event.PetGet, self.shortKeyClick, self)
  end
  L_UI:removeListener(L_UI.pageEvent.showed, self.onEvent_showed, self)
  if self.closeCB then
    self.closeCB(self.info)
  end
  self.bindComponents.canvasGroup.blocksRaycasts = false
end

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

function this:initPage()
  local petItem = L_PetStore:getPetItem(self.info.data.guid)
  self.bind.txt_name = string.format("Lv.%d %s", petItem:petLv(), petItem:getPetName())
  self.modules.module_pixelIcon:setGuid(self.info.data.guid)
  self.bind.toggle_isGeneMutation = not self.info.data.isNew
  self:openTickTimer()
end

function this:openTickTimer()
  self.initTime = 6
  local func = function()
    self.initTime = self.initTime - 1
    if self.initTime <= 0 then
      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
