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

local m_infl =  require("Module:fur-conj/data")

local lang = require("Module:languages").getByCode("fur")

local export = {}

local function make_table(data)
	local function show_form(form)
		if not form then
			return "–"
		end
		
		local ret = {}
		
		for key, subform in ipairs(form) do
			table.insert(ret, m_links.full_link({lang = lang, term = subform}))
		end
			
		return table.concat(ret, ", ")
	end
	local function repl(param)
		if param == "conj" then
			return data.conj
		else
			return show_form(data.forms[param])
		end
	end
	local wikicode = [=[{{{comment}}}
<div class="NavFrame">
<div class="NavHead" align=left>{{{inf}}} 的變位({{{conj}}})</div>
<div class="NavContent">
{| style="background:#F0F0F0;text-align:center;width:100%;margin:auto;"
|-
! colspan="3" style="background:#e2e4c0" | 不定式
| colspan="5" | {{{inf}}}
|-
! colspan="3" style="background:#e2e4c0" | 動名詞
| colspan="5" | {{{gerund}}}
|-
! colspan="2" rowspan="3" style="background:#e2e4c0" | 過去分慈
! colspan="2" style="background:#e2e4c0" |
! colspan="2" style="background:#e2e4c0" | 單數
! colspan="2" style="background:#e2e4c0" | 複數
|-
! colspan="2" style="background:#e2e4c0" | 陽性
| colspan="2" | {{{m_past_part}}}
| colspan="2" | {{{m_pl_past_part}}}
|-
! colspan="2" style="background:#e2e4c0" | 陰性
| colspan="2" | {{{f_past_part}}}
| colspan="2" | {{{f_pl_past_part}}}
|-
! colspan="2" rowspan="2" style="background:#C0C0C0" | 人稱
! colspan="3" style="background:#C0C0C0" | 單數
! colspan="3" style="background:#C0C0C0" | 複數
|-
! style="background:#C0C0C0;width:12.5%" | 第一人稱
! style="background:#C0C0C0;width:12.5%" | 第二人稱
! style="background:#C0C0C0;width:12.5%" | 第三人稱
! style="background:#C0C0C0;width:12.5%" | 第一人稱
! style="background:#C0C0C0;width:12.5%" | 第二人稱
! style="background:#C0C0C0;width:12.5%" | 第三人稱
|-
! rowspan="5" style="background:#c0cfe4" | 直陳語氣
! style="background:#c0cfe4" colspan="1" |
! style="background:#c0cfe4" | jo
! style="background:#c0cfe4" | tu
! style="background:#c0cfe4" | lui/jê
! style="background:#c0cfe4" | nô
! style="background:#c0cfe4" | vô
! style="background:#c0cfe4" | lôr
|-
! style="height:3em;background:#c0cfe4" | 現在時
| o {{{pres_ind_1sg}}}
| tu {{{pres_ind_2sg}}}
| al/e {{{pres_ind_3sg}}}
| o {{{pres_ind_1pl}}}
| o {{{pres_ind_2pl}}}
| a {{{pres_ind_3pl}}}
|-
! style="height:3em;background:#c0cfe4" | 未完成時
| o {{{impf_ind_1sg}}}
| tu {{{impf_ind_2sg}}}
| al/e {{{impf_ind_3sg}}}
| o {{{impf_ind_1pl}}}
| o {{{impf_ind_2pl}}}
| a {{{impf_ind_3pl}}}
|-
! style="height:3em;background:#c0cfe4" | 簡單過去時
| o {{{past_ind_1sg}}}
| tu {{{past_ind_2sg}}}
| al/e {{{past_ind_3sg}}}
| o {{{past_ind_1pl}}}
| o {{{past_ind_2pl}}}
| a {{{past_ind_3pl}}}
|-
! style="height:3em;background:#c0cfe4" | 未來時
| o {{{futr_ind_1sg}}}
| tu {{{futr_ind_2sg}}}
| al/e {{{futr_ind_3sg}}}
| o {{{futr_ind_1pl}}}
| o {{{futr_ind_2pl}}}
| a {{{futr_ind_3pl}}}
|-
! rowspan="2" style="background:#c0d8e4" | 條件語氣
! style="background:#c0d8e4" colspan="1" |
! style="background:#c0d8e4" | jo
! style="background:#c0d8e4" | tu
! style="background:#c0d8e4" | lui/jê
! style="background:#c0d8e4" | nô
! style="background:#c0d8e4" | vô
! style="background:#c0d8e4" | lôr
|-
! style="height:3em;background:#c0d8e4" | 現在時
| o {{{pres_con_1sg}}}
| tu {{{pres_con_2sg}}}
| al/e {{{pres_con_3sg}}}
| o {{{pres_con_1pl}}}
| o {{{pres_con_2pl}}}
| a {{{pres_con_3pl}}}
|-
! rowspan="3" style="background:#c0e4c0" | 虛擬語氣
! style="background:#c0e4c0" colspan="1" |
! style="background:#c0e4c0" | jo
! style="background:#c0e4c0" | tu
! style="background:#c0e4c0" | lui/jê
! style="background:#c0e4c0" | nô
! style="background:#c0e4c0" | vô
! style="background:#c0e4c0" | lôr
|-
! style="height:3em;background:#c0e4c0" | 現在時
| o {{{pres_sub_1sg}}}
| tu {{{pres_sub_2sg}}}
| al/e {{{pres_sub_3sg}}}
| o {{{pres_sub_1pl}}}
| o {{{pres_sub_2pl}}}
| a {{{pres_sub_3pl}}}
|-
! style="height:3em;background:#c0e4c0" | 未完成體
| o {{{impf_sub_1sg}}}
| tu {{{impf_sub_2sg}}}
| al/e {{{impf_sub_3sg}}}
| o {{{impf_sub_1pl}}}
| o {{{impf_sub_2pl}}}
| a {{{impf_sub_3pl}}}
|-
! rowspan="3" colspan="2" style="background:#e4d4c0" | 命令式
! style="background:#e4d4c0" | –
! style="background:#e4d4c0" | tu
! style="background:#e4d4c0" | –
! style="background:#e4d4c0" | nô
! style="background:#e4d4c0" | vô
! style="background:#e4d4c0" | –
|-
| –
| {{{imp_2sg}}}
| –
| {{{imp_1pl}}}
| {{{imp_2pl}}}
| –
|}
</div></div>]=]
	return mw.ustring.gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl)
end

-- Main entry point
function export.show(frame)
	local args = mw.clone(frame:getParent().args)
	
	-- Create the forms
	local data = {forms = {}, categories = {}}
	
	if mw.title.getCurrentTitle().nsText ~= "" then return end
	
	args[1] = mw.title.getCurrentTitle().text
	
	local last2 = mw.ustring.sub(args[1], -2)
	local ending = mw.ustring.sub(args[1], -1)
	if m_infl.irregular[args[1]] then
		m_infl.irregular[args[1]](args, data)
	elseif m_infl[last2] then
		args[1] = mw.ustring.sub(args[1], 1, -3)
		m_infl[last2](args, data)
	elseif m_infl[ending] then
		args[1] = mw.ustring.sub(args[1], 1, -2)
		m_infl[ending](args, data)
	else
		error("Inflection for " .. word .. " not found.")
	end
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

return export