local p = {}

local Navbox = require("Module:Navbox")
local country = {
	[1] = {"Afghanistan"},
	[2] = {"Albania"},
	[3] = {"Algeria"},
	[4] = {"Andorra"},
	[5] = {"Angola"},
	[6] = {"Antigua and Barbuda"},
	[7] = {"Argentina"},
	[8] = {"Armenia"},
	[9] = {"Australia"},
	[10] = {"Austria"},
	[11] = {"Azerbaijan"},
	[12] = {"Bahamas","the Bahamas"},
	[13] = {"Bahrain"},
	[14] = {"Bangladesh"},
	[15] = {"Barbados"},
	[16] = {"Belarus"},
	[17] = {"Belgium"},
	[18] = {"Belize"},
	[19] = {"Benin"},
	[20] = {"Bhutan"},
	[21] = {"Bolivia"},
	[22] = {"Bosnia and Herzegovina"},
	[23] = {"Botswana"},
	[24] = {"Brazil"},
	[25] = {"Brunei"},
	[26] = {"Bulgaria"},
	[27] = {"Burkina Faso"},
	[28] = {"Burma"},
	[29] = {"Burundi"},
	[30] = {"Cambodia"},
	[31] = {"Cameroon"},
	[32] = {"Canada"},
	[33] = {"Cape Verde"},
	[34] = {"Central African Republic", "the Central African Republic"},
	[35] = {"Chad"},
	[36] = {"Chile"},
	[37] = {"China"},
	[38] = {"Colombia"},
	[39] = {"Comoros"},
	[40] = {"Costa Rica"},
	[41] = {"Croatia"},
	[42] = {"Cuba"},
	[43] = {"Cyprus"},
	[44] = {"Czech Republic", "the Czech Republic"},
	[45] = {"Democratic Republic of the Congo", "the Democratic Republic of the Congo"},
	[46] = {"Denmark"},
	[47] = {"Djibouti"},
	[48] = {"Dominica"},
	[49] = {"East Timor"},
	[50] = {"Dominican Republic", "the Dominican Republic"},
	[51] = {"Ecuador"},
	[52] = {"Egypt"},
	[53] = {"El Salvador"},
	[54] = {"Equatorial Guinea"},
	[55] = {"Eritrea"},
	[56] = {"Estonia"},
	[57] = {"Ethiopia"},
	[58] = {"Federated States of Micronesia", "the Federated States of Micronesia"},
	[59] = {"Fiji"},
	[60] = {"Finland"},
	[61] = {"France"},
	[62] = {"Gabon"},
	[63] = {"Gambia", "the Gambia"},
	[64] = {"Georgia", "Georgia (country)"},
	[65] = {"Germany"},
	[66] = {"Ghana"},
	[67] = {"Greece"},
	[68] = {"Grenada"},
	[69] = {"Guatemala"},
	[70] = {"Guinea"},
	[71] = {"Guinea-Bissau"},
	[72] = {"Guyana"},
	[73] = {"Haiti"},
	[74] = {"Honduras"},
	[75] = {"Hungary"},
	[76] = {"Iceland"},
	[77] = {"India"},
	[78] = {"Indonesia"},
	[79] = {"Iran"},
	[80] = {"Iraq"},
	[81] = {"Ireland"},
	[82] = {"Israel"},
	[83] = {"Italy"},
	[84] = {"Ivory Coast"},
	[85] = {"Jamaica"},
	[86] = {"Japan"},
	[87] = {"Jordan"},
	[88] = {"Kazakhstan"},
	[89] = {"Kenya"},
	[90] = {"Kiribati"},
	[91] = {"Kuwait"},
	[92] = {"Kyrgyzstan"},
	[93] = {"Laos"},
	[94] = {"Latvia"},
	[95] = {"Lebanon"},
	[96] = {"Lesotho"},
	[97] = {"Liberia"},
	[98] = {"Libya"},
	[99] = {"Liechtenstein"},
	[100] = {"Lithuania"},
	[101] = {"Luxembourg"},
	[102] = {"Republic of Macedonia","the Republic of Macedonia"},
	[103] = {"Madagascar"},
	[104] = {"Malawi"},
	[105] = {"Malaysia"},
	[106] = {"Maldives"},
	[107] = {"Mali"},
	[108] = {"Malta"},
	[109] = {"Marshall Islands", "the Marshall Islands"},
	[110] = {"Mauritania"},
	[111] = {"Mauritius"},
	[112] = {"Mexico"},
	[113] = {"Moldova"},
	[114] = {"Monaco"},
	[115] = {"Mongolia"},
	[116] = {"Montenegro"},
	[117] = {"Morocco"},
	[118] = {"Mozambique"},
	[119] = {"Namibia"},
	[120] = {"Nauru"},
	[121] = {"Nepal"},
	[122] = {"Netherlands", "the Netherlands"},
	[123] = {"New Zealand"},
	[124] = {"Nicaragua"},
	[125] = {"Niger"},
	[126] = {"Nigeria"},
	[127] = {"North Korea"},
	[128] = {"Norway"},
	[129] = {"Oman"},
	[130] = {"Pakistan"},
	[131] = {"Palau"},
	[132] = {"Panama"},
	[133] = {"Papua New Guinea"},
	[134] = {"Paraguay"},
	[135] = {"Peru"},
	[136] = {"Philippines", "the Philippines"},
	[137] = {"Poland"},
	[138] = {"Portugal"},
	[139] = {"Qatar"},
	[140] = {"Republic of the Congo", "the Republic of the Congo"},
	[141] = {"Romania"},
	[142] = {"Russia"},
	[143] = {"Rwanda"},
	[144] = {"Saint Kitts and Nevis"},
	[145] = {"Saint Lucia"},
	[146] = {"Saint Vincent and the Grenadines"},
	[147] = {"Samoa"},
	[148] = {"San Marino"},
	[149] = {"São Tomé and Príncipe"},
	[150] = {"Saudi Arabia"},
	[151] = {"Senegal"},
	[152] = {"Serbia"},
	[153] = {"Seychelles"},
	[154] = {"Sierra Leone"},
	[155] = {"Singapore"},
	[156] = {"Slovakia"},
	[157] = {"Slovenia"},
	[158] = {"Solomon Islands", "the Solomon Islands"},
	[159] = {"Somalia"},
	[160] = {"South Africa"},
	[161] = {"South Korea"},
	[162] = {"South Sudan"},
	[163] = {"Spain"},
	[164] = {"Sri Lanka"},
	[165] = {"Sudan"},
	[166] = {"Suriname"},
	[167] = {"Swaziland"},
	[168] = {"Sweden"},
	[169] = {"Switzerland"},
	[170] = {"Syria"},
	[171] = {"Taiwan"},
	[172] = {"Tajikistan"},
	[173] = {"Tanzania"},
	[174] = {"Thailand"},
	[175] = {"Togo"},
	[176] = {"Tonga"},
	[177] = {"Trinidad and Tobago"},
	[178] = {"Tunisia"},
	[179] = {"Turkey"},
	[180] = {"Turkmenistan"},
	[181] = {"Tuvalu"},
	[182] = {"Uganda"},
	[183] = {"Ukraine"},
	[184] = {"United Arab Emirates", "the United Arab Emirates"},
	[185] = {"United Kingdom", "the United Kingdom"},
	[186] = {"United States", "the United States"},
	[187] = {"Uruguay"},
	[188] = {"Uzbekistan"},
	[189] = {"Vanuatu"},
	[190] = {"Vatican City"},
	[191] = {"Venezuela"},
	[192] = {"Vietnam"},
	[193] = {"Yemen"},
	[194] = {"Zambia"},
	[195] = {"Zimbabwe"},
}

