score_version = '"2.22.0"'
page_namespace = 104

function error( txt )
	return '<span style="color:red">Error: incorrect use of Tscore template: ' .. txt .. '</span>'
end

function getnextpage( start, name )
	local stdargs = { start='', next='0', score='', cont='', width='180', layout='' }
	local wynik = { }
	local wynik2 = { }
	local strona, kod, kod2, kod3, kod4, n
	if string.match( name, '^Page:') == 'Page:' then
		name = string.sub( name, 8 )
	end
	strona = mw.title.makeTitle( 'Page', name )
	kod = strona:getContent()
	if kod == nil then
		return nil
	end
	kod = string.match( kod, '{{[SsTt]score%s*|.*cont *= *' .. start .. '.*}}' )
	if kod == nil then
		return nil
	end
	kod = string.sub( kod, 1, string.find( kod, '[^!]}}' ) )
	for kod2 in string.gmatch( kod, '|[^|]*') do
		kod3 = mw.text.trim( string.sub( kod2, 2 ) )
		n = string.find( kod3, '=' )
		kod4 = mw.text.trim( string.sub( kod3, 1, n-1 ) )
		if stdargs[kod4] ~= nil then
			stdargs[kod4] = mw.text.trim( string.sub( kod3, n+1 ) )
		else
			wynik[kod4] = mw.text.trim( string.sub( kod3, n+1 ) )
		end
	end
	if stdargs['next'] ~= '0' then
		wynik2 = getnextpage( start, stdargs['next'] )
		if type( wynik2 ) ~= "table" then
			return wynik2
		end
	end
	for k,v in pairs( wynik ) do
		if wynik2[k] ~= nil then
			wynik[k] = wynik[k] .. ' ' .. wynik2[k]
		end
		wynik[k] = string.gsub( wynik[k], '{{!}}', '|' )
	end
	return wynik
end

function scal( frame )
	local stdargs = { start='', next='0', score='', cont='', width='180', layout='', debug='0' }
	local args = frame:getParent().args
	local kod_score = ""
	local warning = ''
	local argn = { }
	local title = mw.title.getCurrentTitle()
	local paper_size = "a2"
	for k, v in pairs( args ) do
		if type( k ) == "number" then
			return error('unnamed template parameter encountered; maybe | was used instead of {{!}}')
		elseif type( k ) == "string" and stdargs[k] ~= nil then
			stdargs[k] = v
		end
	end
	if stdargs['score'] == '' then
		return error('no score code')
	end
	if stdargs['start'] ~= '' and stdargs['next'] == '0' then
		warning = warning .. error('no next page defined') .. '<br>'
	end
	if stdargs['start'] ~= '' and stdargs['cont'] ~= '' then
		warning = warning .. error('the first page cannot be a sunbsequent page') .. '<br>'
	end
	if title.namespace ~= page_namespace and stdargs['cont'] ~= '' then
		return warning
	end
	if title.namespace ~= page_namespace and stdargs['start'] ~= '' and stdargs['next'] ~= '0' then
		argn = getnextpage( stdargs['start'], stdargs['next'] )
		if argn == nil then
			return error( 'next page not found' )
		elseif type( argn ) == "string" and argn == '' then
			return error( 'no continueing Tscore template found on the next page' )
		elseif type( argn ) ~= "table" then
			return error( 'unidentified error' )
		end
	end
	for k, v in pairs( args ) do
		if type( k ) == "string" and stdargs[k] == nil then
			kod_score = kod_score .. "\n" .. k .. ' = '
			if string.sub( k, 1, 1 ) == 'l' then
				kod_score = kod_score .. "\\lyricmode "
			end
			kod_score = kod_score .. "{ " .. v;
			if argn[k] ~= nil then
				kod_score = kod_score .. ' ' .. argn[k]
			end
			kod_score = kod_score .. " }\n"
		end
	end
-- limit width for Page ns
	if title.namespace == page_namespace and tonumber(stdargs['width']) > 140 then
		stdargs['width'] = '140'
	end
-- optimize paper size: a2 / a3 / a4 ; width in milimeters
	if tonumber(stdargs['width']) > 400 then
		stdargs['width'] = '400'
	elseif tonumber(stdargs['width']) < 190 then
		paper_size = "a4"
	elseif tonumber(stdargs['width']) < 280 then
		paper_size = "a3"
	end
	kod_score = kod_score .. "\n\\paper { #(set-paper-size \"" .. paper_size .. "\") }\n\\header { tagline = ##f }\n\\version " .. score_version
	kod_score = kod_score .. "\n\\score {\n\\midi { }\n\\layout { line-width = #" .. stdargs['width'] .. "\n" .. stdargs['layout'] .. "}\n" .. stdargs['score'] .. " }"
	if stdargs['debug'] == '1' then
		warning = warning .. "\n<code><nowiki>" .. kod_score .. "</nowiki></code>\n"
	end
	return warning .. frame:extensionTag( 'score', kod_score, { raw = "1", midi = "1", vorbis = "1" } )
end

local p = {}
function p.genscore( frame )
	return scal( frame )
end
return p