Documentation for this module may be created at Module:Infobox/doc

local p = {}

function p.infobox(f)
	local args = require('Module:Arguments').getArgs(f)
	local titleObject = mw.title.getCurrentTitle()
	local title = args.title or titleObject.baseText
	local template = f:getParent():getTitle():lower():gsub('template:', '')

	local json = {
		images = {},
		rows = {}
	}

	local imageArea = args.imagearea

	if not imageArea and imageArea ~= 'none' then
		local images = {}
		local defaultImageSize = args.defaultimagesize or '150px'
		local defaultImageClass = args.defaultimageclass

		args.image1 = args.image1 or args.image or 'title'
		args.image1size = args.image1size or args.imagesize
		args.image1class = args.image1class or args.imageclass
		args.group1 = args.group1 or args.group
		args.group1size = args.group1size or args.groupsize
		args.group1class = args.group1class or args.groupclass

		local imgCount = {}
		local groupCount = {}
		local groupImgList = {}

		for k, v in pairs(args) do
			if type(k) == 'string' and type(v) == 'string' then
				local image, num = k:match('^(image)(%d+)$')
				local group, groupNum = k:match('^(group)(%d+)$')
				local groupImg, groupImgNum = k:match('^(%d+)-(%d+)$')

				if v:lower() ~= 'none' then
					if image then
						table.insert(imgCount, tonumber(num))
					elseif group then
						table.insert(groupCount, tonumber(groupNum))
						groupImgList['group' .. groupNum] = groupImgList['group' .. groupNum] or {}
					elseif groupImg then
						groupImgList['group' .. groupImg] = groupImgList['group' .. groupImg] or {}
						table.insert(groupImgList['group' .. groupImg], tonumber(groupImgNum))
					end
				end
			end
		end

		local animate

		if #groupCount > 0 then
			table.sort(groupCount)

			local tabber = {}

			for _, v in ipairs(groupCount) do
				local group = args['group' .. v]
				local groupSize = args['group' .. v .. 'size'] or defaultImageSize
				local groupClass = args['group' .. v .. 'class'] or defaultImageClass
				local groupImages = {}

				if groupImgList['group' .. v] then
					table.sort(groupImgList['group' .. v])

					for _, w in ipairs(groupImgList['group' .. v]) do
						local image = args[v .. '-' .. w]
						local size = args[v .. '-' .. w .. 'size'] or groupSize
						local class = args[v .. '-' .. w .. 'class'] or groupClass

						if image and string.match(image, 'UNIQ%-%-gallery%-') then
							image = image
						elseif image and image:match(';') then
							if not animate then
								animate = require('Module:Animate').animate
							end
							image = animate{image, size, class = class}
						elseif image then
							json.images[#json.images + 1] = image
							local altText = image .. ': Infobox image for ' .. title .. ' the ' .. template .. '.'
							image = '[[File:' .. image .. '|' .. size .. '|class=' .. (class or '') .. '|alt=' .. altText .. ']]'
						else
							image = ''
						end

						if image ~= '' then
							table.insert(groupImages, image)
						end
					end
				end

				table.insert(tabber, '|-|' .. group .. '=\n' .. table.concat(groupImages, '<br />'))
			end

			table.insert(images, f:extensionTag('tabber', table.concat(tabber, '\n')))
		end

		table.sort(imgCount)

		for _, v in ipairs(imgCount) do
			local image = args['image' .. v]
			local size = args['image' .. v .. 'size'] or defaultImageSize
			local class = args['image' .. v .. 'class'] or defaultImageClass

			if image == 'title' then
				local imageTitle = mw.title.new('Media:' .. title .. '.png')

				if #groupCount == 0 and imageTitle and imageTitle.exists then
					json.images[#json.images + 1] = title .. '.png'
					local altText = title .. '.png: Infobox image for ' .. title .. ' the ' .. template .. '.'
					image = '[[File:' .. title .. '.png|' .. size .. '|class=' .. (class or '') .. '|alt=' .. altText .. ']]'
				else
					image = ''
				end
			elseif image and string.match(image, 'UNIQ%-%-gallery%-') then
				image = image
			elseif image then
				json.images[#json.images + 1] = image
				local altText = image .. ': Infobox image for ' .. title .. ' the ' .. template .. '.'
				image = '[[File:' .. image .. '|' .. size .. '|class=' .. (class or '') .. '|alt=' .. altText .. ']]'
			else
				image = ''
			end

			if image ~= '' then
				table.insert(images, image)
			end
		end

		if #images > 0 then
			imageArea = table.concat(images, '<br />')
		else
			imageArea = 'none'
		end
	end

	if not imageArea or imageArea == 'none' then
		imageArea = args.grouparea or ''
	end

	local extraText = args.extratext

	if not extraText or extraText == 'none' then
		extraText = ''
	end

	local html = {}

	table.insert(html, '{| class="infobox"')

	table.insert(html, '|-')
	table.insert(html, '! colspan="2" class="infobox-title" | ' .. title)

	if imageArea ~= '' then
		table.insert(html, '|-')
		table.insert(html, '| colspan="2" class="infobox-imagearea animated-container" | ' .. imageArea)
	end

	if extraText ~= '' then
		table.insert(html, '|-')
		table.insert(html, '| colspan="2" class="infobox-extratext" | ' .. extraText)
	end

	if args.rows and args.rows ~= '' then
		table.insert(html, args.rows)
	end

	if args.footer and args.footer ~= '' then
		json.footer = args.footer
		table.insert(html, '|-')
		table.insert(html, '| colspan="2" class="infobox-footer" | ' .. args.footer)
	end

	table.insert(html, '|}')

	json.title = title

	return f:preprocess(table.concat(html, '\n'))
end

return p