local m_links = require("Module:links")
local m_utils = require("Module:utilities")

local lang = require("Module:languages").getByCode("li")
local export = {}

-- Functions that do the actual inflecting by creating the forms of a basic term.
local inflections = {}
local irregular = {}

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local infl_type = frame.args[1] == nil and "regular" or frame.args[1]
	
	if not inflections[infl_type] then
		error("Unknown inflection type '" .. infl_type .. "'")
	end
	
	local data = {forms = {}, categories = {}}
	data.info = ""
	
	local args = require("Module:parameters").process(frame:getParent().args, inflections[infl_type].params)
	
	-- Generate the forms
	inflections[infl_type].func(args, data)
	
	return make_table(data)
end

inflections["regular"] = {
	params = {
		[1] = {},
		[2] = {},
		["doubled"] = {}
		},
	func = function(args, data)
		local base = mw.title.getCurrentTitle().nsText == "Template" and "-" or mw.title.getCurrentTitle().text
		-- Final consonant is doubled in certain cases to show a short vowel (like English and Dutch)
		-- If the stem ends in -e, don't add another one
		local doubled = args["doubled"] ~= "" and args["doubled"] or (base:find("e$") and base:sub(1, -2) or base)
		
		data.forms["nom_sg_m"] = doubled .. "e"
		data.forms["nom_pl_m"] = base
		data.forms["nom_sg_f"] = base
		data.forms["nom_pl_f"] = base
		data.forms["nom_sg_n"] = base
		data.forms["nom_pl_n"] = base
		
		data.forms["gen_sg_m"] = doubled:sub(-1) == "s" and doubled .. "es" or doubled .. "s"
		data.forms["gen_pl_m"] = doubled .. "er"
		data.forms["gen_sg_f"] = doubled .. "er"
		data.forms["gen_pl_f"] = doubled .. "er"
		data.forms["gen_sg_n"] = doubled:sub(-1) == "s" and doubled .. "es" or doubled .. "s"
		data.forms["gen_pl_n"] = doubled .. "er"
		
		data.forms["loc_sg_m"] = doubled .. "es"
		data.forms["loc_pl_m"] = doubled .. "es"
		data.forms["loc_sg_f"] = doubled .. "es"
		data.forms["loc_pl_f"] = doubled .. "es"
		data.forms["loc_sg_n"] = doubled .. "es"
		data.forms["loc_pl_n"] = doubled .. "es"
		
		data.forms["voc_all"] = base
		
		data.forms["dat_sg_m"] = doubled .. "e"
		data.forms["dat_pl_m"] = doubled .. "en"
		data.forms["dat_sg_f"] = doubled .. "er"
		data.forms["dat_pl_f"] = doubled .. "en"
		data.forms["dat_sg_n"] = base
		data.forms["dat_pl_n"] = doubled .. "en"
		
		data.forms["acc_sg_m"] = doubled .. "e"
		data.forms["acc_pl_m"] = doubled .. "en"
		data.forms["acc_sg_f"] = base
		data.forms["acc_pl_f"] = base
		data.forms["acc_sg_n"] = base
		data.forms["acc_pl_n"] = doubled .. "en"
	end
}

inflections["irregular"] = {
	params = {
		[1] = {},
		[2] = {}
		},
	func = function(args, data)
		local base = args[2] or (mw.title.getCurrentTitle().nsText == "Template" and "good" or mw.title.getCurrentTitle().text)
		
		if irregular[base] then
			irregular[base](data)
		else
			error("Unknown irregular adjective '" .. base .. "'.")
		end
	end
}

