local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
local m_common = require("Module:urj-fin-common")

local export = {}

local lang = require("Module:languages").getByCode("urj-fin-pro")

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

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local args = frame:getParent().args
	
	local data = {forms = {}, title = nil, categories = {}}
	
	-- Generate the forms
	inflect(args, data)
	
	-- Apply gradation
	for key, forms in pairs(data.forms) do
		for i, form in ipairs(forms) do
			forms[i] = m_common.apply_gradation(form)
		end
	end
	
	-- Check if the lemma form matches the page name
	if data.forms["nom_sg"] and (lang:makeEntryName(data.forms["nom_sg"][1])) ~= mw.title.getCurrentTitle().subpageText then
		table.insert(data.categories, lang:getCanonicalName() .. " entries with inflection not matching pagename")
	end
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

function inflect(args, data)
	local prefix = args[1] or ""
	local stem = args[2] or ""
	
	if stem == "" then
		stem = prefix
		prefix = ""
	end
	
	if stem == "" then
		if mw.title.getCurrentTitle().nsText == "Template" then
			stem = "-"
		else
			error("Parameter 1 (stem) must be provided")
		end
	end
	
	local vowels = m_common.detect_harmony(stem)
	local stems = m_common.make_stems((mw.ustring.gsub(stem, "([aeiouäëöü])n[eë]n$", "%1c")))
	
	if mw.ustring.find(stem, "[aeiouäëöü]n[eë]n$") then
		stems.final = stem
	end
	
	make_forms(stems, vowels, data)
	
	if mw.ustring.find(stem, "^[mc]inu$") then
		data.forms["nom_sg"] = {(mw.ustring.gsub(stem, "u$", "ä"))}
		
		for key, form in pairs(data.forms) do
			if key:find("_pl") then
				data.forms[key] = nil
			end
		end
	elseif mw.ustring.find(stem, "^[mth]e$") then
		data.forms["nom_pl"] = {stem, (mw.ustring.gsub(stem, "e$", "öö"))}
		data.forms["acc_pl"] = {stem .. "det", stem .. "idet"}
		
		for key, form in pairs(data.forms) do
			if key:find("_sg") then
				data.forms[key] = nil
			end
		end
	elseif stem == "hän" then
		data.forms["nom_sg"] = {"hän"}
		
		for key, form in pairs(data.forms) do
			if key:find("_pl") then
				data.forms[key] = nil
			end
		end
	end
	
	for key, subforms in pairs(data.forms) do
		for i, form in ipairs(subforms) do
			subforms[i] = prefix .. form
		end
	end
	
	for _, t in ipairs(stems.types) do
		table.insert(data.categories, "Proto-Finnic nominals by type/" .. t)
	end
end

function make_forms(stems, vowels, data)
	data.forms["nom_sg"] = {stems.final}
	data.forms["acc_sg"] = {stems.normal .. "n"}
	data.forms["gen_sg"] = {stems.normal .. "n"}
	data.forms["par_sg"] = {stems.t .. vowels.a}
	data.forms["ine_sg"] = {stems.normal .. "ss" .. vowels.a, stems.normal .. "hn" .. vowels.a}
	data.forms["ela_sg"] = {stems.normal .. "st" .. vowels.a}
	data.forms["ill_sg"] = {stems.normal .. "s" .. vowels.e .. "n"}
	data.forms["ade_sg"] = {stems.normal .. "ll" .. vowels.a}
	data.forms["abl_sg"] = {stems.normal .. "lt" .. vowels.a}
	data.forms["all_sg"] = {stems.normal .. "l" .. vowels.e .. "n", stems.normal .. "l" .. vowels.e .. "k"}
	data.forms["ess_sg"] = {stems.n .. vowels.a}
	data.forms["tra_sg"] = {stems.normal .. "ksi"}
	data.forms["com_sg"] = {stems.n .. vowels.e .. "k"}
	data.forms["ins_sg"] = {stems.normal .. "n"}
	data.forms["abe_sg"] = {stems.normal .. "tt" .. vowels.a}
	
	data.forms["nom_pl"] = {stems.normal .. "t"}
	data.forms["acc_pl"] = {stems.normal .. "t"}
	data.forms["gen_pl"] = {stems.t .. vowels.e .. "n", stems.i .. "t" .. vowels.e .. "n"}
	data.forms["par_pl"] = {stems.i .. "t" .. vowels.a}
	data.forms["ine_pl"] = {stems.i .. "ss" .. vowels.a, stems.i .. "hn" .. vowels.a}
	data.forms["ela_pl"] = {stems.i .. "st" .. vowels.a}
	data.forms["ill_pl"] = {stems.i .. "s" .. vowels.e .. "n"}
	data.forms["ade_pl"] = {stems.i .. "ll" .. vowels.a}
	data.forms["abl_pl"] = {stems.i .. "lt" .. vowels.a}
	data.forms["all_pl"] = {stems.i .. "l" .. vowels.e .. "n", stems.i .. "l" .. vowels.e .. "k"}
	data.forms["ess_pl"] = {stems.i .. "n" .. vowels.a}
	data.forms["tra_pl"] = {stems.i .. "ksi"}
	data.forms["com_pl"] = {stems.i .. "n" .. vowels.e .. "k"}
	data.forms["ins_pl"] = {stems.i .. "n"}
	data.forms["abe_pl"] = {stems.i .. "tt" .. vowels.a}
