local this = class("battleInfoTpl")

function this:init(config)
  self.data = config
  self.group = {}
  for i, v in pairs(config) do
    if not table.isEmpty(v.attrGroup) then
      if not self.group[v.attrGroup[1]] then
        self.group[v.attrGroup[1]] = {}
      end
      self.group[v.attrGroup[1]][v.attrGroup[2]] = v
    end
  end
end

function this:getGroupId(tpl)
  if not table.isEmpty(tpl.attrGroup) then
    return tpl.attrGroup[1]
  end
end

function this:getGroupType(tpl)
  if not table.isEmpty(tpl.attrGroup) then
    return tpl.attrGroup[2]
  end
end

function this:getHasGroup(id)
  local tpl = self:getTplById(id)
  if tpl == nil then
    return false
  else
    return not table.isEmpty(tpl.attrGroup)
  end
end

function this:getTplById(id)
  return self.data[id]
end

function this:getAttrType(tpl)
  return tpl.attrType
end

function this:getIsRatio(tpl)
  return tpl.isRatio
end

function this:getAttrVal(tpl)
  return tpl.attrVal
end

function this:getAttrID(tpl)
  return tpl.attrID
end

function this:getName(tpl)
  return L_Config:provider(tpl.name)
end

function this:getIcon(tpl)
  local icon = L_Config:getPathByHash(tpl.icon)
  return icon
end

function this:getShowTxt(id, value)
  local tpl = self.data[id]
  if tpl.isRatio == 0 then
    return tostring(value)
  else
    local num = string.format("%.1f", value / 100)
    return string.concat(num, "%")
  end
end

function this:getShowTxtComma(id, value)
  local tpl = self.data[id]
  if tpl.isRatio == 0 then
    return self:addComma(value)
  else
    local num = string.format("%.1f", value / 100)
    return string.concat(num, "%")
  end
end

function this:addComma(value)
  if tonumber(value) == nil then
    return "Error: Input is not a number."
  end
  value = tostring(value)
  local integer_part, decimal_part = string.match(value, "^(%d+)(%.%d*)$")
  integer_part = integer_part or value
  local output = ""
  local length = string.len(integer_part)
  local counter = 0
  for i = length, 1, -1 do
    counter = counter + 1
    output = string.sub(integer_part, i, i) .. output
    if counter == 3 and 1 < i then
      output = "," .. output
      counter = 0
    end
  end
  if decimal_part then
    output = output .. decimal_part
  end
  return output
end

function this:getShowPropertyTxt_spiritron(id, value)
  local tpl = self.data[id]
  if tpl.isRatio == 0 then
    return tostring(value)
  else
    return tostring(value / 100) .. "%"
  end
end

function this:getShowIncreaseNum(id, value1, value2)
  local tpl = self.data[id]
  if tpl.isRatio == 0 then
    return tostring(value1 - value2)
  else
    local temp = (value1 - value2) / 100
    if temp == 0 then
      return "0%"
    end
    return string.concat(temp, "%")
  end
end

function this:getGroupById(id)
  local group = self:getTplById(id).attrGroup
  if table.isEmpty(group) then
    return
  end
  local groupId = group[1]
  return self.group[groupId]
end

function this:getShortName(tpl)
  return L_Config:provider(tpl.shortName)
end

function this:getShowType(tpl)
  return tpl.showType
end

function this:getSort(tpl)
  return tpl.sort
end

function this:getPetShowType(tpl)
  return tpl.petShowType
end

function this:getPetSort(tpl)
  return tpl.petSort
end

function this:getPetFruitPixelEffectPathInfo(tpl)
  local petFruitPixelEffectPathInfo = {}
  petFruitPixelEffectPathInfo.frontPixelEffectPath = tpl.pixelEffect[1]
  petFruitPixelEffectPathInfo.backPixelEffectPath = tpl.pixelEffect[2]
  return petFruitPixelEffectPathInfo
end

function this:getPixelEffectFrameTextureConfigInfos(tpl)
  local frameTextureConfigInfos = {}
  for i, rawFrameTextureConfigInfo in pairs(tpl.pixelEffectFrame) do
    local frameTextureConfigInfo = {}
    frameTextureConfigInfo.rowNum = rawFrameTextureConfigInfo[1]
    frameTextureConfigInfo.colNum = rawFrameTextureConfigInfo[2]
    frameTextureConfigInfo.iconNum = rawFrameTextureConfigInfo[3]
    if i == 1 then
      frameTextureConfigInfos.frontFrameTextureConfigInfo = frameTextureConfigInfo
    else
      frameTextureConfigInfos.backFrameTextureConfigInfo = frameTextureConfigInfo
    end
  end
  return frameTextureConfigInfos
end

function this:getGroupIdById(id)
  local group = self:getTplById(id).attrGroup
  if table.isEmpty(group) then
    return
  end
  return group
end

return this