irregular["good"] = function(data)
	table.insert(data.categories, "林堡語不規則形容詞")
	data.info = "(不規則)"

	data.forms["nom_sg_m"] = "gooje"
	data.forms["nom_pl_m"] = "gooj"
	data.forms["nom_sg_f"] = "gooj"
	data.forms["nom_pl_f"] = "gooj"
	data.forms["nom_sg_n"] = "good"
	data.forms["nom_pl_n"] = "gooj"
	
	data.forms["gen_sg_m"] = "goods"
	data.forms["gen_pl_m"] = "goojer"
	data.forms["gen_sg_f"] = "goojer"
	data.forms["gen_pl_f"] = "goojer"
	data.forms["gen_sg_n"] = "goods"
	data.forms["gen_pl_n"] = "goojer"
	
	data.forms["loc_sg_m"] = "goojes"
	data.forms["loc_pl_m"] = "gooje"
	data.forms["loc_sg_f"] = "goojes"
	data.forms["loc_pl_f"] = "gooje"
	data.forms["loc_sg_n"] = "goojes"
	data.forms["loc_pl_n"] = "gooje"
	
	data.forms["voc_all"] = "gooj"
	
	data.forms["dat_sg_m"] = "gooje"
	data.forms["dat_pl_m"] = "goojen"
	data.forms["dat_sg_f"] = "goojer"
	data.forms["dat_pl_f"] = "goojen"
	data.forms["dat_sg_n"] = "good"
	data.forms["dat_pl_n"] = "goojen"
		
	data.forms["acc_sg_m"] = "gooje"
	data.forms["acc_pl_m"] = "goojen"
	data.forms["acc_sg_f"] = "good"
	data.forms["acc_pl_f"] = "gooj"
	data.forms["acc_sg_n"] = "good"
	data.forms["acc_pl_n"] = "goojen"
end

-- Make the table
function make_table(data)
	local function show_form(form)
		if not form then
			return "—"
		end
		
		local ret = {}
		
		table.insert(ret, m_links.full_link({lang = lang, term = form}))
			
		return table.concat(ret, "<br/>")
	end
	
	local function repl(param)
		if param == "lemma" then
			return m_links.full_link({lang = lang, alt = mw.title.getCurrentTitle().text}, "term")
		elseif param == "info" then
			return data.info
		else
			return show_form(data.forms[param])
		end
	end
	
	local wikicode = [=[
<div class="NavFrame">
<div class="NavHead" style="text-align: left">{{{lemma}}}{{{info}}} 的變格</div>
<div class="NavContent">
{| border="1px solid #cdcdcd" style="border-collapse:collapse; background:#FEFEFE; width:100%" class="inflection-table"
|-
! rowspan="2" style="background:#C0C0C0" | 數 & 性
! colspan="2" style="background:#C0C0C0" | 陽性
! colspan="2" style="background:#C0C0C0" | 陰性
! colspan="2" style="background:#C0C0C0" | 中性
|-
! style="background:#C0C0C0" | 單數
! style="background:#C0C0C0" | 複數
! style="background:#C0C0C0" | 單數
! style="background:#C0C0C0" | 複數
! style="background:#C0C0C0" | 單數
! style="background:#C0C0C0" | 複數
|-
! style="background:#c0cfe4" | 主格
| {{{nom_sg_m}}}
| {{{nom_pl_m}}}
| {{{nom_sg_f}}}
| {{{nom_pl_f}}}
| {{{nom_sg_n}}}
| {{{nom_pl_n}}}
|-
! style="background:#c0cfe4" | 屬格
| {{{gen_sg_m}}}
| {{{gen_pl_m}}}
| {{{gen_sg_f}}}
| {{{gen_pl_f}}}
| {{{gen_sg_n}}}
| {{{gen_pl_n}}}
|-
! style="background:#c0cfe4" | 方位格
| {{{loc_sg_m}}}
| {{{loc_pl_m}}}
| {{{loc_sg_f}}}
| {{{loc_pl_f}}}
| {{{loc_sg_n}}}
| {{{loc_pl_n}}}
|-
! style="background:#c0cfe4" | 呼格<sup>1</sup>
| colspan="6" | {{{voc_all}}}
|-
! style="background:#c0cfe4 | 與格<sup>2</sup>
| {{{dat_sg_m}}}
| {{{dat_pl_m}}}
| {{{dat_sg_f}}}
| {{{dat_pl_f}}}
| {{{dat_sg_n}}}
| {{{dat_pl_n}}}
|-
! style="background:#c0cfe4" | 賓格<sup>2</sup>
| {{{acc_sg_m}}}
| {{{acc_pl_m}}}
| {{{acc_sg_f}}}
| {{{acc_pl_f}}}
| {{{acc_sg_n}}}
| {{{acc_pl_n}}}
|-
| colspan="7" style="border:none;text-align:left" | <sup style="margin-left:20px">1</sup>僅限於少數古舊形式。
|-
| colspan="7" style="border:none;text-align:left" | <sup style="margin-left:20px">2</sup>現在已經棄用,而改用主格。
|}</div></div>]=]
	
	return (mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl)) .. m_utils.format_categories(data.categories, lang)
end

return export