local k = {}
local WikitextLocal = require( 'Module:WikitextLocal' )

function Tcode( args )
	if args.T == nil or args.T == '' then
		return ''
	end
	local div = mw.html.create( 'div' )
		:attr( 'class', 'noteTag-title' )
		:attr( 'data-notetag-code', args.T )
		:wikitext( WikitextLocal.title( args.T ) )
	if args.dt ~= nil and args.dt ~= '' then
		div:attr( 'data-notetag-desc', args.dt )
	end
	return tostring( div )
end

function group( name, frame )
	if name == nil or name == '' then
		return ''
	end
	local moduleTitle = mw.title.makeTitle( 'Module', 'KGroup/' .. name )
	if moduleTitle and moduleTitle.exists then
		local data = require( 'Module:KGroup/' .. name )
		local pieces = {}
		if data.content then
			for i, v in ipairs( data.content ) do
				if v.type == 'item' and v.rule then
					table.insert( pieces, '-{H|' .. v.rule .. '}-' )
				end
			end
			return tostring( mw.html.create( 'div' )
				:attr( 'data-notetag-group-source', 'module' )
				:attr( 'data-notetag-group', data.name or name )
				:wikitext( table.concat( pieces ) ) )
		end
	end
	local templateTitle = mw.title.makeTitle( 'Template', 'KGroup/' .. name )
	if templateTitle and templateTitle.exists then
		return frame:expandTemplate{ title = templateTitle }
	end
	return tostring( mw.html.create( 'div' )
			-- :attr( 'id', 'noteTag-group-' .. mw.uri.anchorEncode( name ) )
			:attr( 'data-notetag-group-source', 'none' )
			:attr( 'data-notetag-group', name ) )
end

function Gcode( args, frame )
	local code = {}
	for i = 1, 30 do
		table.insert( code, group( args['G' .. i], frame ) )
	end
	code = table.concat( code )
	if code ~= '' then
		code = tostring( mw.html.create( 'div' )
				:attr( 'class', 'noteTag-group' )
				:wikitext( code ) )
		if args.G31 ~= nil then
			code = code .. '[[Category:NoteTag에 알 수 없는 변수를 포함한 위키백과 문서|G]]'
		end
	end
	return code
end

function local_( i, code, desc )
	if code == nil or code == '' then
		return ''
	end
	local div = mw.html.create( 'div' )
		-- :attr( 'id', 'noteTag-local-' .. i )
		:attr( 'data-notetag-code', code )
		:wikitext( WikitextLocal.hidden( code ) )
	if desc ~= nil and desc ~= '' then
		div:attr( 'data-notetag-desc', desc )
	end
	return tostring( div )
end

function Lcode( args )
	local code = {}
	for i = 1, 30 do
		table.insert( code, local_( i, args[i], args['d' .. i] ) )
	end
	code = table.concat( code )
	if code ~= '' then
		code = tostring( mw.html.create( 'div' )
				:attr( 'class', 'noteTag-local' )
				:wikitext( code ) )
		if args[31] ~= nil then
			code = code .. '[[Category:NoteTag에 알 수 없는 변수를 포함한 위키백과 문서|L]]'
		end
	end
	return code
end

function k.main( frame )
	local args
	if frame == mw.getCurrentFrame() then
		-- Being called from {{noteTag}}
		args = frame:getParent().args
	else
		-- Being called from another module
		args = frame
		frame = mw.getCurrentFrame()
	end
	local Tc = Tcode( args )
	local Gc = Gcode( args, frame )
	local Lc = Lcode( args )
	local code = Tc .. Gc .. Lc
	if code ~= '' then
		local hash = require( 'Module:Crc32lua' ).crc32( mw.dumpObject( args ) )
		code = frame:extensionTag{
			name = 'indicator',
			content = '표기',
			args = { name = string.format( 'noteTag-%x', hash ) },
		} .. tostring( mw.html.create( 'div' )
				:attr( 'id', string.format( 'noteTag-%x', hash ) )
				:attr( 'class', 'noteTag' )
				:wikitext( code ) )
		if mw.title.getCurrentTitle():inNamespace( 'Template' ) then
			code = code .. '[[Category:noteTag를 포함한 위키백과 틀]]'
		end
	end
	return code
end

return k