local this = class("cellPetAttributeInfo", G_UIModuleBase)
local _petLearingEnumTpl = L_GameTpl:getPetLearingEnumTpl()
local _battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local _petTalentUpgradeTpl = L_GameTpl:getPetTalentUpgradeTpl()
local petLearningAblityTpl = L_GameTpl:getPetLearningAblityTpl()
local ColorOfTextComprehensionLevel = {
  NORMAL = C_LuaUtility.ParseHtmlStringColor("#806E5B"),
  UPGRADE = C_LuaUtility.ParseHtmlStringColor("#E0923E")
}
local qualityImg = {
  ss = "UI/Atlas/PetUpgrade/tex_petupgrade_dec_attributedec1.png",
  sss = "UI/Atlas/PetUpgrade/tex_petupgrade_dec_attributedec2.png"
}
local qualityTextColorGradient = {ss = "#e0923e", sss = "#ff5d92"}
local lightIconColor = {ss = "#ffbf5b", sss = "#ff9dab"}

function this.bind()
  return {
    img_attributeIcon = "",
    img_attributeLightIcon = "",
    img_attrQuality = "",
    color_attrIconColor = nil,
    color_attrLightIcon = nil,
    color_attrRingColor = nil,
    text_curAttrNum = "",
    text_addAttrNum = "",
    txt_attrLevel = "",
    txt_attrExp = "",
    active_isUpLevel = false,
    active_isMaxLevel = false,
    fill_petExp = 0,
    fill_curExp = 0
  }
end

function this.methods()
  return {}
end

function this:created(...)
  this.super.created(self, ...)
end

function this:open()
  this.super.open(self)
end

function this:setAddResultExpLayoutWidth()
  local width = self.bindComponents.trans_image_curExp.rect.width
  local height = self.bindComponents.trans_image_curExp.rect.height
  self.bindComponents.trans_layout_addResultExp.sizeDelta = C_Vector2(width, height)
  self.addResultExpLayoutWidth = width
  self.addResultExpLayoutHeight = height
end

function this:setShowPetGuidAndAttrEnumNum(petGuid, attrEnumNum)
  self.petGuid = petGuid
  self.attrEnumNum = attrEnumNum
  self.comprehensionMaxLevel = _petTalentUpgradeTpl:getTargetComprehensionMaxLevel(self.attrEnumNum)
end

function this:refreshBaseInfoUI()
  if self.petGuid == nil or self.attrEnumNum == nil then
    errorf("cellPetAttributeInfo \230\156\170\232\174\190\231\189\174\233\156\128\232\166\129\229\177\149\231\164\186\231\154\132\229\174\160\231\137\169Guid\229\146\140\229\177\158\230\128\167\230\158\154\228\184\190")
    return
  end
  self.bind.active_isUpLevel = false
  self.bind.active_isMaxLevel = false
  self.bind.text_addAttrNum = ""
  self.bind.img_attributeLightIcon = ""
  self.petItem = L_PetStore:getPetItem(self.petGuid)
  local petItem = self.petItem
  local petProperty = L_PetStore:getPetProperty(petItem)
  self.petProperty = petProperty
  local petLearingEnumTpl = _petLearingEnumTpl:getTplByEnumId(self.attrEnumNum)
  self.bind.img_attributeIcon = _petLearingEnumTpl:getNewIcon(petLearingEnumTpl)
  local color = _petLearingEnumTpl:getIconColor(petLearingEnumTpl)
  if string.isEmpty(color) then
    color = "#ffffff"
  end
  local _, attrColor = C_ColorUtility.TryParseHtmlString(color)
  self.bind.color_attrIconColor = attrColor
  self.bind.color_attrRingColor = attrColor
  local topRankComprehension = L_PetStore:getRangeValueComprehension(false, 8, petItem.comprehension)
  local attr_id
  for n, k in pairs(topRankComprehension) do
    if k.attr_id == self.attrEnumNum then
      attr_id = k.rankId
      break
    end
  end
  self.bind.img_attrQuality = ""
  if attr_id then
    local tpl = petLearningAblityTpl:getTplById(attr_id)
    local level = petLearningAblityTpl:getLevel(tpl)
    local imgPath = qualityImg[level]
    if imgPath then
      self.bind.img_attrQuality = imgPath
      self.bind.color_attrIconColor = C_Color(1, 1, 1, 1)
      self.bind.color_attrRingColor = C_Color(1, 1, 1, 1)
    end
    local qualityColor = qualityTextColorGradient[level]
    if qualityColor then
      local _1, topColor = C_ColorUtility.TryParseHtmlString("#353130")
      local _2, bottomColor = C_ColorUtility.TryParseHtmlString(qualityColor)
      self.bindComponents.LText_curAttrNum:SetColorGradient(topColor, topColor, bottomColor, bottomColor)
    end
    local attrLightIconColor = lightIconColor[level]
    if attrLightIconColor then
      self.bind.img_attributeLightIcon = _petLearingEnumTpl:getLightIcon(petLearingEnumTpl)
      local _, lightColor = C_ColorUtility.TryParseHtmlString(attrLightIconColor)
      self.bind.color_attrLightIcon = lightColor
    end
  end
  self.curAttrNum = L_PetStore:getPetAttFinalValue(petProperty, self.attrEnumNum, self.petGuid)
  self.curAttrNumTxt = _battleInfoTpl:getShowTxtComma(self.attrEnumNum, self.curAttrNum)
  self.bind.text_curAttrNum = self.curAttrNumTxt
  local selfAttrComprehension = petItem:getTargetAttributeComprehension(self.attrEnumNum)
  self.selfAttrComprehension = selfAttrComprehension
  self.bind.txt_attrLevel = string.format("Lv. <size=24>%s</size>", tostring(selfAttrComprehension.level))
  local isMaxLv = selfAttrComprehension.level >= self.comprehensionMaxLevel
  self.bind.active_isMaxLevel = isMaxLv
  local tpl = _petTalentUpgradeTpl:getTplByAttrEnumNumAndLevel(self.attrEnumNum, selfAttrComprehension.level)
  local nextExp = _petTalentUpgradeTpl:getCurLevelMaxExp(tpl)
  local curExp = isMaxLv and nextExp or selfAttrComprehension.cur_exp
  self.bind.txt_attrExp = string.format("%d/%d", curExp, nextExp)
  self.bind.fill_petExp = curExp / nextExp
  self.bind.fill_curExp = curExp / nextExp
