local m_utilities = require("Modulo:utilities")
local m_ja = require("Modulo:ja")
local export = {}

local lang = require("Modulo:languages").getByCode("ja")

local kanji_grade_links = {
	"[[:w:Kyōiku_kanji|Grado: 1]]",
	"[[:w:Kyōiku_kanji|Grado: 2]]",
	"[[:w:Kyōiku_kanji|Grado: 3]]",
	"[[:w:Kyōiku_kanji|Grado: 4]]",
	"[[:w:Kyōiku_kanji|Grado: 5]]",
	"[[:w:Kyōiku_kanji|Grado: 6]]",
	"[[:w:Jōyō kanji|Grado: S]]",      -- 7
	"[[:w:Jinmeiyō kanji|Jinmeiyō]]",  -- 8
	"[[:w:Hyōgai kanji|Hyōgaiji]]"         -- 9
}

-- this is the function that is called from templates
function export.show(frame)
	PAGENAME = mw.title.getCurrentTitle().text
	local args = frame:getParent().args
	local categories = {}
	local cells = {}
	-- replace e.g. 時々 with 時時
	local kanji = mw.ustring.gsub(PAGENAME, '([㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟])々', '%1%1')
	-- remove non-kanji characters
	kanji = mw.ustring.gsub(kanji, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')

	local table_head = '<table align="right" class="wikitable" style="text-align:center; font-size:small; margin:0;"><tr><th colspan="' .. mw.ustring.len(kanji) .. '" style="font-weight:normal;">[[kanji|Kanji]] in questo lemma</th></tr><tr lang="ja" class="Jpan" style="font-size:2em; background:white; line-height:1em;" >'

	-- hold the single kanji as we iterate over all the kanji
	local single_kanji = ""
	-- hold a reading passed from the first unnamed parameter
	local reading = ""
	local actual_reading = ""
	local okurigana = ""
	local sortkey = ""
	local yomi = args["yomi"] or ""
	if yomi == "ok" or yomi == "j" then yomi = "jūbakoyomi" end -- on/kun is jūbakoyomi; NOTE: these are only applicable for two-kanji compounds
	if yomi == "ko" or yomi == "y" or yomi == "yu" then yomi = "yutōyomi" end -- kun/on is yutōyomi; NOTE: these are only applicable for two-kanji compounds
	if yomi == "on" or yomi == "o" then yomi = "on" end
	if yomi == "kun" or yomi == "k" then yomi = "kun" end
	if yomi == "irr" or yomi == "irreg" or yomi == "irregular" then yomi = "irregolare" end
	if yomi == "kan" or yomi == "kanyo" or yomi == "kanyoon" then yomi = "kanyoon" end
	
	-- if hiragana readings were passed,
	-- make the "spelled with ..." categories, the readings cells on the lower level and build the sort key
	-- otherwise rely on the pagename to make the original kanjitab and categories
	table.insert(cells, '<tr style="background:white;">')
	for i = 1, mw.ustring.len(kanji) do
		single_kanji = mw.ustring.sub(kanji,i,i)
		reading = args[i] or ""
		table.insert(cells, "<td>")
		if reading ~= "" and mw.ustring.match(reading, '[ぁ-ゖ]') then
			-- subcategorize by reading if this is joyo kanji, don't do that for less common kanji, with exceptions
			if (m_ja.kanji_grade(single_kanji) < 8 or mw.ustring.match(single_kanji, '[厭昌之芽昌浩智晃淳敦聡晃旭亮糊桂隘阿唖撫鼠阿耘迂寅已伊餡姦闊礙碍凱亥价謳嘔齧日臣桶抉兎鵜卯綾飴焙肋鮫頚糞軋烏痒捷辰叩橙揃嶋澤菱彦囃覗呑之乃鼠做寅樋堤槌机杖頼辿哉叢狢峯巳卍鱒仄他惚弘宏燕倦經痙圭禽僑鋸醵墟屹綺几翫癌劫膠昂鹸牽喧餐鑽瑣些渾梱坤國壕誦哨蒐杓爾梓荼楕躁綜楚闡閃撰專泄藉棲錘錐祷盪淘點顛填擲擢闖厨蛋潭腿冪碧劈焚祓弗憑誹砒婢挽拔撥剥胚播乃狼牢蓮礫醂龍榴蕾酉祐佑耶也蔓曼沫邁呆硼牡甫步矮狸]')) and yomi ~= "irregular" then
				-- table.insert(categories, "Japanese terms spelled with " .. single_kanji .. " read as " .. reading)
			else
				-- table.insert(categories, "Japanese terms spelled with " .. single_kanji)
			end
			-- commentato senza categorie inglesi!

			actual_reading = args[("k" .. i)] or ""
			okurigana = args[("o" .. i)] or ""
			table.insert(cells, '<span class="Jpan" lang="ja">')
			if actual_reading == "" and okurigana == "" then
				sortkey = (sortkey .. reading)
				table.insert(cells, reading)
			elseif actual_reading ~= "" and okurigana == "" then
				sortkey = (sortkey .. actual_reading)
				table.insert(cells, reading)
				table.insert(cells, " > ")
				table.insert(cells, actual_reading)
			elseif actual_reading == "" and okurigana ~= "" then
				sortkey = (sortkey .. reading .. okurigana)
				table.insert(cells, reading)
				table.insert(cells, "(")
				table.insert(cells, okurigana)
				table.insert(cells, ")")
			else
				sortkey = (sortkey .. actual_reading .. okurigana)
				table.insert(cells, reading)
				table.insert(cells, "(")
				table.insert(cells, okurigana)
				table.insert(cells, ") > ")
				table.insert(cells, actual_reading)
				table.insert(cells, "(")
				table.insert(cells, okurigana)
				table.insert(cells, ")")
			end
			table.insert(cells, '</span>')
		else
			-- table.insert(categories, "Japanese terms spelled with " .. single_kanji)
-- no cat inglesi
		end
		
		if reading ~= "" and mw.ustring.match(reading, '[ぁ-ゖ]') then table.insert(cells, "<br/>") end
		table.insert(cells, "<small>")
		local kanji_grade = m_ja.kanji_grade(single_kanji)
		table.insert(cells, kanji_grade_links[kanji_grade] or "")
		table.insert(cells, "</small>")
		table.insert(cells, "</td>")
	end
	-- finish the html for the row
	table.insert(cells, "</tr>")

	local rendaku = args["r"] or ""
	-- no categorie inglesi
	-- if rendaku ~= "" then table.insert(categories, "Japanese terms with rendaku") end
	if yomi ~= "" then
		table.insert(cells, "<tr>")
		if yomi == "on" then 
			--no categorie inglesi
			--table.insert(categories, "Japanese terms read with on'yomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[音読み|on\'yomi]]</td>')
		elseif yomi == "kun" then 
			--no categorie inglesi
			--table.insert(categories, "Japanese terms read with kun'yomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[訓読み|kun\'yomi]]</td>')
		elseif yomi == "yutōyomi" then
			--no categorie inglesi
			--table.insert(categories, "Japanese terms read with yutōyomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[湯桶読み|yutōyomi]]</td>')
		elseif yomi == "jūbakoyomi" then 
			--no categorie inglesi
			--table.insert(categories, "Japanese terms read with jūbakoyomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[重箱読み|jūbakoyomi]]</td>')
		elseif yomi == "irregolare" then 
			--no categorie inglesi
			--table.insert(categories, "Japanese terms with irregular kanji readings")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '"><i>lettura irregolare</i></td>')
		elseif yomi == "kanyoon" then
			--no categorie inglesi
			--table.insert(categories, "Japanese terms read with kan'yōon")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[慣用音#Japanese|kan\'yōon]]</td>')
		end
	table.insert(cells, "</tr>")
	end

	-- use user-provided sortkey if we got one, otherwise 
	-- use the sortkey we've already made by combining the 
	-- readings if provided, if we have neither then 
	-- default to empty string and don't sort
	local userprovided_sortkey = args["sort"] or ""
	if userprovided_sortkey ~= "" then sortkey = userprovided_sortkey end
	if sortkey ~= "" then sortkey = m_ja.jsort(sortkey) end

	-- will only use sortkey if sortkey is different from PAGENAME
	if sortkey == "" then
		return
			table_head ..
			mw.ustring.gsub(kanji, '(.)', '<td style="padding:0.5em;">[[%1#Giapponese|%1]]</td>') .. '</tr>' ..
			table.concat(cells) ..
			'</table>' ..
			m_utilities.format_categories(categories, lang)
	else
		return
			table_head ..
			mw.ustring.gsub(kanji, '(.)', '<td style="padding:0.5em;">[[%1#Giapponese|%1]]</td>') .. '</tr>' ..
			table.concat(cells) ..
			'</table>' ..
			m_utilities.format_categories(categories, lang, sortkey)
	end
end

return export