local p = {}

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local routeModule = require("Module:Infobox road/route")
local mapModule = require("Module:Infobox road/map")
local lengthModule = require("Module:Infobox road/length/sandbox")
local sectionModule = require("Module:Infobox road/sections")
local locationModule = require("Module:Infobox road/locations/sandbox")
local parserModule = require("Module:Road data/parser")
local parser = parserModule.parser
local format = mw.ustring.format
local frame = mw.getCurrentFrame()

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

-- HEADER COLORS

function p.headerStyle(frame)
	local args = getArgs(frame)
	local deleted = args.decommissioned or args.deleted
	local uc = args.header_type == "under construction" or args.header_type == "const" or args.header_type == "uc"
	local minor = args.header_type == "minor"
	local hist = args.header_type == "hist" or args.header_type == "historic" or args.header_type == "historical" or args.header_type == "scenic"
	local color = parser(args, 'color')
	
	if uc then
		return "header-uc"
	elseif minor then
		return "header-minor"
	elseif deleted then
		return "header-deleted"
	elseif hist then
		return "header-hist"
	elseif color then
		return "header-" .. color
	elseif args.state or args.province or args.country then
		local country = country(args)
		return "header-" .. country or "header-default"
	else
		return "header-default"
	end
end

-- SECOND IMAGE

local function photo(args)
	local country = country(args)
	local photo = args.photo or args.image
	local width = args.photo_wide or args.image_width or "290px"
	local alt = args.photo_alt or args.image_alt or photo
	
	if photo == nil then
		return nil
	elseif country == "USA" then
		return nil
		else return string.format('[[File:%s|%s|alt=%s]]', photo, width, alt)
	end
end

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

-- TRANSLATION

local function translate(args, lang)
	local lang = parser(args, 'lang') or args.lang
	local translation = args.translation or parser(args, 'translation')
	if not translation then
		return nil
	else
		return frame:expandTemplate{ title = 'lang', args = { lang, translation }}
	end
end

function p.translate(frame)
	local pframe = frame:getParent()
    local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
    local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
    
    local lang = config.lang
	return translate(args, lang)
end

-- MAIN INFOBOX BELOW --

local function info(args)
	local country = country(args)
	local statejct = args.state or args.province
	local countyjct = args.county
	local parentType = args.spur_type
	local parentRoute = args.spur_of
	local aux = parser(args, 'aux') or "Auxiliary route" or nil
	local law = parser(args, 'law') or nil
	local maint = args.maint or parser(args, 'maint') or nil
	local formed = args.established or args.formed or nil
	local deleted = args.decommissioned or args.deleted or nil
	
	local infobox_args = {
		['child'] = "yes"
	}

	--ROUTE INFORMATION --
	if maint ~= nil or args.section or args.spur_of or args["e-road"] or args.ahn or args.tahn or args.mrn or args.length_mi or args.length_km or args.time_period or args.formed or
		args.allocation or args.history or args.restrictions or args.tourist or args.status or args.margary then
			
		infobox_args['header2'] = "Route information"
	else
		return nil
	end
	
	local jct = frame:expandTemplate{
			title = 'jct', args = {
				state = statejct,
				country = country,
				county1 = countyjct,
				parentType,
				parentRoute,
				noshield1 = "yes"
			}
		}

	if not parentType and not parentRoute then
		return nil
	elseif type(aux) == "table" then
		infobox_args['data3'] = "Auxiliary route of " .. jct
	else
		infobox_args['data3'] = tostring(aux) .. " of " .. jct
	end
	
	if args["e-road"] then
		local eshield = args["e-road-shield"] or ''
		infobox_args['data4'] = eshield .. " Part of " .. args["e-road"]
	end
	if args.ahn then
		local ashield = args["ahn-shield"] or ''
		infobox_args['data5'] = ashield .. " Part of " .. args.ahn
	end
	if args.tahn then
		local tshield = args["tahn-shield"] or ''
		infobox_args['data6'] = tshield .. " Part of " .. args.tahn
	end
	if args.mrn then
		local mshield = args["mrn-shield"] or ''
		infobox_args['data7'] = mshield .. " Part of " .. args.mrn
	end
	
	infobox_args['label8'] = "Legal definition"
	infobox_args['data8'] = law
	infobox_args['label8'] = "Maintenance"
	
	if maint == "none" or nil then
		return nil
	else
		infobox_args['data8'] =  maint
	end
	
	if args.time_period then
		infobox_args['label9'] = "Established by" 
		infobox_args['data9'] = args.established_by
	end
	
	infobox_args['label12'] = "Length"
	infobox_args['data12'] = lengthModule._length
	infobox_args['label14'] = "Planned length"
	infobox_args['data14'] = lengthModule._length({desc = planned})
	infobox_args['label16'] = "Untolled length"
	infobox_args['data16'] = lengthModule._length({desc = free})
	infobox_args['label18'] = "Tolled length"
	infobox_args['data18'] = lengthModule._length({desc = tolled})
	infobox_args['label20'] = "Length under construction"
	infobox_args['data20'] = lengthModule._length({desc = constr})
	infobox_args['label22'] = "Overall length"
	infobox_args['data22'] = lengthModule._length({desc = overall})
	
	infobox_args['label25'] = "Status"
	infobox_args['data25'] = args["status"]
	infobox_args['label26'] = "Existed"
	if formed == nil then
		return nil
	elseif deleted == nil then
		infobox_args['data26'] = formed .. "–present"
	else
		infobox_args['data26'] =  formed .. "–" .. deleted
	end
	
	infobox_args['label27'] = "History"
	infobox_args['data27'] = args.history
	infobox_args['label28'] = "Time period"
	infobox_args['data28'] = args.time_period
	
	if args.time_period then
		infobox_args['label29'] = "Cultural significance"
		infobox_args['data29'] = args.significance
		infobox_args['label30'] = "Known for"
		infobox_args['data30'] = args.known_for
		infobox_args['label31'] = "Related routes"
		infobox_args['data31'] = args.related
	else
		infobox_args['label30'] = "Known for"
		infobox_args['data30'] = args.known_for
	end
	
	if country == "AUS" then
		infobox_args['label31'] = "[[Route number#Australia|Allocation]]"
	else
		infobox_args['label31'] = "Component<br>highways"
	end
	infobox_args['data31'] = args.allocation
	
	infobox_args['label32'] = "Tourist routes"
	infobox_args['data32'] = args.tourist
	infobox_args['label33'] = "Restrictions"
	infobox_args['data33'] = args.restrictions
	
	return frame:expandTemplate ({title='Infobox', args = infobox_args})
		
