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

local p = {}
local getArgs = require('Module:Arguments').getArgs
local roadDataModule = require("Module:Road data")
local util = require("Module:Road data/util")
local parserModule = require("Module:Road data/parser")
local parser = parserModule.parser
local format = mw.ustring.format
local frame = mw.getCurrentFrame()
local templatestyles = 'Infobox road/styles.css'


local function country(args)
	local state = args.state or args.province
	local country
	local countryModule = mw.loadData("Module:Road data/countrymask")
	local country = args.country or countryModule[state]
	
	return country
end

local size = "40px"

local function banner(args)
	if args.marker_image then return nil end
	if args.shielderr then return nil end
		
	local shield = parser(args, 'banner')
	if not shield or shield == '' then return nil end
	
	local alt = parser(args, 'banner')
	
	return string.format('[[File:%s|%s|alt=%s]]', shield, size, alt)
end



local function shield(args)
	if args.marker_image then return args.marker_image end

	local shield = parser(args, 'shieldmain') or parser(args, 'shield')
	if not shield or shield == '' then return nil end
	
	local label = parser(args, 'name') or parser(args, 'abbr') or ''
	local alt = label .. ' marker'
	local orientation = parser(args, 'orientation')
	
	local function shield_size(image_name, orientation)
		local image = 'File:' .. image_name
		local title = mw.title.new(image)

		local width = title.file.width
		local height = title.file.height
		
		if (orientation and orientation == 'upright') or height > width then
			return size
		else
			return 'x' .. size
		end
	end
	
	if type(shield) == 'table' then
		local res = {}
		local sizes = {}
		for i,v in ipairs(shield) do
			sizes[i] = v
			res[i] = string.format('[[File:%s|%s|alt=%s]]', v, shield_size(v), alt)
		end
    	return table.concat(res, ' ')
	else
    	return string.format('[[File:%s|%s|alt=%s]]', shield, shield_size(shield, orientation), alt)
	end
end

-- Links/abbreviations
local function name(args)
	local name = args.name or parser(args, 'name') or parser(args, 'abbr')
	return name
end

function p._routeInfo(args)
	local banner = banner(args)
	local shield = shield(args)
	local name = name(args)

	if not args.type and not args.route and not args.name and not args.marker_image then
		local container = nil
	else local container = mw.html.create('div'):cssText('text-align:center;')
		if shield == nil or args.marker_image == 'none' or args.name and not args.marker_image and not args.type and not args.route then 
			container:tag('p'):cssText('margin:0.1em;'):wikitext(name)
		elseif args.marker_image ~= '' and args.name == '' or args.name == nil and not args.type and not args.route then
			container:tag('p'):cssText('margin:0.1em;'):wikitext(shield)
		elseif args.country == 'AUS' then
			container:tag('p'):cssText('margin:0.1em;'):wikitext(name)
			container:tag('p'):cssText('margin:0.1em 0 0 0;'):wikitext(shield)
		else
			container:tag('p'):cssText('margin:0.1em 0 0 0;'):wikitext(banner)
			container:tag('p'):cssText('margin:0 0 0.1em;'):wikitext(shield)
			container:tag('p'):cssText('margin:0.1em;'):wikitext(name)
		end
		return tostring(container)
	end
end

function p.infobox(args)
	local lengthModule = require "Module:Infobox road/length"
	local IBR = require "Module:Infobox road/sandbox"
	local headerStyle = IBR.headerStyle(args)

	local infobox = {}
	
	infobox['above'] = p._routeInfo(args)
	infobox['subheader'] = args.alternate_name
	infobox['subheader2'] = "<div class=" .. headerStyle .. "style='margin:0; height:.5em;'></div>"
	infobox['label1'] = "Location"
	infobox['data1'] = args.location
	infobox['label2'] = "Length"
	infobox['data2'] = lengthModule._length(num, args)
	infobox['label3'] = "Existed"
	infobox['data3'] = IBR._existed(args)
	infobox['label4'] = "History"
	infobox['data4'] = args.history
	
	if country(args) == "AUS" then
		infobox['label5'] = "[[Route number#Australia|Route number]]"
		infobox['data5'] = args.route
	end
	
	infobox['label6'] = "Tourist routes"
	infobox['data6'] = args.tourist

	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. frame:expandTemplate ({title='Infobox', args = infobox})
	
end

function p.infobox(frame)
	local args = getArgs(frame)
	
	return p.infobox(args)
end

return p