local export = {}

local m_ja = require("Module:ja")
local m_str_utils = require("Module:string utilities")

local concat = table.concat
local find = m_str_utils.find
local get_script = require("Module:scripts").getByCode
local hira_to_kata = m_ja.hira_to_kata
local insert = table.insert
local kana_to_romaji = require("Module:Hrkt-translit").tr
local kata_to_hira = m_ja.kata_to_hira
local gmatch = m_str_utils.gmatch
local match = m_str_utils.match
local split = m_str_utils.split

local Jpan = get_script("Jpan")
-- local katakana_script = get_script("Kana")
local Hira = get_script("Hira")

local PAGENAME = mw.loadData("Module:headword/data").pagename
local NAMESPACE = mw.title.getCurrentTitle().nsText

-- Only used by commented-out code.
-- local data = mw.loadData("Module:ja/data")

local CONCAT_SEP = ', '
	
local labels = {
	{
		text = "吳音",
		text2 = "goon",
		classification = "on",
	},
	{
		text = "漢音",
		text2 = "kan'on",
		classification = "on",
	},
	{
		text = "宋音",
		text2 = "sōon",
		classification = "on",
	},
	{
		text = "唐音",
		text2 = "tōon",
		classification = "on",
	},
	{
		text = "慣用音",
		text2 = "kan'yōon",
		classification = "on",
	},
	{
		entry = "音讀",
		text = "音讀",
		text2 = "on",
		classification = "on",
		unclassified = "(未分類)",
	},
	{
		entry = "訓讀",
		text = "訓讀",
		text2 = "kun",
		classification = "kun",
	},
	{
		text = "名乘",
		text2 = "nanori",
		classification = "nanori",
	},
}

local function track(code)
	require("Module:debug").track("ja-kanji-readings/" .. code)
end

local function plain_link(data)
	data.term = data.term:gsub('[%.%- ]', '') -- 「かな-し.い」→「かなしい」, 「も-しく は」→「もしくは」
	data.tr = data.tr and data.tr:gsub('[%.%-]', '') or '-'
	data.sc = match(data.term:gsub('[%z\1-\127]', ''), '[^' .. Hira:getCharacters() .. ']') and Jpan or Hira
	data.pos = data.pos ~= '' and data.pos or nil
	return require("Module:links").full_link(data, "term") --"term" makes italic
end

