require('strict')

local config = {
	top = {
		image = 'Importance top.svg', alt = '极高重要度',
		aliases = {'极高', '極高'}
	},
	
	high = {
		image = 'Importance high.svg', alt = '高重要度',
		aliases = {'高'}
	},

	mid = {
		image = 'Importance mid.svg', alt = '中重要度',
		aliases = {'中'}
	},

	low = {
		image = 'Importance low.svg', alt = '低重要度',
		aliases = {'低'}
	},

	bottom = {
		image = 'Importance bottom.svg', alt = '极低重要度',
		aliases = {'极低', '極低'}
	},

	no = {
		image = 'Importance no.svg', alt = '无重要度',
		aliases = {'无', '無'}
	},

	na = {
		image = 'Importance na.svg', alt = '不适用重要度',
		aliases = {'不适用', '不適用'}
	},

	unknown = {
		image = 'Importance unknown.svg', alt = '未知重要度',
		aliases = {'未知', '未评', '未評', ''},
	},
}

local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.main(frame)
    local args = getArgs(frame)
    return p._main(args)
end

function p._main(args)
	local ret = ''
	local class
	
	-- 预处理输入等级
	local input = args.class or args[1]
	if input == nil then
		input = 'unknown'
	else
		input = mw.ustring.lower(input)
	end
	
	-- 获取输入等级
	for k, _ in pairs(config) do
		if k == input then
			class = k
			break
		end
	end
	
	if class == nil then
		for k, v in pairs(config) do
			for _, w in pairs(v.aliases) do
				if w == input then
					class = k
					break
				end
			end
			if class ~= nil then break end
		end
	end
	
	-- 获取评级图示
	if class == nil then
		ret = mw.getCurrentFrame():expandTemplate{ title = 'icon', args = {args[1]} }
	else
		ret = '[[File:' .. config[class].image .. '|' .. config[class].alt .. '|16px]]'
	end
	
	if args['css-class'] or args.style then
		local span = ''
		if args['css-class'] then
			span = span .. ' class="' .. args['css-class'] .. '"'
		end
		if args.style then
			span = span .. ' style="' .. args.style .. '"'
		end
		ret = '<span' .. span .. '>' .. ret .. '</span>'
	end

	-- RETURN
    return ret
end

return p