local this = {}
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local _petTpl = L_GameTpl:getPetTpl()
local _heroTpl = L_GameTpl:getHeroTpl()
local _mountTpl = L_GameTpl:getMountTpl()
local _systemFiltercontentTpl = L_GameTpl:getSystemFiltercontentTpl()
local Type2Func_filter = {
  [L_Const.resType.accessory] = "getAccessoryFilterKey",
  [L_Const.resType.pet] = "getPetFilterKey",
  [L_Const.resType.hero] = "getHeroFilterKey"
}

function this:getFilterKey(itemType, keyDic, itemId, guid)
  if keyDic[L_SortFilterConst.FilterType.All] then
    return {}
  end
  if Type2Func_filter[itemType] then
    return self[Type2Func_filter[itemType]](self, keyDic, itemId, guid)
  else
    errorf("\231\177\187\229\158\139" .. itemType .. "\228\187\163\231\160\129\230\156\170\232\161\165\229\133\133\239\188\140\228\188\152\229\133\136\230\137\190\231\173\150\229\136\146\231\161\174\232\174\164\230\152\175\229\144\166\233\133\141\231\189\174\230\173\163\231\161\174\229\185\182\230\137\190\231\168\139\229\186\143\230\142\165\229\133\165\228\186\134")
  end
end

function this:getAccessoryFilterKey(keyDic, itemId, guid)
  local tpl_item = _accessoryTpl:getTplById(itemId)
  local serverData = guid and L_AccessoryStore:getAccessory(guid) or {}
  local res = {}
  for typeId, idList in pairs(keyDic) do
    local compareValue
    if typeId == L_SortFilterConst.FilterType.Suit then
      compareValue = _accessoryTpl:getSetId(tpl_item)
    elseif typeId == L_SortFilterConst.FilterType.EquipStatus then
      compareValue = math.isEmpty(serverData.wearHero) and L_SortFilterConst.FilterId.IsNotEquipped or L_SortFilterConst.FilterId.IsEquipped
    elseif typeId == L_SortFilterConst.FilterType.EquipQuality then
      compareValue = _accessoryTpl:getRarity(tpl_item)
    elseif typeId == L_SortFilterConst.FilterType.LevelStatus then
      compareValue = L_AccessoryStore:getIsMaxLevel(guid) and L_SortFilterConst.FilterId.MaxLevel or L_SortFilterConst.FilterId.NotMaxLevel
    elseif typeId == L_SortFilterConst.FilterType.LockedStatus then
      compareValue = serverData.lock and L_SortFilterConst.FilterId.Locked or L_SortFilterConst.FilterId.UnLocked
    end
    if compareValue == nil then
      errorf("\231\173\155\233\128\137\228\184\187\233\162\152" .. typeId .. "\229\156\168\232\163\133\229\164\135\228\187\163\231\160\129\230\156\170\232\161\165\229\133\133\239\188\140\228\188\152\229\133\136\230\137\190\231\173\150\229\136\146\231\161\174\232\174\164\230\152\175\229\144\166\233\133\141\231\189\174\230\173\163\231\161\174\229\185\182\230\137\190\231\168\139\229\186\143\230\142\165\229\133\165\228\186\134")
    end
    local node, index = table.ipairsFind(idList, function(value)
      return value == compareValue
    end)
    res[L_SortFilterConst.FilterTypeKeyName[typeId]] = index ~= nil
  end
  return res
end

function this:getPetFilterKey(keyDic, itemId, guid)
  local tpl_item = _petTpl:getTplById(itemId)
  local res = {}
  local pet = L_PetStore:getPetItem(guid)
  for typeId, idList in pairs(keyDic) do
    local compareValue, isTable
    if typeId == L_SortFilterConst.FilterType.Element or typeId == L_SortFilterConst.FilterType.ElementWithoutNone then
      compareValue = _petTpl:getElement(tpl_item)
      isTable = true
    elseif typeId == L_SortFilterConst.FilterType.MoveType then
      local paraList = {}
      for i, v in pairs(idList) do
        local tpl_systemFilterContent = _systemFiltercontentTpl:getTplById(v)
        local para = _systemFiltercontentTpl:getEnumId(tpl_systemFilterContent)
        table.insert(paraList, para)
      end
      idList = paraList
      local tpl_mount = _mountTpl:getTplById(itemId)
      compareValue = _mountTpl:getMoveType(tpl_mount)
      isTable = true
    elseif typeId == L_SortFilterConst.FilterType.QiBoGrade then
      local _, temp = L_PetStore:getPetGradeNew(guid)
      compareValue = temp
    elseif typeId == L_SortFilterConst.FilterType.QiBoSpecial then
      compareValue = pet:getIsSpecialPet() and L_SortFilterConst.FilterId.PetSpeical or L_SortFilterConst.FilterId.PetNormal
    elseif typeId == L_SortFilterConst.FilterType.QiBoBoxMode then
      compareValue = L_SortFilterConst.FilterId.QiBoBoxMode
    end
    if compareValue == nil then
      errorf("\231\173\155\233\128\137\228\184\187\233\162\152" .. typeId .. "\229\156\168\229\174\160\231\137\169\228\187\163\231\160\129\230\156\170\232\161\165\229\133\133\239\188\140\228\188\152\229\133\136\230\137\190\231\173\150\229\136\146\231\161\174\232\174\164\230\152\175\229\144\166\233\133\141\231\189\174\230\173\163\231\161\174\229\185\182\230\137\190\231\168\139\229\186\143\230\142\165\229\133\165\228\186\134")
    end
    local node, index = table.ipairsFind(idList, function(value)
      if isTable then
        local node2, index2 = table.ipairsFind(compareValue, function(value2)
          return value2 == value
        end)
        return index2 ~= nil
      else
        return value == compareValue
      end
    end)
    res[L_SortFilterConst.FilterTypeKeyName[typeId]] = index ~= nil
  end
  return res
end

function this:getHeroFilterKey(keyDic, itemId, guid)
  local tpl_item = _heroTpl:getTplById(itemId)
  local serverData = guid and L_HeroStore:getHero(guid) or {}
  local res = {}
  for typeId, idList in pairs(keyDic) do
    local compareValue, isTable
    if typeId == L_SortFilterConst.FilterType.Element then
      compareValue = _heroTpl:getElement(tpl_item)
    elseif typeId == L_SortFilterConst.FilterType.Profession then
      compareValue = _heroTpl:getPosition(tpl_item)
    end
    if compareValue == nil then
      errorf("\231\173\155\233\128\137\228\184\187\233\162\152" .. typeId .. "\229\156\168\232\167\146\232\137\178\228\187\163\231\160\129\230\156\170\232\161\165\229\133\133\239\188\140\228\188\152\229\133\136\230\137\190\231\173\150\229\136\146\231\161\174\232\174\164\230\152\175\229\144\166\233\133\141\231\189\174\230\173\163\231\161\174\229\185\182\230\137\190\231\168\139\229\186\143\230\142\165\229\133\165\228\186\134")
    end
    local node, index = table.ipairsFind(idList, function(value)
      if isTable then
        local node2, index2 = table.ipairsFind(compareValue, function(value2)
          return value2 == value
        end)
        return index2 ~= nil
      else
        return value == compareValue
      end
    end)
    res[L_SortFilterConst.FilterTypeKeyName[typeId]] = index ~= nil
  end
  return res
end

return this