end

function p.info(frame)
	local args = getArgs(frame)
	return info(args)
end
	
local function bottom(args)	
	
	local ntitle = parser(args, 'nbrowse') or nil
	local nlist = parser(args, 'nbrowselinks') or nil
	local title = parser(args, 'browse') or nil
	local list = parser(args, 'browselinks') or nil
	local previousRoute = args.previous_route
	local nextRoute = args.next_route
	local extended = args.browse
	
	local infobox_args = {
		['child'] = "yes"
	}
	
	-- HIGHWAY SYSTEM --
	if args.nobrowse or country == nil then
		return nil
	elseif country == "GBR" or country == "ENG" or country == "NIR" or country == "SCT" or country == "WLS" or country == "GGY" or country == "IMN" or country == "JEY" or
		country == "AIA" or country == "BMU" or country == "IOT" or country == "VGB" or country == "CYM" or country == "FLK" or country == "GIB" or country == "MSR" or
		country == "PCN" or country == "SHN" or country == "SGS" or country == "TCA" then
		
		infobox_args['header5'] = "Road network"
	else
		infobox_args['header5'] = "Highway system"
	end
	
	-- LINKS AND BROWSE SECTION --

	if args.nobrowse then
		return nil
	else
		if ntitle ~= nil then
			infobox_args['rowclass10'] = "hlist"
			infobox_args['rowstyle10'] = "font-weight: bold;"
			infobox_args['data10'] = ntitle
		end
		if nlist ~= nil then
			infobox_args['rowclass11'] = "hlist"
			infobox_args['data11'] = nlist
		end
		if title ~= nil then
			infobox_args['rowclass12'] = "hlist"
			infobox_args['rowstyle12'] = "font-weight: bold;"
			infobox_args['data12'] = title
		end
		if list ~= nil then
			infobox_args['rowclass12'] = "hlist"
			infobox_args['data12'] = list
		end
	end
	
	infobox_args['data13'] = args.system
	
	if args.nobrowse then
		return nil
	else
		if previousRoute or nextRoute then
			infobox_args['rowstyle15'] = "width:100%; background:none; border-collapse:collapse; display:inline-table;"
			local boxModule = require "Module:Road data/browse/sandbox" -- remove "/sandbox" when live!
			infobox_args['data15'] = boxModule._browse(args)
		end
	end
	
	if args.nobrowse or extended == nil then
		return nil 
	else
		infobox_args['rowstyle17'] = "width:100%; background:none; border-collapse:collapse; display:inline-table;"
		infobox_args['data17'] = extended
	end
	
	infobox_args['data20'] = args.nhrp or args.embedded
	infobox_args['subbox'] = yesno(args.child)
	
	return frame:expandTemplate ({title='Infobox', args = infobox_args})
		
end

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

return p