local this = class("modulePetNewGeneInfo", G_UIModuleBase)

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

function this.methods()
  return {
    geneSkillList = {
      onClick_Choose = function(self, data)
        self:showTip()
      end
    },
    onClick_close = function(self)
      self.bind.active_tip = false
    end,
    onScroll_geneList = function(self)
      self:updateScrollNew()
    end
  }
end

function this:open()
end

function this:updateScrollNew()
  local haveNewUp, haveNewBottom = false
  local showMinIndex = math.maxinteger
  local showMaxIndex = math.mininteger
  for i, v in ipairs(self.modules.list_tipGeneList) do
    if v.isBind then
      showMinIndex = math.min(showMinIndex, v.bind.index)
      showMaxIndex = math.max(showMaxIndex, v.bind.index)
    elseif showMinIndex > v.bind.index then
      haveNewUp = true
    elseif showMaxIndex < v.bind.index then
      haveNewBottom = true
    end
  end
  self.bind.go_newDown = haveNewBottom
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.pet) then
    return
  end
  self.bind.geneSkillList:clear()
  self.bind.list_tipGeneList:clear()
  local genelist = L_PetStore:getPetSkills(self.pet, true, true, false)
  table.sort(genelist, function(a, b)
    return a.genePos < b.genePos
  end)
  for k, gene in ipairs(genelist) do
    gene.index = k
  end
  self.geneData = genelist
  self.bind.geneSkillList:insert_array(genelist)
  self.bind.list_tipGeneList:insert_array(genelist)
end

function this:setGuid(guid)
  self.pet = L_PetStore:getPetItem(guid)
  self:refreshView()
end

function this:refreshGeneSelectState(data)
  for i = 1, #self.bind.geneSkillList do
    if table.isEmpty(data) then
      self.bind.geneSkillList:change(i, {selected = false})
    elseif i == data.index then
      self.bind.geneSkillList:change(i, {selected = true})
    else
      self.bind.geneSkillList:change(i, {selected = false})
    end
  end
end

return this
