local this = class("pagePetGeneMutation", G_UIPageBase)
local _skillLevelTpl = L_GameTpl:getSkillLevelTpl()
local _skillTpl = L_GameTpl:getSkillTpl()
local petDnaTpl = L_GameTpl:getDnaTpl()
local rarity = {
  [1] = {
    color = "#898E93",
    bgImg = "UI/Atlas/PetGene/tes_petdnamutation_bg_grey.png"
  },
  [2] = {
    color = "#898E93",
    bgImg = "UI/Atlas/PetGene/tes_petdnamutation_bg_grey.png"
  },
  [3] = {
    color = "#6CAADD",
    bgImg = "UI/Atlas/PetGene/tes_petdnamutation_bg_blue.png"
  },
  [4] = {
    color = "#A394C4",
    bgImg = "UI/Atlas/PetGene/tes_petdnamutation_bg_purple.png"
  },
  [5] = {
    color = "#D99738",
    bgImg = "UI/Atlas/PetGene/tes_petdnamutation_bg_yellow.png"
  },
  [6] = {
    color = "#A493B9",
    bgImg = "UI/Atlas/PetGene/tes_petdnamutation_bg_shan.png"
  }
}

function this.bind()
  return {
    img_originGeneBg = "",
    txt_originGeneName = "",
    txt_originGeneName_en = "",
    txt_originGeneDesc = "",
    color_originGeneLeftLine = nil,
    color_originGeneRightLine = nil,
    img_newGeneBg = "",
    txt_newGeneName = "",
    txt_newGeneName_en = "",
    txt_newGeneDesc = "",
    color_newGeneLeftLine = nil,
    color_newGeneRightLine = nil
  }
end

function this.methods()
  return {
    onClick_quit = function(self)
      self:onBtnQuit()
    end,
    onClick_confirm = function(self)
      self:onBtnConfirm()
    end,
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.petId = options.petId
end

function this:open()
  self:initPage()
end

function this:initPage()
  local petItem = L_PetStore:getPetItem(self.petId)
  local mutation_info = petItem:getMutationInfo()
  local oldInfo
  for i, gene in pairs(petItem.gene_infos) do
    if gene.pos == mutation_info.pos then
      oldInfo = gene
      break
    end
  end
  if not oldInfo then
    errorf("\231\170\129\229\143\152\229\155\160\229\173\144\230\149\176\230\141\174\233\148\153\232\175\175\239\188\129\239\188\129\239\188\129" .. mutation_info)
    return
  end
  local oldSkillLvCfg = _skillLevelTpl:getTplByIdAndLevel(oldInfo.gene_id, 1)
  if oldSkillLvCfg ~= nil then
    local skillGroupId = tonumber(_skillLevelTpl:getSkillGroupId(oldSkillLvCfg))
    local skillCfg = _skillTpl:getTplById(skillGroupId)
    local skillDescribe = _skillLevelTpl:getSkillDescribe(oldSkillLvCfg)
    local skillName = _skillTpl:getName(skillCfg)
    local dnaTpl = petDnaTpl:getTplById(skillGroupId)
    local dnaRarity = petDnaTpl:getRarity(dnaTpl)
    local _, rarityColor = C_ColorUtility.TryParseHtmlString(rarity[dnaRarity].color)
    self.bind.img_originGeneBg = rarity[dnaRarity].bgImg
    self.bind.txt_originGeneName = L_GameUtil.fillColor(skillName, rarity[dnaRarity].color)
    self.bind.txt_originGeneName_en = L_GameUtil.fillColor("BATTLE YINZI", rarity[dnaRarity].color)
    self.bind.txt_originGeneDesc = skillDescribe
    self.bind.color_originGeneLeftLine = rarityColor
    self.bind.color_originGeneRightLine = rarityColor
  end
  local newSkillLvCfg = _skillLevelTpl:getTplByIdAndLevel(mutation_info.gene_id, 1)
  if newSkillLvCfg ~= nil then
    local skillGroupId = tonumber(_skillLevelTpl:getSkillGroupId(newSkillLvCfg))
    local skillCfg = _skillTpl:getTplById(skillGroupId)
    local skillDescribe = _skillLevelTpl:getSkillDescribe(newSkillLvCfg)
    local skillName = _skillTpl:getName(skillCfg)
    local dnaTpl = petDnaTpl:getTplById(skillGroupId)
    local dnaRarity = petDnaTpl:getRarity(dnaTpl)
    local _, rarityColor = C_ColorUtility.TryParseHtmlString(rarity[dnaRarity].color)
    self.bind.img_newGeneBg = rarity[dnaRarity].bgImg
    self.bind.txt_newGeneName = L_GameUtil.fillColor(skillName, rarity[dnaRarity].color)
    self.bind.txt_newGeneName_en = L_GameUtil.fillColor("BATTLE YINZI", rarity[dnaRarity].color)
    self.bind.txt_newGeneDesc = skillDescribe
    self.bind.color_newGeneLeftLine = rarityColor
    self.bind.color_newGeneRightLine = rarityColor
  end
end

function this:onBtnQuit()
  L_GameUtil.showCommonTip({
    txtContent = L_WordsTpl:getValue("notice_pagePetGeneMutation_01"),
    confirmCallback = function()
      L_PetStore:req_chooseGeneMutation(self.petId, false, function()
        L_PetStore:call(L_PetStore.event.refreshPetGeneMutation, self.petId)
        L_UI:close(self.pageName)
      end)
    end
  })
end

function this:onBtnConfirm()
  L_GameUtil.showCommonTip({
    txtContent = L_WordsTpl:getValue("notice_pagePetGeneMutation_02"),
    confirmCallback = function()
      L_PetStore:req_chooseGeneMutation(self.petId, true, function()
        L_PetStore:call(L_PetStore.event.refreshPetGeneMutation, self.petId)
        L_UI:close(self.pageName)
      end)
    end
  })
end

function this:close(options)
  this.super.close(self, options)
end

return this