end


-- Functions for generating the inflection table

function show_form(form)
	if not form then
		return "—"
	elseif type(form) ~= "table" then
		error("a non-table value was given in the list of inflected forms.")
	end
	
	local ret = {}
	
	for key, subform in ipairs(form) do
		table.insert(ret, m_links.full_link({lang = lang, alt = "*" .. subform}))
	end
	
	return table.concat(ret, "<br/>")
end

-- Make the table
function make_table(data)
	return [=[<div class="NavFrame" style="width: 75%">
<div class="NavHead" style="background:rgb(80%,80%,100%);">การผันรูปของ ]=] .. m_links.full_link({lang = lang, alt = "*" .. mw.title.getCurrentTitle().subpageText}, "term") .. (data.title and " (" .. data.title .. ")" or "") .. [=[</div>
<div class="NavContent">
{| class="inflection-table" style="width:100%; color:rgb(0%,0%,30%); border:solid 1px rgb(80%,80%,100%); text-align:left;" cellspacing="1" cellpadding="2"
| colspan="4" | '''หมายเหตุ''': ระบบการผันรูปของภาษาฟินนิกดั้งเดิมยังไม่ได้รับการสืบสร้างโดยละเอียด สิ่งที่นำเสนอนี้เป็นเพียงความเป็นไปได้หนึ่งเท่านั้น
|-
|- style="background:rgb(80%,80%,100%);vertical-align:top;"
! style="width:33%;" colspan="2" |
! style="width:33%;" | เอกพจน์
! style="width:33%;" | พหูพจน์
|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | nominative
| ]=] .. show_form(data.forms["nom_sg"]) .. [=[

| ]=] .. show_form(data.forms["nom_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" | 
! style="background:rgb(80%,80%,100%);" colspan="2" | accusative
| ]=] .. show_form(data.forms["acc_sg"]) .. [=[

| ]=] .. show_form(data.forms["acc_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | genitive
| ]=] .. show_form(data.forms["gen_sg"]) .. [=[

| ]=] .. show_form(data.forms["gen_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | partitive
| ]=] .. show_form(data.forms["par_sg"]) .. [=[

| ]=] .. show_form(data.forms["par_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | inessive
| ]=] .. show_form(data.forms["ine_sg"]) .. [=[

| ]=] .. show_form(data.forms["ine_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | elative
| ]=] .. show_form(data.forms["ela_sg"]) .. [=[

| ]=] .. show_form(data.forms["ela_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | illative
| ]=] .. show_form(data.forms["ill_sg"]) .. [=[

| ]=] .. show_form(data.forms["ill_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | adessive
| ]=] .. show_form(data.forms["ade_sg"]) .. [=[

| ]=] .. show_form(data.forms["ade_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | ablative
| ]=] .. show_form(data.forms["abl_sg"]) .. [=[

| ]=] .. show_form(data.forms["abl_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | allative
| ]=] .. show_form(data.forms["all_sg"]) .. [=[

| ]=] .. show_form(data.forms["all_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | essive
| ]=] .. show_form(data.forms["ess_sg"]) .. [=[

| ]=] .. show_form(data.forms["ess_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | translative
| ]=] .. show_form(data.forms["tra_sg"]) .. [=[

| ]=] .. show_form(data.forms["tra_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | instructive
| ]=] .. show_form(data.forms["ins_sg"]) .. [=[

| ]=] .. show_form(data.forms["ins_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | comitative
| ]=] .. show_form(data.forms["com_sg"]) .. [=[

| ]=] .. show_form(data.forms["com_pl"]) .. [=[

|- style="background:rgb(95%,95%,100%);vertical-align:top;" |
! style="background:rgb(80%,80%,100%);" colspan="2" | abessive
| ]=] .. show_form(data.forms["abe_sg"]) .. [=[

| ]=] .. show_form(data.forms["abe_pl"]) .. [=[

|}
</div>
</div>]=]

end

return export