local module = class("cellPetNewGene", G_UIModuleBase)

function module:ctor(...)
  module.super.ctor(self, ...)
  self.data = {
    id = nil,
    guid = nil,
    rarity = {
      [1] = "UI/Atlas/PetBoxNew/tex_pet_bg_yinzi1.png",
      [2] = "UI/Atlas/PetBoxNew/tex_pet_bg_yinzi1.png",
      [3] = "UI/Atlas/PetBoxNew/tex_pet_bg_yinzi2.png",
      [4] = "UI/Atlas/PetBoxNew/tex_pet_bg_yinzi3.png",
      [5] = "",
      [6] = ""
    },
    rarityColor = {
      [1] = "#898E93",
      [2] = "#898E93",
      [3] = "#6CAADD",
      [4] = "#A394C4",
      [5] = "#D99738",
      [6] = "#A493B9"
    }
  }
end

function module.bind()
  return {
    selected = false,
    bg = "",
    show_lockBg = false,
    color_lockBg = C_Color(1, 1, 1, 0.7),
    active_goldNode = false,
    active_specialNode = false,
    active_specialLevel = false,
    active_levelList = false,
    levelList = {
      moduleName = "pages/Pet/cellDotStarItem"
    },
    active_line = true,
    active_debuffLine = false,
    color_line = C_Color(1, 1, 1, 0.3),
    geneNameText = "",
    show_rarity = false,
    active_newTag = false,
    active_mutationTag = false
  }
end

function module.methods()
  return {
    onClick_Choose = function(self)
      self:emit("onClick_Choose", self.bind)
    end
  }
end

function module:refresh()
  self:refreshInfo()
end

function module:refreshData(data)
  self.bind.geneId = data.geneId
  self.bind.lock = data.lock
  self.bind.gene_skill = data.gene_skill
  self.bind.pos = data.pos
  self.bind.gene_name = data.gene_name
  self.bind.rarity = data.rarity
  self.bind.geneLv = data.geneLv
  self.bind.rectPos = nil
  self.bind.petId = data.petId
  self:refreshInfo()
end

function module:setSelected(isSelected)
  self.bind.selected = isSelected
end

function module:refreshInfo()
  if self.bind.rectPos then
    self.bindComponents.geneTrans.localPosition = self.bind.rectPos
  end
  self.bind.geneNameText = self.bind.skillName
  if not math.isEmpty(self.bind.geneId) then
    local isSpecialGene = self.bind.rarity >= #self.data.rarity
    local isTopGene = self.bind.rarity >= #self.data.rarity - 1
    self.bind.show_rarity = isTopGene
    if self.bind.show_rarity then
      if isSpecialGene then
        self.bind.color_lockBg = C_Color(0.3, 0.369, 0.53, 0.5)
      else
        self.bind.color_lockBg = C_Color(0.9843137, 0.8666667, 0.5843138, 0.7)
      end
      self.bind.active_goldNode = not isSpecialGene
      self.bind.active_specialNode = isSpecialGene
    else
      self.bind.color_lockBg = C_Color(1, 1, 1, 0.7)
    end
    if isSpecialGene then
      self.bind.color_line = C_Color(0.7058824, 0.8901961, 0.972549, 0.3)
    else
      self.bind.color_line = C_Color(1, 1, 1, 0.3)
    end
    self.bind.bg = self.data.rarity[self.bind.rarity]
    self.bind.active_levelList = not isSpecialGene
    self.bind.active_specialLevel = isSpecialGene
    if not isSpecialGene then
      self.bind.levelList:clear()
      self.star = self.bind.rarity - 2
      if self.star > 0 then
        local data = {}
        for i = 1, self.star do
          table.insert(data, {})
        end
        self.bind.levelList:insert_array(data)
        self.bind.active_line = true
        self.bind.active_debuffLine = false
      else
        self.bind.active_line = false
        self.bind.active_debuffLine = true
      end
    end
    local isMutationTag = false
    local isNewTag = false
    local petItem = L_PetStore:getPetItem(self.bind.petId)
    if petItem then
      if petItem:hasMutationGene() and petItem:getMutationInfo().pos == self.bind.genePos then
        isMutationTag = true
      end
      if L_PetStore:checkIsNewGene(self.bind.petId, self.bind.genePos) then
        isNewTag = true
      end
    end
    if isSpecialGene then
      self.bind.geneNameText = string.format("<color=#4c5e87>%s</color>", self.bind.skillName)
      self.bind.active_line = true
      self.bind.active_debuffLine = false
    elseif isTopGene then
      self.bind.geneNameText = string.format("<color=#FFD65B>%s</color>", self.bind.skillName)
    elseif isMutationTag then
      self.bind.geneNameText = L_GameUtil.fillColor(self.bind.skillName, self.data.rarityColor[self.bind.rarity])
    end
    self.bind.active_mutationTag = isMutationTag
    self.bind.active_newTag = isNewTag
  end
  self.bind.show_lockBg = self.bind.lock
  if not self.bindComponents.info_layout then
    return
  end
  L_GameUtil.forceRebuildLayout(self.bindComponents.info_layout)
end

return module