end

function this:refreshPreAddComprehensionExpUI(addExp, newLv)
  addExp = addExp or 0
  if addExp == 0 then
    self:refreshBaseInfoUI()
    return
  end
  if self.selfAttrComprehension.level >= self.comprehensionMaxLevel then
    return
  end
  local maxNeedExpOfOneComprehension = _petTalentUpgradeTpl:getMaxNeedExpOfOneComprehension(self.attrEnumNum)
  local selfAttrComprehension = self.petItem:getTargetAttributeComprehension(self.attrEnumNum)
  local tpl = _petTalentUpgradeTpl:getTplByAttrEnumNumAndLevel(self.attrEnumNum, selfAttrComprehension.level)
  local nextExp = _petTalentUpgradeTpl:getCurLevelMaxExp(tpl)
  local curComprehensionExp = selfAttrComprehension.cur_exp + nextExp
  if maxNeedExpOfOneComprehension <= addExp + curComprehensionExp then
    addExp = maxNeedExpOfOneComprehension - curComprehensionExp
  end
  self.bind.txt_attrExp = string.format("%d%s/%d", self.selfAttrComprehension.cur_exp, L_GameUtil.fillColor("+" .. tostring(addExp), L_Const.colorHtml.yellow005), nextExp)
  local targetLevel = _petTalentUpgradeTpl:getTargetLevelByAttrEnumNumCurLevelCurExpAddExp(self.attrEnumNum, self.selfAttrComprehension.level, self.selfAttrComprehension.cur_exp, addExp)
  self.bind.fill_curExp = targetLevel > self.selfAttrComprehension.level and 1 or (self.selfAttrComprehension.cur_exp + addExp) / nextExp
  if targetLevel > self.selfAttrComprehension.level then
    self.bind.txt_attrLevel = L_GameUtil.fillColor(string.format("Lv. <size=24>%s</size>", tostring(targetLevel)), L_Const.colorHtml.yellow005)
    self.bind.active_isUpLevel = true
  else
    self.bind.txt_attrLevel = string.format("Lv. <size=24>%s</size>", tostring(selfAttrComprehension.level))
    self.bind.active_isUpLevel = false
  end
  if targetLevel > self.selfAttrComprehension.level then
    local deep_clone = function(object)
      local lookup_table = {}
      
      local function _copy(object)
        if type(object) ~= "table" then
          return object
        elseif lookup_table[object] then
          return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for key, value in pairs(object) do
          new_table[_copy(key)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
      end
      
      return _copy(object)
    end
    local newPetItem = deep_clone(self.petItem)
    for _, comprehension in ipairs(newPetItem.comprehension) do
      if comprehension.attr_id == self.attrEnumNum then
        comprehension.level = targetLevel
        local petTalentUpgradeTpl = _petTalentUpgradeTpl:getTplByAttrEnumNumAndLevel(self.attrEnumNum, targetLevel)
        comprehension.value = _petTalentUpgradeTpl:getStartValue(petTalentUpgradeTpl)
      end
    end
    local newPetProperty = L_PetStore:getPetProperty(newPetItem, newLv)
    local newAttrNum = L_PetStore:getPetAttFinalValue(newPetProperty, self.attrEnumNum)
    local deltaAttrNum = newAttrNum - self.curAttrNum
    if deltaAttrNum ~= 0 then
      self.bind.text_addAttrNum = string.format(" +%s", _battleInfoTpl:getShowTxtComma(self.attrEnumNum, deltaAttrNum))
    else
      self.bind.text_addAttrNum = ""
    end
  else
    local pet = L_PetStore:getPetItem(self.petGuid)
    local old_property = L_PetStore:getPetOriginalProperty(pet)
    local property = L_PetStore:getPetOriginalProperty(pet, newLv)
    local old_value = L_PetStore:getPetAttFinalValue(old_property, self.attrEnumNum)
    local value = L_PetStore:getPetAttFinalValue(property, self.attrEnumNum)
    if value ~= old_value then
      self.bind.text_addAttrNum = string.format(" +%s", _battleInfoTpl:getShowTxtComma(self.attrEnumNum, value - old_value))
    else
      self.bind.text_addAttrNum = ""
    end
  end
end

function this:getAttrEnumNum()
  return self.attrEnumNum
end

function this:close()
end

return this
