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

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

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

local export = {}

-- Shows forms with links, or a dash if empty
local function show_form(subforms)
	if not subforms then
		return "—"
	elseif type(subforms) ~= "table" then
		error("a non-table value was given in the list of inflected forms.")
	elseif #subforms == 0 then
		return "—"
	end
	
	local ret = {}
	
	-- Go over each subform and insert links
	for key, subform in ipairs(subforms) do
		table.insert(ret, m_links.full_link({lang = lang, term = subform}))
	end
	
	return table.concat(ret, ", ")
end

-- Shows the table with the given forms
local function make_table(data)
	local ret = {[=[* 拉登語的變位形式因地區而異。因此,以下表格僅是一種典型的變位,而非詳盡的變位列表。
<div class="NavFrame" style="clear:both; margin-top:1em">
<div class="NavHead" style="text-align: left">]=]}
	table.insert(ret, m_links.full_link({lang = lang, alt = mw.title.getCurrentTitle().text}, "term") .. " 的變位 (" .. data.conj ..  ")</div>\n")
	table.insert(ret, [=[<div class="NavContent">
{| style="background:#F0F0F0;border-collapse:separate;border-spacing:2px;width:100%" class="inflection-table"
|-
! colspan="1" style="background:#e2e4c0" | 不定式
| colspan="1" | ]=])
	table.insert(ret, show_form(data.forms.inf) .. "\n")
	table.insert(ret, [=[
|-
! colspan="2" style="background:#e2e4c0" | 助動詞
| colspan="1" | ]=])
	table.insert(ret, show_form(data.forms.aux) .. "\n")
	table.insert(ret, [=[
! colspan="2" style="background:#e2e4c0" | 動名詞
| colspan="2" | ]=])
	table.insert(ret, show_form(data.forms.gerund) .. "\n")
	table.insert(ret, [=[
|-
! colspan="2" style="background:#e2e4c0" | 過去分詞
| colspan="2" | ]=])
	table.insert(ret, show_form(data.forms.past_part) .. "\n")
	table.insert(ret, [=[
|-
! colspan="1" 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%" | 第三人稱

|-
! style="background:#c0cfe4" colspan="1" | 直陳語態
! style="background:#c0cfe4" | ie
! style="background:#c0cfe4" | tu 
! style="background:#c0cfe4" | el / ela 
! style="background:#c0cfe4" | nos
! style="background:#c0cfe4" | vos
! style="background:#c0cfe4" | ei / eles

|-

! style="height:3em;background:#c0cfe4" colspan="1" | 現在時
]=])
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0cfe4" colspan="1" | 未完成時
]=])
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0cfe4" colspan="1" | 未來時
]=])
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.futr_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="background:#c0e4c0" colspan="1" | 假設語態
! style="background:#c0e4c0" | che ie
! style="background:#c0e4c0" | che tu
! style="background:#c0e4c0" | che el / ela
! style="background:#c0e4c0" | che nos
! style="background:#c0e4c0" | che vos
! style="background:#c0e4c0" | che ei / eles
|-
! style="height:3em;background:#c0e4c0" colspan="1" | 現在時
]=])
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pres_sub_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0e4c0" colspan="1" | 未完成時
]=])
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impf_sub_3pl) .. "\n")
	table.insert(ret, [=[
|-
! colspan="1" rowspan="2" style="height:3em;background:#e4d4c0" | 祈使語態
! style="background:#e4d4c0" | –
! style="background:#e4d4c0" | tu
! style="background:#e4d4c0" | –
! style="background:#e4d4c0" | nos
! style="background:#e4d4c0" | vos
! style="background:#e4d4c0" | –
|-
| —
]=])
	table.insert(ret, "| " .. show_form(data.forms.impr_2sg) .. "\n")
	table.insert(ret, "| —\n")
	table.insert(ret, "| " .. show_form(data.forms.impr_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.impr_2pl) .. "\n")
	table.insert(ret, "| —\n")
	table.insert(ret, [=[
|}
</div></div>]=])

	return table.concat(ret)
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
	
	local word = mw.title.getCurrentTitle().text
	
	if m_infl[word] then
		args[1] = m_infl[word].get_stem(word)
		m_infl[word](args, data)
	elseif mw.ustring.sub(word, -2, -1) == "ir" then
		args[1] = m_infl["ir"].get_stem(word)
		m_infl["ir"](args, data)
	else
		local conj = frame.args["conj"]
		if m_infl[conj] then
			m_infl[conj](args, data)
		else
			error("Inflection for " .. word .. " not found.")
		end
	end
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

return export