--[=[
		Copied from [[Module:ja]] on 2017/6/14.
		Replaces the code in Template:ja-readings which accepted kanji readings,
		and displayed them in a consistent format.
		Substantial change in function was introduced in https://en.wiktionary.org/w/index.php?diff=46057625
]=]
function export.show(frame)
	local args = require("Module:parameters").process(frame:getParent().args, {
		["goon"] = {},
		["kanon"] = {},
		["soon"] = {},
		["toon"] = {},
		["on"] = {},
		["kanyoon"] = {},
		["kun"] = {},
		["nanori"] = {},
		["pagename"] = {},
	})
	
	local lang_code = frame.args[1] or 'ja'
	local lang = require'Module:languages'.getByCode(lang_code)
	local lang_name = lang:getCanonicalName()
	
	if args.pagename and NAMESPACE == "" then
		error("The pagename parameter should not be used in entries, as it is only for testing.")
	end
	local pagename = args.pagename or PAGENAME

	local yomi_data = mw.loadData("Module:ja/data/jouyou-yomi").yomi

	-- this holds the finished product composed of wikilinks to be displayed
	-- in the Readings section under the Kanji section
	local links, categories = {}, {}

	local is_old_format = false
	
	-- We need a separate kanji sortkey module.
	local sortkey = (require("Module:Hani-sortkey").makeSortKey(pagename, lang_code, "Jpan"))
	local function add_reading_category(reading, subtype, period)
		reading = kata_to_hira(reading:gsub("[%. ]+", ""):gsub("%-$", ""):gsub("%-", "・"))
		if subtype then
			return insert(categories, '[[Category:' .. (period or "")  .. subtype .. '讀作「' .. reading .. '」的' .. lang_name .. '漢字' ..
				'|' .. sortkey .. ']]')
		else
			return insert(categories, '[[Category:讀作「' .. reading .. '」的' .. lang_name .. '漢字'
				.. '|' .. sortkey .. ']]')
		end
	end
	
	local unclassified_on = {}
	local classified_on = {}
	local kun = {}
			
	local kana = "[ぁ-ー]"
	
	for _, label in ipairs(labels) do
		local readings = args[label.text2:gsub('ō', 'o'):gsub('\'', '')]
		if readings then
			local unclassified = ""

			if label.unclassified then
				if not (args.goon or args.kanon or args.soon or args.toon or args.kanyoon) then
					unclassified = label.unclassified
				end
			end
			
			if find(readings, '%[%[' .. kana) then
				is_old_format = true

				if label.classification == 'on' then
					for reading in gmatch(readings, kana .. '+') do
						add_reading_category(reading)
					end
				end

				readings = readings:gsub("%[%[([^%]|]+)%]%]", function(entry)
					if find(entry, "^[" .. Jpan:getCharacters() .. "]+$") then
						return plain_link{
							lang = lang,
							term = entry,
						}
					else
						return "[[" .. entry .. "]]"
					end
				end)
			else
				readings = split(readings, "%s*[,、]%s*")

				for i, reading in ipairs(readings) do
					local is_jouyou = false

					local pos, pos_hist, pos_oldest = { }, { '[[w:歷史假名遣|歷史]]' }, { '古日語' }

					-- check for formatting indicating presence of historical kana spelling
					local reading_mod, reading_hist, reading_oldest, reading_surplus = reading:match'^(.-)%f[<%z]<?(.-)%f[<%z]<?(.-)%f[<%z]<?(.*)$'
					
					if reading_surplus ~= '' then
						error("讀法" .. reading .. "指定了過多歷史讀法。最多能指定3個:現代,歷史,古日語。")
					end
					
					if label.text2 == "on" then
						unclassified_on[reading_mod] = true
						insert(unclassified_on, reading_mod)
					elseif label.text2 == "kun" then
						kun[reading_mod] = true
						insert(kun, reading_mod)
					elseif label.classification == "on" then
						classified_on[reading_mod] = true
						insert(classified_on, reading_mod)
					end
					
					-- test if reading contains katakana
					if find(reading_mod .. reading_hist .. reading_oldest, '[ァ-ヺ]') then
						insert(categories, '[[Category:需要注意的' .. lang_name .. '詞|1]]') -- sometimes legit, like 「頁(ページ)」
					end

					if reading_hist ~= '' or reading_oldest ~= '' then
						-- test if historical readings contain small kana (anachronistic)
						if find(reading_hist .. reading_oldest, '[ぁぃぅぇぉゃゅょ]') then
							insert(categories, '[[Category:需要注意的' .. lang_name .. '詞|2]]') -- 
						end
						
						-- test if reading contains kun'yomi delimiter thing but historical readings don't
						if reading_mod:find("-", 1, true) then
							if reading_hist ~= '' and not reading_hist:find("-", 1, true) or reading_oldest ~= '' and not reading_oldest:find("-", 1, true) then
								insert(categories, '[[Category:需要注意的' .. lang_name .. '詞|3]]')
							end
						end
					end

					-- check if there is data indicating that our kanji is a jouyou kanji
					if yomi_data[pagename] then
						local reading = (label.classification == 'on' and hira_to_kata(reading_mod) or reading_mod)
						reading = reading:gsub('%.', '') -- 「あたら-し.い」→「あたら-しい」
						local yomi_type = yomi_data[pagename][reading]

						if yomi_type then
							is_jouyou = true

							if yomi_type == 1 or yomi_type == 2 then
								insert(pos, '[[w:常用漢字|<abbr title="該讀法列在常用漢字列表內。點此查看維基百科解釋常用漢字的條目。">常用</abbr>]]')
							elseif yomi_type == 3 or yomi_type == 4 then
								insert(pos, '[[w:常用漢字|<abbr title="該讀法列在常用漢字列表內,但被標記為有限制或罕用。點此查看維基百科解釋常用漢字的條目。">常用<sup>†</sup></abbr>]]')
							end
						end
					end
					
					local subtype = label.text
					if reading_mod then
						add_reading_category(reading_mod, subtype)
					end
					if reading_hist ~= '' then
						add_reading_category(reading_hist, subtype, '歷史假名遣')
					end
					if reading_oldest ~= '' then
						add_reading_category(reading_oldest, subtype, '古日語')
					end
					
					-- process kun readings with okurigana, create kanji-okurigana links
					if reading:find("-", 1, true) then
						insert(pos, 1, plain_link{
							lang = lang,
							term = reading_mod:gsub('^.+%-', pagename),
						})
						
						if reading_hist ~= '' then
							insert(pos_hist, 1, plain_link{
								lang = lang,
								term = reading_hist:gsub('^.+%-', pagename),
							})
						end
						
						if reading_oldest ~= '' then
							insert(pos_oldest, 1, plain_link{
								lang = lang,
								term = reading_oldest:gsub('^.+%-', pagename),
							})
						end
					elseif label.classification == 'kun' then
						insert(categories, '[[Category:有訓讀但缺少送假名的' .. lang_name .. '漢字|' .. sortkey .. ']]')
					end

					local rom = kana_to_romaji((reading_mod), lang_code):gsub('^(.+)(%-)', '<u>%1</u>')
					local rom_hist = kana_to_romaji((reading_hist:gsub('^(.+)(%-)', '<u>%1</u>')), lang_code, nil, {hist = true})
					local rom_oldest = kana_to_romaji((reading_oldest:gsub('^(.+)(%-)', '<u>%1</u>')), lang_code, nil, {hist = true})
					
					local mod_link = plain_link{
						lang = lang,
						term = reading_mod,
						tr = rom,
						pos = concat(pos, CONCAT_SEP),
					}
					if is_jouyou then
						mod_link = '<mark class="jouyou-reading">' .. mod_link .. '</mark>'
					end
					
					readings[i] = mod_link .. (reading_hist ~= '' and '<sup>←' .. plain_link{
						lang = lang,
						term = reading_hist,
						tr = rom_hist,
						pos = concat(pos_hist, CONCAT_SEP),
					} .. '</sup>' or '') .. (reading_oldest ~= '' and '<sup>←' .. plain_link{
						lang = lang,
						term = reading_oldest,
						tr = rom_oldest,
						pos = concat(pos_oldest, CONCAT_SEP),
					} .. '</sup>' or '')
				end

				readings = concat(readings, "、")
			end
			
			-- Add "on-yomi", "kun-yomi", or "nanori-yomi" class around list of
			-- readings to allow JavaScript to locate them.
			insert(links, "* '''[[Appendix:日語術語#" .. (label.entry or label.text2) .. '|'.. label.text .. "]]'''" .. unclassified .. ': <span class="' .. label.classification .. '-yomi">' .. readings .. '</span>')
		end
	end
	
	for _, reading in ipairs(unclassified_on) do
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/ja-kanji-readings/duplicate reading]]
		if classified_on[reading] then
			track("duplicate reading")
		end
		track("unclassified reading") -- Track unclassified readings for later classification
	end
	
	if not next(classified_on) and not next(unclassified_on) then
		if next(kun) then
			-- [[Special:WhatLinksHere/Wiktionary:Tracking/ja-kanji-readings/kun only]]
			track("kun only")
		end
	elseif not next(kun) then
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/ja-kanji-readings/on only]]
		track("on only")
	end

	if is_old_format then
		insert(categories, '[[Category:使用舊ja-readings格式的日語漢字|' .. sortkey .. ']]')
	end
	
	return concat(links, '\n') .. (NAMESPACE == '' and concat(categories) or '') .. require("Module:TemplateStyles")("Template:ja-readings/style.css")
end

return export