local this = class("homeGridTpl")

function this:init(config)
  self.data = config
  self.tplList = {}
  self.mapIdToHomeId = {}
  for i, v in pairs(self.data) do
    table.insert(self.tplList, v)
    self.mapIdToHomeId[v.worldId] = v.groupId
  end
  table.sort(self.tplList, function(a, b)
    return a.id < b.id
  end)
end

function this:getTplByLvGroup(level)
  local keyId = level + 100
  return self.data[keyId]
end

function this:getObjIdList(tpl)
  return tpl.objId
end

function this:getLevel(tpl)
  return tpl.level or {}
end

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

function this:getWorldId(tpl)
  return tpl.worldId
end

function this:getWorldIdByLvGroup(lv)
  if lv then
    local tpl = self:getTplByLvGroup(lv)
    if tpl then
      return tpl.worldId
    end
  end
end

function this:getIdListByLv(lv, group)
  local tpl = self:getTplByLvGroup(lv, group)
  if tpl then
    return tpl.objId
  end
  return {}
end

function this:getBlockIdListByLv(lv, group)
  group = 1
  local blockList = {}
  for i1, v1 in ipairs(self.tplList) do
    if group == v1.groupId then
      if lv < v1.objLevel then
        break
      end
      for i2, v2 in ipairs(v1.id) do
        if blockList[i2] == nil then
          blockList[i2] = v2
        elseif v2 ~= 0 then
          blockList[i2] = v2
        end
      end
    end
  end
  local testCount = 0
  for i, v in ipairs(blockList) do
    testCount = i
  end
  if testCount == 0 or table.count(blockList) ~= testCount then
    errorf("\229\174\182\229\155\173\231\173\137\231\186\167\229\156\176\229\155\190\230\149\176\230\141\174\233\133\141\231\189\174\233\148\153\232\175\175")
  end
  return blockList
end

function this:getHomeIdByMapId(mapId)
  return self.mapIdToHomeId[mapId]
end

return this
