Implements {{license}}.


require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs

function p._license(args)
	local frame = mw.getCurrentFrame()
	
	-- Parameters
	local text = frame:preprocess(args.text or '{{{text}}}')
	local image = args.image
	local image_r = args.image_r or args['image-r']
	local image_size = args.image_size or args['image-size'] or 'x48px'
	local image_r_size = args.image_r_size or args['image-r-size'] or image_size
	local warning = args.warning
	
	local category = args.category
	local category2 = args.category2
	if category == '' then
		category = nil
	end
	if category2 == '' then
		category2 = nil
	end
	
	-- licenseContainer is the class that styles depend on
	local class_param = 'class="' .. 'licenseContainer licenseBanner dynlayout-exempt ' .. (args.class or '') .. '"'
	local id_param = ''
	if args.id then
		id_param = 'id="' .. args.id .. '"'
	end
	
	-- opening div
	local openDiv = '<div>'
	-- closing div
	local closeDiv = '</div>'
	
	-- license banner
	local leftField = '<span class="noimageLeft"></span>'
	if image then
		leftField = '<span class="imageLeft">[[File:' .. image .. '|' .. image_size .. '|alt=|link=]]</span>'
	end
	leftField = table.concat({openDiv, leftField, closeDiv}, '\n')
	
	local centerField = table.concat({openDiv, openDiv, text, closeDiv}, '\n')
	if warning then
		centerField = centerField .. '\n' .. table.concat({
			openDiv,
			openDiv,
			'[[File:OOjs UI icon alert destructive black-darkred.svg|35px]]',
			closeDiv,
			openDiv,
			warning,
			closeDiv,
			closeDiv
		}, '\n')
	end
	centerField = centerField .. '\n' .. closeDiv
	
	local rightField = '<span class="noimageRight></span>'
	if image_r then
		rightField = '<span class="imageRight">[[File:' .. image_r .. '|' .. image_r_size .. '|alt=|link=]]</span>'
	end
	rightField = table.concat({openDiv, rightField, closeDiv}, '\n')
	
	local licenseBanner = table.concat({
		openDiv,
		openDiv,
		openDiv,
		leftField,
		centerField,
		rightField,
		closeDiv,
		closeDiv,
		closeDiv
	})
	
	-- Commons auto-detection
	local commonsAutoDetection = table.concat({
		'<div class="licensetpl">\n',
		'<span class="licensetpl_short">Public domain</span>',
		'<span class="licensetpl_long">Public domain</span>',
		'<span class="licensetpl_link_req">false</span>',
		'<span class="licensetpl_attr_req">false</span>',
		'\n</div>'
	})
	
	-- Categorization
	local namespace = args.namespace or mw.title.getCurrentTitle().nsText
	local noCatNamespaces = {
		['Help'] = true,
		['Help talk'] = true,
		['Template'] = true,
		['Template talk'] = true,
		['Wikisource'] = true,
		['Wikisource talk'] = true,
		['Module'] = true,
		['Module talk'] = true,
		['Category'] = true,
		['Category talk'] = true
	}
	local categoryContent = ''
	if not noCatNamespaces[namespace] then
		if namespace == 'Author' or namespace == 'Author talk' then
			if category then
				category = 'Author-' .. category
			end
			if category2 then
				category2 = 'Author-' .. category2
			end
		end
		if category then
			category = '[[Category:' .. category .. ']]'
		end
		if category2 then
			category2 = '[[Category:' .. category2 .. ']]'
		end
		categoryContent = (category or '') .. (category2 or '')
	end
	
	local templateStyles = frame:preprocess('<templatestyles src="Template:License/styles.css" />')
	return templateStyles .. table.concat({
		'<div ' .. class_param .. ' ' .. id_param .. '>',
		licenseBanner,
		commonsAutoDetection,
		categoryContent .. closeDiv
	}, '\n')
end

function p.license(frame)
	return p._license(getArgs(frame))
end

return p