local this = class("commonItemTpl")

function this:init(config)
  self.data = config
  self.typeGroupData = {}
  self.typeNameData = {}
  self.rouletteTypeGroupData = {}
  for i, v in pairs(config) do
    local itemType = self:getType(v)
    if not self.typeGroupData[itemType] then
      self.typeGroupData[itemType] = {}
    end
    if not self.typeNameData[itemType] then
      self.typeNameData[itemType] = self:getTypeName(v)
    end
    self.typeGroupData[itemType][v.id] = v
    if v.rouletteType > 0 then
      if not self.rouletteTypeGroupData[v.rouletteType] then
        self.rouletteTypeGroupData[v.rouletteType] = {}
      end
      if not self.rouletteTypeGroupData[v.rouletteType][v.rouletteTab] then
        self.rouletteTypeGroupData[v.rouletteType][v.rouletteTab] = {}
      end
      table.insert(self.rouletteTypeGroupData[v.rouletteType][v.rouletteTab], v)
    end
  end
end

function this:getTplById(id)
  if not self.data[id] then
    printf("[Common Item]", "\231\137\169\229\147\129id\232\161\168\233\135\140\228\184\141\229\173\152\229\156\168 " .. id)
  end
  return self.data[id]
end

function this:getInBag(tpl)
  return tpl.inBag == 1
end

function this:getCanUse(tpl)
  return tpl.canUse == 1
end

function this:getUseFunction(tpl)
  return tpl.useFunction
end

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

function this:getType(tpl)
  return tpl.type
end

function this:getTypeName(tpl)
  return L_Config:provider(tpl.typeName)
end

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

function this:getSpecialDesc(tpl)
  return L_Config:provider(tpl.specialDesc)
end

function this:getIcon(tpl)
  local icon = L_Config:getPathByHash(tpl.icon)
  if string.isEmpty(icon) then
    if tpl.type == L_BagConst.bagItemType.food then
      return "UI/Atlas/Item/tex_icon_itemcooking_default.png"
    elseif tpl.type == L_BagConst.bagItemType.starGift then
      return "UI/Atlas/Item/tex_icon_itemstargift_default.png"
    elseif tpl.type == L_BagConst.bagItemType.skillBook then
      return "UI/Atlas/Item/tex_icon_itemtitle_default.png"
    elseif tpl.type == L_BagConst.bagItemType.enemyCommon then
      return "UI/Atlas/Item/tex_icon_itemstargift_default.png"
    else
      return "UI/Atlas/Item/tex_icon_item_default.png"
    end
  end
  return icon
end

function this:getWayList(tpl)
  local res = {}
  for i = 1, 3 do
    local systemIdKey = string.format("way%d", i)
    local descKey = string.format("way%ddesc", i)
    if not string.isEmpty(L_Config:provider(tpl[descKey])) and L_JumpMgr:checkMapCollectionJumpSystemUnlock(tpl[systemIdKey]) then
      table.insert(res, {
        id = tpl[systemIdKey],
        desc = L_Config:provider(tpl[descKey])
      })
    end
  end
  return res
end

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

function this:getStack(tpl)
  return tpl.stack
end

function this:getTab(tpl)
  return tpl.tab
end

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

function this:getConfig()
  return self.data
end

function this:getSubId(tpl)
  return tpl.subId
end

function this:getTypeGroup(type)
  return self.typeGroupData[type] or {}
end

function this:getTypeNameByTypeNum(type)
  return self.typeNameData[type]
end

function this:getTag(tpl)
  local res = {}
  for i, v in ipairs(tpl.tag) do
    table.insert(res, L_Config:provider(v))
  end
  return res
end

function this:getUseType(tpl)
  return tpl.useType
end

function this:getBagType(tpl)
  return tpl.bagType
end

function this:getBagSubType(tpl)
  return tpl.subType
end

function this:getGiftBagReward(tpl)
  return tpl.giftBagReward
end

function this:getStackNum(tpl)
  return tpl.stackNum
end

function this:getRouletteType(tpl)
  return tpl.rouletteType
end

function this:getRouletteTab(tpl)
  return tpl.rouletteTab
end

function this:getFilterIcon(tpl)
  return tpl.filterIcon
end

return this
