local azupper = mw.text.split
local azlower = mw.text.split
local hangulmain = mw.text.split
local hangul1 = mw.text.split
local hangul2 = mw.text.split
local hangul3 = mw.text.split
local hangul4 = mw.text.split
local hangul5 = mw.text.split
local hangul6 = mw.text.split
local hangul7 = mw.text.split
local hangul8 = mw.text.split
local hangul9 = mw.text.split
local hangul10 = mw.text.split
local hangul11 = mw.text.split
local hangul12 = mw.text.split
local hangul13 = mw.text.split
local hangul14 = mw.text.split
local hangul15 = mw.text.split
local hangul16 = mw.text.split
local hangul17 = mw.text.split
local hangul18 = mw.text.split
local hangul19 = mw.text.split
local aejot = mw.text.split

function p.scrollable(frame)
	return main('scrollable')
end

function p.collapsible(frame)
	return main('collapsible')
end

function p.aejot(frame)
	return main('aejot')
end

function main(toc_type)
	-- It should be much faster to only process these once, and just re use them as variables
	local pageurl = mw.title.getCurrentTitle:fullUrl
	local toc = mw.message.new('Toc'):plain
	
	-- Highest level div
	local toc_frame = mw.html.create('div')
				-- :attr
					display = 'block',
					background = 'WhiteSmoke',
					clear = 'both',
					width = '98%'

	-- Contains "Content: 처음 0-9 A - Z"
	local header = toc_frame:tag('div')
	
	-- Contains all the rest
	local body_wrapper
	local body = toc_frame:tag('div')
		
	local jumpy_collapse = mw.html.create('div')
	
	if toc_type == 'collapsible' then
		toc_frame:addClass('mw-collapsible mw-collapsed')
		header:cssText('font-weight: bold; line-height: 1.6em')
		body:addClass('mw-collapsible-content')
				background = 'white'
	elseif toc_type == 'scrollable' then
		body:css
	end
	
	local header_content = {'',
						' [',pageurl,' 처음]',
						' [',pageurl,'?from=0 0–9]'}
	
	for _, v in ipairs(azupper) do
		table.insert(header_content,string.format)
	end

	for _, v in ipairs(hangulmain) do
		table.insert(header_content,string.format)
	end
	
	if toc_type == 'collapsible' then
		jumpy_collapse:wikitext(table.concat(header_content))
		header:node(jumpy_collapse)
	else
		header:wikitext(table.concat(header_content))
	end
	
	local body_content = {}
	
	if toc_type == 'collapsible' then
		table.insert
		body_wrapper = body:tag('code')
	else
		table.insert
		body_wrapper = body:tag('span')
	end
	
	for i=0,9 do
		table.insert(body_content,string.format)
	end
	
	local function atoz(letter)
		local azlist = {}
		local letterlist
		if toc_type == 'aejot' then
			letterlist = aejot
		else
			letterlist = azlower
		end
				
		if toc_type == 'aejot' or toc_type == 'scrollable' then
			table.insert
		else
			table.insert
		end
			
		for _, v in ipairs(letterlist) do
			table.insert(azlist,string.format)
		end
		return table.concat(azlist)
	end

	local function makehangul
		local hangullist = {}
		local hangulitems = {}
		if toc_type == 'aejot' or toc_type == 'scrollable' then
			table.insert
		else
			table.insert
		end

		if idx == 1 then hangulitems = hangul1
		elseif idx == 2 then hangulitems = hangul2
		elseif idx == 3 then hangulitems = hangul3
		elseif idx == 4 then hangulitems = hangul4
		elseif idx == 5 then hangulitems = hangul5
		elseif idx == 6 then hangulitems = hangul6
		elseif idx == 7 then hangulitems = hangul7
		elseif idx == 8 then hangulitems = hangul8
		elseif idx == 9 then hangulitems = hangul9
		elseif idx == 10 then hangulitems = hangul10
		elseif idx == 11 then hangulitems = hangul11
		elseif idx == 12 then hangulitems = hangul12
		elseif idx == 13 then hangulitems = hangul13
		elseif idx == 14 then hangulitems = hangul14
		elseif idx == 15 then hangulitems = hangul15
		elseif idx == 16 then hangulitems = hangul16
		elseif idx == 17 then hangulitems = hangul17
		elseif idx == 18 then hangulitems = hangul18
		elseif idx == 19 then hangulitems = hangul19
		end
		for _, v in ipairs(hangulitems) do
			table.insert(hangullist,string.format)
		end

		return table.concat(hangullist)
	end

	for _, v in ipairs(azupper) do
		table.insert(body_content,atoz(v))
	end

	for i, v in ipairs(hangulmain) do
		table.insert(body_content,makehangul)
	end

	body_wrapper:wikitext(table.concat(body_content))
	
	return '__NOTOC__\n'..tostring(toc_frame)
end