# Module:Infobox road/sections

local p = {}  

local getArgs = require('Module:Arguments').getArgs  
local lengthModule = require("Module:Infobox road/length")  
local parserModule = require("Module:Road data/parser")  
local parser = parserModule.parser  
local = 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(args)  
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 freeway then  
return "header-MUTCDblue"  
elseif uc then  
return "header-uc"  
elseif minor then  
return "header-minor"  
elseif deleted and header == 'hist' or deleted and color == 'hist' then  
return "header-deleted-hist"  
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  

function p._section(args)  
if args.section1 or args.length_km1 or args.length_mi1 or args.length_notes1 or args.direction_a1 or  
args.terminus_a1 or args.junction1 or args.junctions1 or args.direction_b1 or args.terminus_b1 then  

local infobox_args = {  
['child'] = "yes",  
['decat'] = "yes",  
['headerclass'] = p.headerStyle(args)  
}  
local i = 1  
while (1) do  
local num = i  
local j = ((i - 1) * 5) + 1  
local headern = "header" .. j  
local lengthLabeln = "label" .. (j + 1)  
local lengthDatan = "data" .. (j + 1)  
local directionAn = "label" .. (j + 2)  
local terminusAn = "data" .. (j + 2)  
local jctClassn = "class" .. (j + 3)  
local jctLabeln = "label" .. (j + 3)  
local jctDatan = "data" .. (j + 3)  
local directionBn = "label" .. (j + 4)  
local terminusBn = "data" .. (j + 4)  

if args['section' .. i] or args["length_km" .. i] or args["length_mi" .. i] or args["length_notes" .. i] or args["direction_a" .. i] or  
args["terminus_a" .. i] or args["junction" .. i] or args["junctions" .. i] or args["direction_b" .. i] or args["terminus_b" .. i] then  

if i == 1 then  
infobox_args['title'] = args['section' .. i] or "Section " .. i  
else  
infobox_args[headern] = args['section' .. i] or "Section " .. i  
end  
end  

infobox_args[lengthLabeln] = "Length"  
infobox_args[lengthDatan] = lengthModule._length(num, args)  

local dir_a = args["direction_a" .. i] or args.direction_a or ''  
if dir_a ~= '' then  
infobox_args[directionAn] = dir_a .. "&nbsp;end"  
else  
infobox_args[directionAn] = "From"  
end  

infobox_args[terminusAn] = args["terminus_a" .. i]  
infobox_args[jctClassn] = "plainlist"  
infobox_args[jctLabeln] = "Major intersections"  
infobox_args[jctDatan] = args["junction" .. i] or args["junctions" .. i]  

local dir_b = args["direction_b" .. i] or args.direction_b or ''  
if dir_b ~= '' then  
infobox_args[directionBn] = dir_b .. "&nbsp;end"  
else  
infobox_args[directionBn] = "From"  
end  

infobox_args[terminusBn] = args["terminus_b" .. i]  

if i == 25 then  
break  
else  
i = i + 1  
end  
end  

return :expandTemplate ({title='Infobox', args = infobox_args})  

else return nil  
end  
end  

function p.section  
args = getArgs  
return p._section(args)  
end  

local function loop(args)  
local ring_road = args.ring_road or ''  
if ring_road ~= '' then  
return "Ring road around " .. ring_road  
end  
local beltway_city = args.beltway_city or ''  
if beltway_city ~= '' then  
return "Beltway around " .. beltway_city  
end  
local orbital = args.orbital or ''  
if orbital ~= '' then  
return "Orbital around " .. orbital  
end  
local loop = args.loop or ''  
if loop ~= '' then  
return "Loop around " .. loop  
end  
local tloop = args.tloop or ''  
if tloop == '' then  
return "Tourist loop"  
elseif tloop ~= '' then  
return "Tourist loop around " .. tloop  
end  
return nil  
end  

function p._main(args)  

if args.terminus_a or args.junction or args.junctions or args.terminus_b or args.ring_road or args.beltway_city or  
args.orbital or args.loop or args.tloop then  

local infobox_args = {  
['child'] = "yes",  
['decat'] = "yes",  
}  

infobox_args['title'] = args.section0 or "Major junctions"  
infobox_args['data1'] = loop(args)  

local dir_a = args.direction_a or ''  
if dir_a ~= '' then  
infobox_args['label2'] = dir_a .. "&nbsp;end"  
else  
infobox_args['label2'] = "From"  
end  

infobox_args['data2'] = args.terminus_a  
infobox_args['class3'] = "plainlist"  
infobox_args['label3'] = "<span>Major intersections</span>"  
infobox_args['data3'] = args.junction or args.junctions  

local dir_b = args.direction_b or ''  
if dir_b ~= '' then  
infobox_args['label4'] = dir_b .. "&nbsp;end"  
else  
infobox_args['label4'] = "To"  
end  

infobox_args['data4'] = args.terminus_b  

return :expandTemplate ({title='Infobox', args = infobox_args})  

else  
return nil  
end  
end  

function p.main  
args = getArgs  
return p._main(args)  
end  

return p