local export = {}
local pos_functions = {}
local m_scripts = require("Module:scripts")

local lang = require("Module:languages").getByCode("lad")
local langname = lang:getCanonicalName()

local PAGENAME = mw.title.getCurrentTitle().text

local suffix_categories = {
	["形容詞"] = true,
	["副詞"] = true,
	["名詞"] = true,
	["動詞"] = true,
}

local function track(page)
	require("Module:debug").track("lad-headword/" .. page)
	return true
end

local function glossary_link(entry, text)
	text = text or entry
	return "[[Appendix:Glossary#" .. entry .. "|" .. text .. "]]"
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local tracking_categories = {}
	
	local poscat = frame.args[1]
		or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local params = {
		["head"] = {list = true},
		["lat"] = {list = true}, --Latin equivalent(s)
		["heb"] = {list = true}, --Hebrew equivalent(s)
		["suff"] = {type = "boolean"},
	}

	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end
	
    local parargs = frame:getParent().args
	local args = require("Module:parameters").process(parargs, params)
	local data = {
		lang = lang,
		pos_category = poscat,
		sccat = true, -- add e.g. [[:Category:以希伯來文書寫的拉蒂諾語名詞]]]
		categories = {},
		heads = args["head"],
		genders = {},
		inflections = {},
		categories = {}
	}
	
	if args["suff"] then
		data.pos_category = "後綴"
		
		if suffix_categories[poscat] then
			local singular_poscat = poscat:gsub("s$", "")
			table.insert(data.categories, langname .. "構成" .. singular_poscat .. "的後綴")
		else
			error("No category exists for suffixes forming " .. poscat .. ".")
		end
	end
	
	data.sc = lang:findBestScript(data.heads[1] or PAGENAME)
	data.is_heb = data.sc:getCode() == "Hebr"
	data.is_lat = data.sc:getCode() == "Latn"

	if data.is_heb then
		table.insert(data.inflections, {label = "[[Appendix:希伯來字母|希伯來字母]]"})
	elseif data.is_lat then
		table.insert(data.inflections, {label = "[[Appendix:拉丁字母|拉丁字母]]"})
	end

	if #args.lat > 0 then
		args.lat.label = "[[Appendix:拉丁字母|拉丁字母]]"
		args.lat.sc = m_scripts.getByCode("Latn")
		table.insert(data.inflections, args.lat)
	end

	if #args.heb > 0 then
		args.heb.label = "[[Appendix:希伯來字母|希伯來字母]]"
		args.heb.sc = m_scripts.getByCode("Hebr")
		table.insert(data.inflections, args.heb)
	end

	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data, tracking_categories)
	end
	
	return require("Module:headword").full_headword(data)
		.. require("Module:utilities").format_categories(tracking_categories, lang)
end

pos_functions["形容詞"] = {
	params = {
		["pl"] = {list = true}, --plural(s)
		["f"] = {list = true}, --feminine form(s)
		["fpl"] = {list = true}, --feminine plural(s)
		["m"] = {list = true}, --masculine form(s)
		["mpl"] = {list = true}, --masculine plural(s)
	},
	func = function(args, data, tracking_categories)
		if #args.m > 0 then
			args.m.label = "陽性"
			table.insert(data.inflections, args.m)
		end
	
		if #args.f > 0 then
			args.f.label = "陰性"
			table.insert(data.inflections, args.f)
		end
	
		if #args.pl > 0 then
			args.pl.label = "複數"
			table.insert(data.inflections, args.pl)
		end
	
		if #args.mpl > 0 then
			args.mpl.label = "陽性複數"
			table.insert(data.inflections, args.mpl)
		end
	
		if #args.fpl > 0 then
			args.fpl.label = "陰性複數"
			table.insert(data.inflections, args.fpl)
		end
	end
}

local noun_params = {
	["g"] = {list = true}, --gender(s)
	["pl"] = {list = true}, --plural(s)
	["f"] = {list = true}, --feminine form(s)
	["fpl"] = {list = true}, --feminine plural(s)
	["m"] = {list = true}, --masculine form(s)
	["mpl"] = {list = true}, --masculine plural(s)
}

local allowed_genders = {
	["m"] = true,
	["f"] = true,
	["m-p"] = true,
	["f-p"] = true,
}

local function do_nouns(pos, args, data, tracking_categories)
	for _, g in ipairs(args.g) do
		if not allowed_genders[g] then
			error("無法識別的性別:" .. g)
		end
	end

	local plpos = require("Module:string utilities").pluralize(pos)

	data.genders = args.g

	-- Check for special plural signals
	local mode = nil
	
	if args.pl[1] == "?" or args.pl[1] == "!" or args.pl[1] == "-" or args.pl[1] == "~" or args.pl[1] == "#" then
		mode = args.pl[1]
		table.remove(args.pl, 1)  -- Remove the mode parameter
	end
	
	if mode == "?" then
		-- Plural is unknown
		table.insert(data.categories, langname .. " " .. plpos .. " with unknown or uncertain plurals")
	elseif mode == "!" then
		-- Plural is not attested
		table.insert(data.inflections, {label = "未有複數形式的紀錄"})
		table.insert(data.categories, langname .. " " .. plpos .. " with unattested plurals")
		return
	elseif mode == "-" then
		-- Uncountable noun; may occasionally have a plural
		table.insert(data.categories, langname .. "不可數" .. plpos)
		table.insert(data.inflections, {label = "usually " .. glossary_link("uncountable")})
	elseif mode == "~" then
		-- Mixed countable/uncountable noun, always has a plural
		table.insert(data.inflections, {label = glossary_link("可數") .. " and " .. glossary_link("不可數")})
		table.insert(data.categories, langname .. "不可數" .. plpos)
		table.insert(data.categories, langname .. "可數" .. plpos)
	elseif mode == "#" or pos == "名詞" then
		-- Countable nouns; the default for regular nouns but not proper nouns
		if mode == "#" then
			table.insert(data.inflections, {label = glossary_link("可數")})
		end
		-- Not until we're sure that all nouns properly use pl=-
		-- table.insert(data.categories, langname .. "可數" .. plpos)
	end

	if #args.pl > 0 then
		args.pl.label = "複數"
		args.pl.accel = {form = "p"}
		table.insert(data.inflections, args.pl)
	end

	if #args.f > 0 then
		args.f.label = "陰性"
		table.insert(data.inflections, args.f)
	end

	if #args.fpl > 0 then
		args.fpl.label = "陰性複數"
		table.insert(data.inflections, args.fpl)
	end

	if #args.m > 0 then
		args.m.label = "陽性"
		table.insert(data.inflections, args.m)
	end

	if #args.mpl > 0 then
		args.mpl.label = "陽性複數"
		table.insert(data.inflections, args.mpl)
	end
end

-- Display additional inflection information for a noun
pos_functions["名詞"] = {
	params = noun_params,
	func = function(args, data, tracking_categories)
		return do_nouns("名詞", args, data, tracking_categories)
	end,
}

pos_functions["專有名詞"] = {
	params = noun_params,
	func = function(args, data, tracking_categories)
		return do_nouns("專有名詞", args, data, tracking_categories)
	end,
}

pos_functions["代詞"] = {
	params = {
		["g"] = {list = true}, --gender(s)
	},
	func = function(args, data, tracking_categories)
		for _, g in ipairs(args.g) do
			if not allowed_genders[g] then
				error("無法識別的性別:" .. g)
			end
		end
	
		data.genders = args.g
	end
}

return export