function p.main(frame)
	local pframe = frame:getParent()
	local config = frame.args
	local args = pframe.args
	
	return p._main(args)
end

function p._main(args)
	if mw.title.getCurrentTitle() == "Template:World topic" then
		state = "autocollapse"
	else
		state = args.state
	end
	if args[1] ~= nil or args.prefix ~= nil and (args[2] == nil or ags.suffix == nil) then
		titlemid = " the"
	end
	if args.title ~= nil then
		title = args.title
	else
		title = "[["..(args[1] or args.prefix or "")..(titlemid or "").." World"..(args.suffix or "").."]]"
	end
	if args.group1 ~= nil and args.group1 ~= "" then
		group1 = args.group1
	end

	if args.noredlinks ~= nil then
		list1 = table.concat(noredlinks(args[1], args[2], args.prefix, args.suffix) )
	else
		list1 = table.concat(redlinks(args[1], args[2], args.prefix, args.suffix) )
	end
	local navarguments = {
		name = args.name or "World topic",
		state = state,
		navbar = args.navbar or "Tnavbar",
		border = args.border,
		title = title,
		image = args.image,
		titlestyle = args.titlestyle,
		bodystyle = args.bodystyle,
		abovestyle = args.abovestyle,
		belowstyle = args.belowstyle,
		groupstyle = args.groupstyle,
		liststyle = args.liststyle,
		listclass  = "hlist",
		above = args.above,
		group1 = group1,
		list1 = list1
	}
	return Navbox._navbox(navarguments)
end

function noredlinks(args1, args2, argsprefix, argssuffix)
	local list = {}
	local i = 1
	for x, y in pairs(country) do
		if mw.title.new((args1 or argsprefix or "").." "..(country[i][2] or country[i][1])..(args2 or argssuffix or "")).exists == true then
			table.insert(list, li("[["..(args1 or argsprefix or "").." "..(country[i][2] or country[i][1])..(args2 or
				argssuffix or "").."|"..country[i][1].."]]"))
		elseif country[i][1] == "Georgia" then
			if mw.title.new((args1 or argsprefix or "").." "..country[i][1]..(args2 or argssuffix or "")).exists == true then
				table.insert(list, li("[["..(args1 or argsprefix or "").." "..country[i][1]..(args2 or
				argssuffix or "").."|"..country[i][1].."]]" ) )
			end
		end
		i = i + 1
	end
	return list
end

function redlinks(args1, args2, argsprefix, argssuffix)
	local list = {}
	local i = 1
	for x, y in pairs(country) do
		if country[i][1] == "Georgia" then
			if mw.title.new((args1 or argsprefix or "").." "..country[i][2]..(args2 or argssuffix or "")).exists == true then
				table.insert(list, li("[["..(args1 or argsprefix or "").." "..country[i][2]..(args2 or
				argssuffix or "").."|"..country[i][1].."]]" ))
			else
				table.insert(list, li("[["..(args1 or argsprefix or "").." "..country[i][1]..(args2 or
				argssuffix or "").."|"..country[i][1].."]]" ))
			end
		else
			table.insert(list, li("[["..(args1 or argsprefix or "").." " .. (country[i][2] or country[i][1]) .. (args2 or 
				argssuffix or "").."|".. country[i][1] .. "]]"))
		end
		i = i + 1
	end
	return list
end

function li(text)
	local li = mw.html.create("li")
	li
		:wikitext(text)
		:done()
	return tostring(li)
end

return p