require('strict')

local p = {}

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

function p._media(args)
	local iconType = args['type'] or 'audio'
	local namespace = args.namespace or mw.title.getCurrentTitle().nsText
	local addKey = yesno(args.key) or false
	local parts = args.parts
	local iconSize = args[1] or args.size
	local category = args.category
	local nocat = yesno(args.nocat) or not addKey or namespace == 'Wikisource' or namespace == 'Author'
	local nobreak = yesno(args.nobreak) or not addKey
	
	local iconAliases = {
		multi = 'multi',
		
		audio = 'audio',
		spoken = 'audio',
		speaker = 'audio',
		
		music = 'music',
		note = 'music',
		song = 'music',
		
		video = 'video',
		film = 'video',
		
		score = 'score'
	}
	iconType = iconAliases[iconType] or iconAliases['audio']
	
	local multi = yesno(args.multi) or iconType == 'multi' or false
	if multi then
		for k, v in pairs(iconAliases) do
			args[v] = yesno(args[v]) or yesno(args[k])
		end
		
		local icons = {}
		local iconTypeList = {'audio', 'music', 'score', 'video'}
		
		for i = 1, #iconTypeList do
			local iType = iconTypeList[i]
			if args[iType] then
				table.insert(
					icons,
					p._media({
						['type'] = iType,
						['multi'] = false,
						['namespace'] = namespace,
						['key'] = addKey,
						['parts'] = parts,
						['size'] = iconSize,
						['category'] = category,
						['nocat'] = nocat,
						['nobreak'] = nobreak and i == 1
					})
				)
			end
		end
		
		return table.concat(icons)
	end
	
	local iconSizes = {
		Author = 20,
		Template = 20,
		Portal = 20,
		key = 20,
		default = 15
	}
	iconSize = iconSize or iconSizes[namespace] or iconSizes['default']
	if addKey then
		iconSize = iconSizes['key']
	end
	
	local iconFiles = {
		audio = 'Speaker Icon.svg',
		music = 'Musical note nicu bucule 01.svg',
		score = 'Romantic music icon.svg',
		video = 'Video Camera Icon.svg'
	}
	local icon = '[[File:' .. iconFiles[iconType] .. '|' .. iconSize .. 'px|link=]]'
	
	if addKey then
		local leadText = ''
		local followText = ''
		
		local partDefaults = {
			music = 'pieces',
			score = 'pieces',
			video = 'parts',
			audio = 'chapters'
		}
		parts = parts or partDefaults[iconType]
		
		if args.namespace == 'Wikisource' or args.namespace == 'Author' then
			local iconMeaning = {
				music = 'includes a musical version',
				score = 'is rendered with the [[Help:Sheet music|Score extension]]',
				video = 'includes a video version',
				audio = 'includes a spoken word version'
			}
			leadText = 'The icon '
			followText = ' indicates that the work ' .. iconMeaning[iconType]
		else
			local iconFormat = {
				music = 'musical format',
				score = 'format using the [[Help:Sheet music|Score extension]]',
				video = 'video format',
				audio = 'spoken word format'
			}
			followText = ' one or more ' .. parts .. ' are available in a ' .. iconFormat[iconType]
		end
		
		icon = "''" .. leadText .. icon .. followText .. ".''"
		if not nobreak then
			icon = '<br/>' .. icon
		end
	end
	
	local categoryDefaults = {
		video = '[[Category:' .. 'Works with videos' .. ']]',
		music = '[[Category:' .. 'Works with music' .. ']]',
		score = '[[Category:' .. 'Works with music' .. ']]',
		audio = '[[Category:' .. 'Spoken works' .. ']]'
	}
	category = category or categoryDefaults[iconType]
	if nocat then
		category = ''
	end
	
	return icon .. category
end

function p.media(frame)
	return p._media(getArgs(frame))
end

return p