Documentation icon دستاویز [تخلیق]
local p = {}
local getArgs = require('Module:Arguments').getArgs -- Import module function to work with passed arguments
local parserModule = require "Module:Road data/parser"
local parser = parserModule.parser

local function size(args)
	local country = args.country
	local state = args.state or args.province
	local type = args.type
	if country == 'MEX' then
		return "20x30"
	elseif country == 'CAN' then
		if state == 'AB' or state == 'QC' or state == 'SK' then
			return "20x30"
		elseif state == 'NS' then
			if (type == 'NS' or type == 'Hwy') then
				return "18"
			else
				return "x20"
			end
		elseif state == 'ON' then
			if type == 'ON' or type == 'Hwy' or type == 'Fwy' then
				return "20x30"
			elseif type == 'CR' or type == 'RR' then
				return "30x32"
			end
		end
		return "25x20"
	elseif country == 'USA' then
		if state == 'NY' and (type == 'NY 1927' or type == 'NY 1948') then
			return '20'
		end
	end
	return 'x20'
end

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

local function link(args)
	local abbr = parser(args, 'abbr')
	if not abbr then
		local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
		return mw.ustring.format("<span class=\"error\">Invalid type: %s</span>[[Category:Infobox road transclusion errors|& %s]]", args.type, page)
	end
	local link = parser(args, 'link')
	if not link or link == '' then
		return mw.ustring.format("<span class=\"nowrap\">%s</span>", abbr)
	else
		return mw.ustring.format("<span class=\"nowrap\">[[%s|%s]]</span>", link, abbr)
	end
end

local function previousRoute(args)
	local shieldText = shield(args)
	local linkText = link(args)
	local cell = mw.html.create('td'):css( {width = "45%", ["vertical-align"] = "middle", padding = "0", ["text-align"] = "right"} )
	cell:wikitext("→ " .. shieldText .. ' ' .. linkText)
	return cell
end

local function nextRoute(args)
	local shieldText = shield(args)
	local linkText = link(args)
	local cell = mw.html.create('td'):css( {width = "45%", ["vertical-align"] = "middle", padding = "0", ["text-align"] = "left"} )
	cell:wikitext(linkText .. ' ' .. shieldText .. " ←")
	return cell
end

function p._browse(args)
	local browseRow = mw.html.create('tr')
	
	local country = args.country
	local state = args.state or args.province
	local county = args.county
	
	local previousData = {country = country, state = state, county = county,
	                      type = args.previous_type, route = args.previous_route,
	                      dab = args.previous_dab}
	local nextData = {country = country, state = state, county = county,
	                  type = args.next_type, route = args.next_route,
	                  dab = args.next_dab}
	
	local previousRoute = previousRoute(previousData)
	local nextRoute = nextRoute(nextData)
	
	local centerRoute = mw.html.create('td'):css( {["text-align"] = "center", ["white-space"] = "nowrap", width = "10%", ["vertical-align"] = "middle", padding = "0"} )
	local route = args.browse_route
	if route then
		centerRoute:wikitext("'''" .. route .. "'''")
	end
	
	browseRow:node(previousRoute):node(centerRoute):node(nextRoute)
	return tostring(browseRow)
end

function p.browse(frame)
	local args = getArgs(frame)
	args.browse_route = args.route
	return p._browse(args)
end

return p