local p = {}

local FilterDiv = require('Module:FilterDiv').new
local Const = require("Module:Const/name")
local Util = require('Module:Util')
local Link = require('Module:Item').link
local Linq = require('Module:Linq')
local Data = require("Module:Item/data")
require('Module:no globals')


local function MakeLine(id,id2,sep)
    local list = {}
    local cnt = 0
    id2 = id2 or 9
    id2 = id2>id and id2 or id+id2-1
    for j=id,id2 do
        cnt = cnt + 1
        list[cnt] = Link(Data[j])
    end
    local result = table.concat(list, sep or "·")
    return result
end

p.series = Util['@frame'](function(key, cnt, sep) --=p.series('木针匣')
	local item = Data[key]
	local id = item.id
	sep = sep or '·'
    if cnt then return MakeLine(id, tonumber(cnt), sep) end
    -- 未设定数量,根据id和品级自动判断
	local first = id
	local last = id
	local oldLevel = item[8]
	while true do
		id = id + 1
		item = Data[id]
		if item==nil then break end
		local newLevel = item[8]
		if newLevel < oldLevel then break end
		oldLevel = newLevel
		last = id
	end
	return MakeLine(first, last, sep)
end, {sep=3})


local AllOthers = {
    '工具',
    '材料',
    '毒物',
    '毒药',
    '药材',
    '丹药',
    '蛰罐',
    '食材',
    '素食',
    '荤食',
    '神兵',
    '茶酒',
    '宝典',
    '特殊',
    '西域',
    '其他',
}

local AllEquips = {
	"针匣",
	"对刺",
	"暗器",
	"箫笛",
	"掌套",
	"短杵",
	"拂尘",
	"长鞭",
	"剑",
	"刀",
	"长兵",
	"瑶琴",
	"宝物",
	"冠饰",
	"鞋子",
	"护甲",
	"衣着",
	"代步",
	'绳索',
	'副产物'
}

p.all = Util['@frame'](function(labels)
    labels = labels and mw.text.split(labels, ' ', true)
	if labels and #labels==1 then
		if Util.contains(AllEquips,labels) then
			return require('Module:Item/nav/equip')(labels[1])
		else
			return require('Module:Item/nav/other')(labels[1])
		end
    end

    local filterDiv = FilterDiv{{'装备', '其他'}}
    local version = mw.huiji.db.findOne{_id='Data:Version.json'}.Item

    for _, typ in ipairs(AllEquips) do
        if labels==nil or Util.contains(labels, typ) then
            filterDiv:add({
        		lazyload = '{{nowiki|#invoke:Item|equip|'..typ..'|_='.. version ..'}}'
        	}, typ, '')
        end
	end
	for _, typ in ipairs(AllOthers) do
        if labels==nil or Util.contains(labels, typ) then
            filterDiv:add({
        		lazyload = '{{nowiki|#invoke:Item|other|'..typ..'|_='.. version ..'}}'
        	}, '', typ)
        end
    end

    return filterDiv
end)

function p.gangActor(rawText, levels)
	local list = Linq(mw.text.split(rawText, '|', true))
        :Select(function(pair) return tonumber(mw.text.split(pair, '&', true)[1]) end)
        :Where(function(id) return id>0 end)
        :SelectMany(function(id)
            local baseItem = Data[id]
            if baseItem==nil then error(id) end
            local isMedicine = baseItem[5]==31
            return Linq(levels)
                :Select(function(level)
                    return isMedicine and Util.clamp(math.floor(level/2+1),0,5) or Util.clamp(level,0,8)
                end)
                :Distinct()
                :Select(function(addLevel) return id+addLevel end)
                :Done()
        end)
        :Select(function(id)
            local item = Data[id]
            return {
                link = Link(item),
                title = Const.item[item[5]]
            }
        end)
        :Done()
	return #list>0 and require('Module:Output').navbox(list) or '无'
end

return p
avatar