Module:Interlingual Energizer

local function renderRow(heading, value)
	return '|-\n| style="vertical-align:top; padding-left:5px; padding-right:5px; white-space:nowrap;" | \'\'\'' .. heading .. '\'\'\'\n| ' .. value .. '\n'
end
local function renderPlainRow(text, style) 
	return 	'|-\n| style="padding-left:5px; padding-right:5px; '..style..'" colspan="2" | '.. text .. '\n'
end
local function renderRowHeading(heading) 
	return renderPlainRow('\'\'\'' .. heading .. '\'\'\'', 'text-align:center;padding-top:10px;')
end
local function collapsiblePanel(title, content, lang)
	return '{| class="collapsible collapsed" style="clear: left;" lang="'..lang..'" \n |- style="text-align:left;" | \n ! '..title..'\n|-\n|\n'..content..'\n\|\}'
end
local function translatedPanel(frame, params, root, title)
	local elements = {}
	table.insert(elements, '== '..title.. ' ==\n')
	local langs = {}
	local descs = {}
	for key, value in pairs(params) do
		if string.match( key, '^'..root..'\_.*' ) ~= nil and value ~= '' then
			local langcode,_ = string.gsub( key, '^'..root..'\_', '')
			local l = mw.language.fetchLanguageName(langcode)
			table.insert(langs, '[[#'..mw.uri.anchorEncode( title..'-'..l )..'|'..l..']]' )
		--	table.insert(descs, '=== '..l..' ===\n')
			table.insert(descs, frame:expandTemplate{title = 'Anchor', args = {title..'-'..l}}..'\n'..collapsiblePanel(l, value, langcode))
		end
	end
	if #langs > 0 then
		table.insert(elements, '<p>\'\'The '..mw.ustring.lower(title)..' is also available in '..#langs..' other languages: ' .. table.concat(langs, ', ')..'\'\'</p>\n' )
	end
	table.insert(elements, params[root].. '\n')
	table.insert(elements, table.concat(descs))
	return '\n'..table.concat(elements)
end
local function firstParagraph(frame, params)
	local translatedNames = {}
	for key, value in pairs(params) do
		if string.match( key, '^name\_.*' ) ~= nil and value ~= '' then
			local langcode,_ = string.gsub( key, '^name\_', '')
			local l = mw.language.fetchLanguageName(langcode)
			table.insert(translatedNames, '\'\''.. l ..':\'\' <span lang="'.. langcode.. '">'..value..'</span>')
		end
	end
	local names = ''
	if #translatedNames > 0 then
		names = ' (' .. table.concat(translatedNames, ', ') .. ')'
	end
	return 'The activity \'\'\'' .. params['name'] .. '\'\'\''.. names ..' is part of the collection of [[Interlingual Energizers|interlingual energizers]]. See a [[:Category:All Interlingual Energizers|list of all activities]].'
end


local p = {}

function p.energizer( frame )
	local params = frame:getParent().args
	local elements = {}
	local cats = {}
	local language = mw.getContentLanguage()
	
	table.insert(cats, '[[Category:All Interlingual Energizers]]')
	
	table.insert(elements, '__NOTOC__\n')
	-- Box Start
	table.insert(elements, '<div style="float:right; margin:0 0 1em 1em;">\n{| cellpadding="0" cellspacing="0" style="background-color:#f9f9f9; border:1px solid #999; width:350px;"\n|-\n')
	if params['image'] == nil or params['image'] == '' then
		params['image'] = 'Bundesarchiv Bild 194-0191-38, Kindergarten, Kinderheim.jpg' -- fallback image	
	end
	table.insert(elements, '| colspan="2" style="text-align:center; padding:0px;" | [[File:' .. params['image']  .. '|350px]]\n' )
	table.insert(elements, renderRowHeading('Facilitator') )
	table.insert(elements, renderRow('amount', params['facilitator_amount']) )
	local levels = {}
	levels["simple"] = 1
	levels["medium"] = 2
	levels["advanced"] = 3
	if levels[params['facilitator_language_level']] ~= nil then
		local catname = 'Category:'.. language:ucfirst(params['facilitator_language_level']).. ' Interlingual Energizers'
		table.insert(elements, renderRow('language level', string.rep('[[File:Full Star Blue.svg|16px]]', levels[params['facilitator_language_level']]) .. string.rep('[[File:Empty Star.svg|16px]]', 3-levels[params['facilitator_language_level']]) .. ' ([[:'..catname..'|'..params['facilitator_language_level']..']])' ))	
		table.insert(cats, '[['..catname..']]')
	end
	table.insert(elements, renderRowHeading('Participants') )
	table.insert(elements, renderRow('age', params['participants_age']) )
	table.insert(elements, renderRow('group size', params['participants_group_size']) )
	table.insert(elements, renderRowHeading('Setting') )
	table.insert(elements, renderRow('materials', params['setting_materials']) )
	table.insert(elements, renderRow('space', params['setting_space']) )
	table.insert(elements, renderRow('time', params['setting_time']) )
	table.insert(elements, renderRowHeading('Outcome') )
	table.insert(elements, renderPlainRow(params['outcome'], '') )
	if params['outcome'] ~= nil and params['outcome'] ~= '' then
		local outcomeCats = {
			["energizer"] = "Energizers in Interlingual Energizers",
			["ice breaker"] = "Ice breakers in Interlingual Energizers",
			["GTKEO"] = "Ice breakers in Interlingual Energizers",
			["getting to know each other"] = "Ice breakers in Interlingual Energizers",
			["teambuilder"] = "Teambuilders in Interlingual Energizers"
		}
		for pattern, cat in pairs(outcomeCats) do
			if string.find(string.lower(params['outcome']), string.lower(pattern)) ~= nil then
				table.insert(cats, '[[Category:' .. cat .. ']]')	
			end
		end
	end
	table.insert(elements, '|}</div>\n')
	-- Box End

	table.insert(elements, firstParagraph(frame, params))
	if params['introduction'] ~= nil and params['introduction'] ~= '' then
		table.insert(elements, translatedPanel(frame, params, 'introduction', 'Introduction') )
	end	
	if params['rules'] ~= nil and params['rules'] ~= '' then
		table.insert(elements, translatedPanel(frame, params, 'rules', 'Rules') )
	end
	if params['end'] ~= nil and params['end'] ~= '' then
		table.insert(elements, translatedPanel(frame, params, 'end', 'End of the Game') )
	end
	if params['extensions'] ~= nil and params['extensions'] ~= '' then
		table.insert(elements, '\n== Possible extensions ==\n' .. params['extensions'] )
	end
	table.insert(elements, '\n'..frame:expandTemplate{title = 'Erasmus+ Disclaimer'})
	
	for key, value in pairs(params) do
		if string.match( key, '^%w+\_.*' ) ~= nil and value ~= '' then
			local langcode,_ = string.gsub( key, '^%w+\_', '')
			local l = mw.language.fetchLanguageName(langcode, 'en')
			if l ~= nil and l ~= '' then
				table.insert(cats, '[[Category:Interlingual Energizers in '..l..']]')
			end
		end
	end
	
	if mw.title.getCurrentTitle().text ~= 'Interlingual Energizers/Preload' then
		table.insert(elements, '\n' .. table.concat(cats,'\n'))
	end
	return  table.concat(elements)
end

return p