local this = class("moduleBattlePetGeneInfo", G_UIModuleBase)
local _enemyTpl = L_GameTpl:getEnemyTpl()
local _petTpl = L_GameTpl:getPetTpl()

function this.bind()
  return {
    active_tip = false,
    list_tipGeneList = {
      moduleName = "pages/pet/new/cellPetTipsGene"
    },
    size_scrollGene = C_Vector2(728, 700),
    size_tipBoard = C_Vector2(750, 853),
    name = "",
    lv = "",
    show_empty = false,
    modulePetBoxRating = {
      moduleName = "pages/pet/new/modulePetBoxRating"
    }
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      self.bind.active_tip = false
    end
  }
end

function this:open()
  self.monsterEntity = nil
  self.index = 0
  self.bind.active_tip = false
  self.grade = nil
  self.geneData = nil
end

function this:show()
  if not table.isEmpty(self.petInfo) then
    self:setSelectMonster()
  end
end

function this:close()
end

function this:hide()
  self.bind.active_tip = false
end

function this:setSelectMonster()
  if table.isEmpty(self.petInfo) then
    return
  end
  self.index = C_EntityManager.GetCatchCurSelectIndex()
  self.monsterEntity = C_EntityManager.GetSelectMonsterEntities()[self.index]
  local level = self.monsterEntity.data.level
  local configId = self.monsterEntity.data.configId
  local tpl = _enemyTpl:getTplById(configId)
  if tpl then
    local petId = _enemyTpl:getPetId(tpl)
    local petTpl = _petTpl:getTplById(petId)
    if petTpl then
      self.bind.name = _petTpl:getName(petTpl)
    end
  end
  self.bind.lv = string.format("Lv.%d", level)
  print("\229\189\147\229\137\141\230\128\170\231\137\169Index:" .. self.index)
  for i, v in pairs(self.petInfo) do
    if self.monsterEntity.data.UUID == v.uuid then
      self.grade = v.grade
      self.geneData = v.geneInfos
      break
    end
  end
  if self.geneData == nil or self.grade == nil then
    return
  end
  self.parseGeneData = {}
  self.modules.modulePetBoxRating:setGrade(self.grade)
  for i, v in pairs(self.geneData) do
    table.insert(self.parseGeneData, L_PetManager:parseGeneInfo(v.geneId))
  end
  self:showTip()
end

function this:showTip()
  self.bind.active_tip = true
  self:refreshView()
  Unity.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.bindComponents.geneList)
  self.bind.size_tipBoard = C_Vector2(750, math.min(self.bindComponents.geneList.sizeDelta.y, 700) + 75)
  self.bind.size_scrollGene = C_Vector2(728, math.min(self.bindComponents.geneList.sizeDelta.y, 700))
end

function this:refreshView()
  if table.isEmpty(self.parseGeneData) then
    self.bind.show_empty = true
    return
  end
  self.bind.list_tipGeneList:clear()
  local genelist = self.parseGeneData
  table.sort(genelist, function(a, b)
    return a.rarity < b.rarity
  end)
  for k, gene in ipairs(genelist) do
    gene.index = k
  end
  self.bind.list_tipGeneList:insert_array(genelist)
  self.bind.show_empty = table.isEmpty(genelist) and true or false
end

function this:setPetData(data)
  self.petInfo = data
end

return this
