-- This module is currently used by Template:zh-hanzi-box, which is meant to be a replacement for both zh-hanzi and Hani-forms
-- CSS is in MediaWiki:Common.css (search for zh-hanzi-box)
local export = {}

local gsub = mw.ustring.gsub
local match = mw.ustring.match
local len = mw.ustring.len

local function create_combined_box(title)
	local combined_box = [=[{| class="zh-hanzi-box"
! [[อักษรจีนตัวเต็ม|ตัวเต็ม]]และ[[อักษรจีนตัวย่อ|ตัวย่อ]]
|- 
| lang="zh" class="Hani" | ]=] .. add_glosses(title) .. [=[</span>
|}]=]
	return combined_box
end

local function create_separate_box(simtitle, tratitle)
	local separate_box = [=[{| class="zh-hanzi-box"
! [[อักษรจีนตัวเต็ม|ตัวเต็ม]] 
| lang="zh" class="Hani" | ]=] .. add_glosses(tratitle) .. [=[ </span>
|-
! [[อักษรจีนตัวย่อ|ตัวย่อ]] 
| lang="zh" class="Hani" | ]=] .. add_glosses(simtitle) .. [=[ </span>
|}]=]
   return separate_box
end

local function check_box(sim,tra)
	local title = mw.title.getCurrentTitle()
	if title.nsText == '' and title.exists then
		local text = sim .. '/' .. tra
		text = gsub(text,'%[%[[^%]|]+|([^%]|]+)%]%]','%1')
		text = gsub(text,'%[%[ไฟล์:[^%]]+|link=([^%]]+)%]%]','%1')
		text = gsub(text,'หรือ','/')
		text = gsub(text,' ','/')
		text = gsub(text,'[%[%]]','')
		text = gsub(text,'/+','/')
		for item in mw.text.gsplit(text,"/",true) do
			if item ~= '' and not (mw.title.new(item) or {}).exists then
				return '[[Category:Chinese terms with uncreated forms]]<small class="attentionseeking">(At least one of the forms in the hanzi box is uncreated. Detected: "[[' .. item .. ']]".)</small>'
			end
		end
	end
	return ''
end

function add_glosses(text)
	local link, character = '', ''
	local m_zh, m_zh_data, m_glosses_data = require("Module:zh"), require("Module:zh/data"), require("Module:zh/data/glosses")
	if len(mw.title.getCurrentTitle().text) == 1 or match(text, '%|') then
		return text
	end
	for link in mw.ustring.gmatch(text, '%[%[[^%[%]]+%]%]') do
		link = gsub(link, '[%[%]]', '')
		local original_link = link
		for character in mw.ustring.gmatch(link, '[一-鿌]') do
			local original_character = character
			if m_glosses_data.glosses[character] or m_glosses_data.glosses[m_zh_data.st[character]] then
				character = '<span class="explain" title="' .. (m_glosses_data.glosses[character] or m_glosses_data.glosses[m_zh_data.st[character]]) .. '">' .. character .. '</span>'
			end
			link = gsub(link, original_character, character)
		end
		link = original_link .. '|' .. link
		text = gsub(text, '%[%['..original_link..'%]%]', '[['..link..']]')
	end
	return text
end

-- This function should be publicly invoked by a template
-- It takes two arguments, the first of which is required and second optional. 
-- The first parameter represents the simplified form of an entry. If the characters are the same in both scripts, then only the first parameter is used.
-- The second parameter optionally represents the traditional form of an entry, if applicable.
function export.init(frame)
	local arguments = frame:getParent().args
	--local sim = arguments[1] or error("First parameter is required!")
	local sim = arguments[1] or ""
	local tra = arguments[2] or ""

	if tra == "" then
		return create_combined_box(sim) .. check_box(sim,'')
	else
		return create_separate_box(sim, tra) .. check_box(sim,tra)
	end
end

return export