local this = class("modulePetBoxInfo", G_UIModuleBase)
local petLearningAblityTpl = L_GameTpl:getPetLearningAblityTpl()
local petLearningEnumTpl = L_GameTpl:getPetLearingEnumTpl()

function this.bind()
  return {
    modulePetBoxRating = {
      moduleName = "pages/pet/new/modulePetBoxRating"
    },
    modulePetBoxName = {
      moduleName = "pages/pet/new/modulePetBoxName"
    },
    modulePetNewSimpleTags = {
      moduleName = "pages/pet/new/modulePetNewSimpleTags"
    },
    modulePetFavorability = {
      moduleName = "pages/pet/new/modulePetFavorability"
    },
    modulePetBoxAttrInfo = {
      moduleName = "pages/pet/new/modulePetBoxAttrInfo"
    },
    modulePetSatiety = {
      moduleName = "pages/pet/new/modulePetSatiety"
    },
    modulePetNewSkills = {
      moduleName = "pages/pet/new/modulePetNewSkills"
    },
    modulePetGeneSkills = {
      moduleName = "pages/pet/new/modulePetNewGeneInfo"
    },
    modulePetNewHomeTag = {
      moduleName = "pages/pet/new/modulePetNewHomeTags"
    },
    modulePetMountInfo = {
      moduleName = "pages/pet/new/modulePetMountInfo"
    },
    modulePetFeather = {
      moduleName = "pages/pet/new/modulePetFeather"
    },
    modulePetRaceInfo = {
      moduleName = "pages/pet/new/modulePetRaceInfo"
    },
    modulePetCard = {
      moduleName = "pages/pet/new/modulePetCard"
    },
    toggleTabInfo = true,
    panel_skill = false,
    panel_info = false,
    detailBtnActive = true,
    mutationBtnActive = false,
    active_btnRoot = true
  }
end

function this.methods()
  return {
    onClick_detail = function(self)
      L_UI:open("pagePet", {
        petId = self.petGuid
      })
    end,
    onClick_mutation = function(self)
      L_UI:open("pagePetGeneMutation", {
        petId = self.petGuid
      })
    end,
    onClick_detail2 = function(self)
      L_UI:open("pagePet", {
        petId = self.petGuid
      })
    end,
    onClick_tabOn = function(self)
      self:showInfo(true)
    end,
    onClick_tabOff = function(self)
      self:showInfo(false)
    end,
    modulePetFeather = {
      onCLickTagTipFeather = function(self)
        self.modules.modulePetNewHomeTag:closeTip()
      end
    }
  }
end

function this:open()
  self.recoverLimit = tonumber(L_Config:getConfig("game", "PET_MAXENERGY"))
  self.recoverPoint = tonumber(L_Config:getConfig("game", "PET_ENERGYRECOVER"))
  self.scale = tonumber(L_Config:getConfig("game", "PET_ENERGYPERSEC"))
  self:showInfo(true)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPet, self.onEvent_refreshPet, self)
  L_PetStore:listenCallFunc(L_PetStore.event.setCurPetId, self.onEvent_setCurPetId, self)
  L_PetStore:listenCallFunc(L_PetStore.event.refreshPetGeneMutation, self.onEvent_refreshPetGeneMutation, self)
  self._ESyncMountRouletteServerDataHandler = handler(self, self.onEvent_mountSync)
  AzurWorld.mountMgr:RegisterEvent(C_EMountEvent.ESyncMountRouletteServerData, self._ESyncMountRouletteServerDataHandler)
end

function this:close()
  L_TimerManager:clearTimer(self)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPet, self.onEvent_refreshPet)
  L_PetStore:unListenCallFunc(L_PetStore.event.setCurPetId, self.onEvent_setCurPetId)
  L_PetStore:unListenCallFunc(L_PetStore.event.refreshPetGeneMutation, self.onEvent_refreshPetGeneMutation, self)
  AzurWorld.mountMgr:UnregisterEvent(C_EMountEvent.ESyncMountRouletteServerData, self._ESyncMountRouletteServerDataHandler)
end

function this:onEvent_refreshPet(pet)
  if pet.guid ~= self.petGuid then
    return
  end
  self:refreshPetProperty(self.petGuid)
end

function this:onEvent_setCurPetId(petGuid)
  if petGuid == self.petGuid or math.isEmpty(self.petGuid) then
    return
  end
  self:refreshPetProperty(petGuid, true)
end

function this:onEvent_refreshPetGeneMutation(petGuid)
  if petGuid ~= self.petGuid then
    return
  end
  self:refreshPetProperty(self.petGuid)
end

function this:onEvent_mountSync()
  if math.isEmpty(self.petGuid) then
    return
  end
  self:refreshPetProperty(self.petGuid)
end

function this:showInfo(bool)
  self.bind.panel_info = not bool
  self.bind.panel_skill = bool
end

function this:refreshPetProperty(petGuid, doAnim)
  if math.isEmpty(petGuid) then
    self.modules.modulePetCard:enableCard(false)
    return
  end
  self.petGuid = petGuid
  self.modules.modulePetCard:enableCard(true)
  if not doAnim then
    self.modules.modulePetCard:setGuid(petGuid)
    self:initNoAnimModule(petGuid)
  else
    self.modules.modulePetCard:setGuidWithAnim(petGuid)
    if self.parent and self.parent.bindComponents.anim then
      self.parent.bindComponents.anim:Stop()
      self.parent.bindComponents.anim:Play("anim_petbox_switch")
    end
    L_TimerManager:newOrResetTimer(self, "setPetInfo", function()
      self:initNoAnimModule(petGuid)
    end, 0.2)
  end
  local petItem = L_PetStore:getPetItem(petGuid)
  if petItem:hasMutationGene() then
    self.bind.mutationBtnActive = true
    self.bind.detailBtnActive = false
  else
    self.bind.mutationBtnActive = false
    self.bind.detailBtnActive = true
  end
end

function this:initNoAnimModule(petGuid)
  self.modules.modulePetBoxRating:setGuid(petGuid)
  self.modules.modulePetBoxName:setGuid(petGuid)
  self.modules.modulePetNewSimpleTags:setGuid(petGuid, true)
  self.modules.modulePetFavorability:setGuid(petGuid)
  self.modules.modulePetBoxAttrInfo:setGuid(petGuid)
  self.modules.modulePetSatiety:setGuid(petGuid)
  self.modules.modulePetNewSkills:setGuid(petGuid)
  self.modules.modulePetGeneSkills:setGuid(petGuid)
  self.modules.modulePetNewHomeTag:setGuid(petGuid)
  self.modules.modulePetMountInfo:setGuid(petGuid)
  self.modules.modulePetFeather:setGuid(petGuid)
  self.modules.modulePetRaceInfo:setGuid(petGuid)
end

function this:setDetailBtnActive(isShow)
  self.bind.active_btnRoot = isShow
end

return this
