local p = {}

local i18n = {
	trackCategory = '[[Category:Pages with%s videos]]',
	services = {	-- when video embeded with a new service provider, add it here
		youtube = ' YouTube',
		youtubeplaylist = ' YouTube',
		vimeo = ' Vimeo',
		bilibili = ' Bilibili',
	}
}

function p.genVideo( provider, id, width, height, align, caption, urlArgs )
	if not id then
		return nil
	end
	provider = provider and provider:lower() or 'youtube'
	local dimensions = tonumber( width ) or 425
	height = tonumber( height )
	if height then
		dimensions = dimensions .. 'x' .. height
	end
	if urlArgs then
		return mw.getCurrentFrame():callParserFunction{ name = '#ev', args = { provider, id = id, dimensions = dimensions, alignment = align, description = caption, urlArgs = urlArgs } }
	else
		return mw.getCurrentFrame():extensionTag{ name = provider, content = id, args = { dimensions = dimensions, alignment = align, description = caption } }
	end
end

function p.main( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end

	local result = p.genVideo( args.provider, args[ 1 ], args[ 2 ], args[ 3 ], args.align, args.caption, args.urlargs )
	if result then
		--result = mw.html.create( 'div' ):wikitext( result )
		if not args.nocat then
			result = tostring( result ) .. i18n.trackCategory:format( i18n.services[ args.provider and args.provider:lower() or '' ] or '' )
		end
	end

	return result
end

return p
