local this = class("modulePetLevelInfo", G_UIModuleBase)
local _petLevelTpl = L_GameTpl:getPetLevelTpl()
local commonItemTpl = L_GameTpl:getCommonItemTpl()

function this.bind()
  return {
    txt_curLv = "",
    txt_maxLv = "",
    txt_exp = "",
    fill_petExp = 0,
    fill_curExp = 0,
    active_isMaxLevel = false
  }
end

function this.methods()
  return {}
end

function this:setShowPetGuid(petGuid)
  self.petGuid = petGuid
end

function this:refreshUI()
  if not self.petGuid then
    errorf("modulePetLevelInfo \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")
    return
  end
  self.pet = L_PetStore:getPetItem(self.petGuid)
  self.maxLv = L_PetStore:getPetMaxLevel(true)
  self.bind.txt_maxLv = tostring(self.maxLv)
  self:onEvent_petExpChange()
end

function this:onEvent_petExpChange(selectInfo)
  local pet = L_PetStore:getPetItem(self.petGuid)
  if not selectInfo then
    self:setPetInfo(pet.lv, 0, selectInfo)
    return
  end
  local tempExpAll = 0
  for _, v in ipairs(selectInfo) do
    local exp = commonItemTpl:getUseFunction(commonItemTpl:getTplById(v.item_id))[1]
    tempExpAll = tempExpAll + v.item_num * exp
  end
  local tempExp = tempExpAll + pet.exp
  local tempLv = pet.lv
  for i = tempLv, self.maxLv - 1 do
    if 0 > tempExp - _petLevelTpl:getExp(_petLevelTpl:getTplById(i)) then
      break
    end
    tempLv = i + 1
    tempExp = tempExp - _petLevelTpl:getExp(_petLevelTpl:getTplById(i))
  end
  self:setPetInfo(tempLv, tempExpAll, selectInfo)
end

function this:setPetInfo(lv, addExp, selectInfo)
  self.lv = lv
  local pet = L_PetStore:getPetItem(self.petGuid)
  self.bind.active_isMaxLevel = false
  local petExp = pet.exp
  if not selectInfo or #selectInfo <= 0 then
    self.bind.txt_curLv = L_GameUtil.fillColor(tostring(lv), "#FBFFB8")
  else
    self.bind.txt_curLv = L_GameUtil.fillColor(tostring(lv), L_Const.colorHtml.yellow002)
  end
  local maxExp = _petLevelTpl:getExp(_petLevelTpl:getTplById(pet.lv))
  if lv >= self.maxLv then
    if self.maxLv == L_PetStore:getPetMaxLevel() then
      self.bind.fill_petExp = 1
      self.bind.active_isMaxLevel = true
    else
      self.bind.fill_petExp = 0
    end
    self.bind.fill_curExp = 0
    maxExp = _petLevelTpl:getExp(_petLevelTpl:getTplById(lv - 1))
  else
    self.bind.fill_petExp = petExp / maxExp
    if lv > pet.lv then
      self.bind.fill_curExp = 1
    else
      self.bind.fill_curExp = (addExp + petExp) / _petLevelTpl:getExp(_petLevelTpl:getTplById(lv))
    end
  end
  local txtPetExp = L_GameUtil.fillColor(tostring(petExp), "#AD9947")
  if 0 < addExp then
    self.bind.txt_exp = string.format("%s%s/%d", txtPetExp, L_GameUtil.fillColor("+" .. tostring(addExp), L_Const.colorHtml.yellow005), maxExp)
  else
    self.bind.txt_exp = string.format("%s/%d", txtPetExp, maxExp)
  end
end

function this:getCurrentLv()
  return self.lv
end

return this
