local m_gen_num = require("Module:gender and number")
local m_links = require("Module:links")
local m_utilities = require("Module:utilities")

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

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

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">
<div class="NavHead" style="text-align: left">การผันรูปของ ]=]}
	table.insert(ret, m_links.full_link({lang = lang, alt = data.forms.infinitive[1]}, "term") .. "</div>\n")
	table.insert(ret, [=[<div class="NavContent">
{| style="background:#F0F0F0;width:100%" class="inflection-table"
|-
! colspan="2" style="background:#e2e4c0" | รูปกริยากลาง
| colspan="6" align="center" | ]=])
	table.insert(ret, show_form(data.forms.infinitive) .. "\n")
	table.insert(ret, [=[|-
! colspan="2" style="background:#e2e4c0" | รูปกริยาเป็นนาม
| colspan="6" align="center" | ]=])
	table.insert(ret, show_form(data.forms.gerund) .. "\n")
	table.insert(ret, [=[|-
! colspan="2" style="background:#e2e4c0" | past participle
| colspan="6" align="center" | ]=])
	if not data.forms.part_all then
		table.insert(ret, m_gen_num.format_list({"m"}) .. " " .. show_form(data.forms.part_m) .. ", ")
		table.insert(ret, m_gen_num.format_list({"f"}) .. " " .. show_form(data.forms.part_f) .. ", ")
		table.insert(ret, m_gen_num.format_list({"n"}) .. " " .. show_form(data.forms.part_n) .. ", ")
		table.insert(ret, m_gen_num.format_list({"m-p"}) .. " " .. show_form(data.forms["part_m-p"]) .. ", ")
		table.insert(ret, m_gen_num.format_list({"f-p"}) .. " " .. show_form(data.forms["part_f-p"]) .. "\n")
	else
		table.insert(ret, show_form(data.forms.part_all) .. "\n")
	end
	table.insert(ret, [=[
|- style="background:#C0C0C0"
! colspan="2" | person
! style="width:12.5%" | บุรุษที่หนึ่งเอกพจน์<br/>yo
! style="width:12.5%" | บุรุษที่สองเอกพจน์<br/>tu
! style="width:12.5%" | บุรุษที่สามเอกพจน์<br/>él/elli
! style="width:12.5%" | บุรุษที่หนึ่งพหูพจน์<br/>nosotros/nós
! style="width:12.5%" | บุรุษที่สองพหูพจน์<br/>vosotros/vós
! style="width:12.5%" | บุรุษที่สามพหูพจน์<br/>ellos
|-
! style="background:#c0cfe4" rowspan="6" | มาลาบอกเล่า
! style="height:3em;background:#c0cfe4" | present
]=])
	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" | imperfect
]=])
	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" | preterite
]=])
	table.insert(ret, "| " .. show_form(data.forms.pret_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pret_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pret_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pret_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pret_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.pret_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0cfe4" | pluperfect
]=])
	table.insert(ret, "| " .. show_form(data.forms.plup_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.plup_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.plup_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.plup_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.plup_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.plup_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="height:3em;background:#c0cfe4" | future
]=])
	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="height:3em;background:#c0cfe4" | conditional
]=])
	table.insert(ret, "| " .. show_form(data.forms.cond_ind_1sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.cond_ind_2sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.cond_ind_3sg) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.cond_ind_1pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.cond_ind_2pl) .. "\n")
	table.insert(ret, "| " .. show_form(data.forms.cond_ind_3pl) .. "\n")
	table.insert(ret, [=[
|-
! style="background:#c0e4c0" rowspan="2" | มาลาสมมุติ
! style="height:3em;background:#c0e4c0" | present
]=])
	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" | imperfect
]=])
	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, [=[
|-
!  style="height:3em;background:#e4d4c0" colspan="2" | มาลาสั่ง
| —
| ]=])
	table.insert(ret, show_form(data.forms.impr_2sg) .. "\n")
	table.insert(ret, [=[
| —
| —
| ]=])
	table.insert(ret, show_form(data.forms.impr_2pl) .. "\n")
	table.insert(ret, [=[
| —
|}
</div></div>]=])

	return table.concat(ret)
end

-- Main entry point
function export.show(frame)
	local args = frame:getParent().args
	
	-- Create the forms
	local data = {forms = {}, categories = {}, refl = false}
	
	-- Find what type of verb is it (hard-coded in the template).
	-- m_gen_numerate standard conjugated forms for each type of verb,
	local infl_type = frame.args["conj"]
	
	if not args[1] then args[1] = "{{{1}}}" end
	--[[ Provisional code to prevent arg 2 from being blank, to avoid
		the following forms from ending up as blank stems with their
		inflectional endings in cases where this is invoked with only
		args 1 and 3:
			* 1st sg. ind. (would result in "o")
			* 3rd sg. ind. (would result in "a")
			* 2nd. sg. imp. (would result in "a")
	]]--
	if (args[2] == "" or not args[2]) then args[2] = args[1] end
	
	if not infl_type then
		data.forms.infinitive = {args["inf"]}; if data.forms.infinitive[1] == "" then data.forms.infinitive = nil end
	elseif m_infl[infl_type] then
		m_infl[infl_type](args, data)
	else
		error("Verb type " .. infl_type .. " not supported.")
	end
	
	return make_table(data) .. m_utilities.format_categories(data.categories, lang)
end

return export