local this = class("spiritronTpl")

function this:init(config)
  self.data = config
end

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

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

function this:getAttribute(tpl)
  return tpl.attribute
end

function this:getReishiSkill(tpl)
  return tpl.reishiSkill
end

function this:getId(tpl)
  return tpl.id
end

function this:getReishiExp(tpl)
  return tpl.reishiExp
end

function this:getRarity(tpl)
  return tpl.rarity
end

function this:getDesc(tpl)
  return L_Config:provider(tpl.desc)
end

function this:getUIPath(tpl)
  return tpl.uiPath
end

function this:getSpinePath(tpl)
  return tpl.resourcePath
end

function this:getCgPath(tpl)
  return tpl.cgPath
end

function this:getVideo(tpl)
  return tpl.video
end

function this:getVideoSound(tpl)
  return tpl.videoSound
end

function this:getStartAnimation(tpl)
  return tpl.startAnimation
end

function this:getAnimationSound(tpl)
  return tpl.animationSound
end

function this:getAvatarTexture(tpl, index)
  if tpl == nil or tpl.icon == nil then
    return "UI/Atlas/Item/tex_icon_spiritrons_default.png"
  end
  local icon = tpl.icon[index]
  if string.isEmpty(icon) then
    return "UI/Atlas/Item/tex_icon_spiritrons_default.png"
  end
  return icon
end

function this:getProperty(id, level, rank)
  local spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
  local rankData = spiritronRankTpl:getChildren(id)
  if rankData == nil or rankData[rank] == nil then
    return {}
  end
  local rankUpAttribute = rankData[rank].rankUpAttributeAll
  local spiritronAttribute = self:getAttribute(self:getTplById(id))
  if spiritronAttribute then
    local spiritronValueTpl = L_GameTpl:getSpiritronValueTpl()
    local property = spiritronValueTpl:getBaseAttribute(spiritronValueTpl:getTplById(spiritronAttribute * 1000 + level))
    local tab = {}
    for i, v in pairs(property) do
      tab[i] = v
    end
    if not table.isEmpty(rankUpAttribute) then
      tab = self:add(tab, rankUpAttribute)
    end
    local proList = {}
    for k, v in pairs(tab) do
      proList[k] = math.floor(v)
    end
    return proList
  end
  return {}
end

function this:getSkill(id, star)
  local skillLevelTpl = L_GameTpl:getSkillLevelTpl()
  local skillId = self:getSkillId(id)
  if skillId then
    local tpl = skillLevelTpl:getTplByIdAndLevel(skillId, star)
    local skill = skillLevelTpl:getSkillDescribe(tpl) or ""
    local valueList = skillLevelTpl:getValue(tpl) or {}
    return skill, valueList
  end
  return "", {}
end

function this:getSkillId(id)
  local reishiSkill = self:getReishiSkill(self:getTplById(id))
  return reishiSkill
end

function this:add(myTable, otherTable)
  for i, v in pairs(otherTable) do
    if myTable[i] then
      myTable[i] = v + myTable[i]
    else
      myTable[i] = v
    end
  end
  return myTable
end

function this:getTask(tpl)
  return tpl.task
end

function this:getStory(tpl)
  return tpl.story
end

function this:getStoryLock(tpl)
  return tpl.storyLock
end

return this
