Mô đun:SportsRankings

require('strict');
local p = {} 
local error_msg = '|_template= thiếu hoặc trống';
-- data for various rankings held in module subpages, e.g. "Mô đun:SportsRankings/data/FIFA World Rankings"
local data = {}      -- parameters containing data help in three tables
						data.source = {}     -- parameters for using in cite web (title, url, website)
						data.updated = {}    -- date of latest update (month, day, year)
						data.rankings = {}   -- the rankings list (country code, ranking, movement)
					    data.alias = {}      -- alias list (country code, country name =key)
					    
					--
local  templateArgs = {} -- contains arguments from template involking module
local function getArgs(frame)
	local parents = mw.getCurrentFrame():getParent()
		
	for k,v in pairs(parents.args) do
		--check content
		if v and v ~= "" then
			templateArgsk=v --parents.argsk
		end
	end
	for k,v in pairs(frame.args) do
		--check content
		if v and v ~= "" then
			templateArgsk=v --parents.argsk
		end
	end
	-- allow empty caption to blank default
	if parents.args'caption' then templateArgs'caption' = parents.args'caption' end
end
local function loadData(frame)

local source = frame.args1 -- source of rankings e.g. FIFA World Rankings
data = require('Mô đun:SportsRankings/data/'.. source);

end
local function getDate(option)

local dateTable = data.updated         -- there must be date table (data.updated)
-- TODO add a warning and/or category
if option == "LAST" then 
		local lastDateTable = data.previous 
		if lastDateTable then             -- there might not be a previous data table (data.previous)
			dateTable = lastDateTable
	   else 
	   		return "Không có ngày trước có giá trị (thiếu data.updated)"
end
end

if templateArgs'mdy' and templateArgs'mdy' ~= "" then
	   return dateTable'month' .. " " .. dateTable'day' .. ", " .. dateTable'year'
else
	   return dateTable'day' .. " " .. dateTable'month' .. " " .. dateTable'year'
end
end
local function addCiteWeb(frame)  -- use cite web template
	
	return frame:expandTemplate{ title = 'chú thích web' , args = {
		url = data.source'url',            --"https://www.fifa.com/fifa-world-ranking/ranking-table/men/index.html", 
			title = data.source'title',        -- "The FIFA/Coca-Cola World Ranking",
			website = data.source'website',    --"FIFA",
			'date' = getDate(),
			'access-date' = getDate()
			}}
end
local function addReference(frame)
	
	local text = ""
	if data.source'text' then text = data.source'text' end
	
	return frame:expandTemplate{ title = 'refn' , args = {
		name=frame.args1,                                 --ranking used, e.g. "FIFA World Rankings",
	    text .. addCiteWeb(frame)
	}}
end
--date
function p.main(frame)
	
getArgs(frame) -- returns args table having checked for content
loadData(frame)
local outputString = ""
local validCode = false
local country = templateArgs2 -- country name or county code passed as parameter
local rank, move

if string.len( country) ==  3 then -- if we have a three letter country code 
	    for _,u in pairs(data.alias) do  -- run through alias list { 3-letter code, country name }
	    	if u1==country then        -- if code = passed parameter
	       		country = u2           -- set country name as key for ranking table
	       		validCode = true
	       		break
	       	end
	    end    
	    -- if no match of code to country name, set category
	    if not validCode then
	    	outputString="Thể loại:Trang sử dụng SportsRankings có các tham số không rõ" .. outputString
end
end
for _,v in pairs(data.rankings) do
	if v1==country then 
		rank = v2    -- get rank
		move = v3    -- get move from last ranking
		break
	end
end
if not rank then -- no ranking found (do we want a tracking for no rank found?)
	rank = 'NR' 
	    --outputString="Thể loại:Trang sử dụng SportsRankings có các tham số không rõ" .. outputString
--outputString="Thể loại:Trang sử dụng SportsRankings không có xếp hạng" .. outputString
	end
	
	if rank ~= 'NR' then
		outputString = outputString .. ' ' .. rank .. ' '
		if move  0 then                                 -- if ranking up
	    	outputString = outputString .. frame:expandTemplate{ title = 'increase' } .. ' ' .. move
	    end	
else
	outputString = outputString .. frame:expandTemplate{ title = 'Abbr', args = { "NR", "Không được xếp hạng"}  }
	--	Không được xếp hạng 
	end
	outputString = outputString .. ' (' .. getDate() .. ')'
	outputString = outputString .. addReference(frame)

return outputString
	
end
--caption=
local function table(frame, ranking, first,last)
local styleString = ""
if templateArgs'style' and templateArgs'style' ~= "" then styleString = templateArgs'style' end


local sublist2 = { "ENG", "SCO", "WAL", "IRE", "NIR", "FRA", "Anh", "Pháp", "Đức" }
local sublist3 = { "AFG","AUS","BAN","BHR","BHU","BRU","CAM","CHN","GUM","HKG","IDN","IND","IRN","IRQ","JOR",
	              "JPN","KGZ","KOR","KSA","KUW","LAO","LIB","MAC","MAS","MDV","MNG","MYA","NEP","OMA","PAK",
	              "PHI","PLE","PRK","QAT","SIN","SRI","SYR","THA","TJK","TKM","TLS","TPE","UAE","UZB","VIE",
	              "YEM" }
local lastRank = 0
local selectCount = 0
local selectData = nil
local selectList = nil
if templateArgs'select' then 
	if data.confederationtemplateArgs'select' then
	    	selectList = templateArgs'select'
	    	selectData = data.confederationselectList
	    	selectCount = 1
	end
end

-- column header customisation
local rankHeader = templateArgs'rank_header' or "Hạng"
local selectionHeader = templateArgs'selection_header' or selectList or "Hạng"
local teamHeader = templateArgs'team_header' or "Đội tuyển"
local pointsHeader = templateArgs'points_header' or "Điểm"
local changeHeader = templateArgs'change_header' or "Thay đổi"

--start table
local outputString = '"
return outputString
	
end
function p.replaceKeywords(keyword)
keyword =  string.gsub( keyword, "INSERT_UPDATE_DATE", getDate())
keyword =  string.gsub( keyword, "INSERT_LAST_DATE", getDate("LAST"))
keyword =  string.gsub( keyword, "INSERT_REFERENCE", addReference(mw.getCurrentFrame()))
return keyword
end
--last=   -- first and last ranking to display (defaults 1-10)
function p.list(frame)
getArgs(frame) -- returns args table having checked for content
loadData(frame)	
local ranking = frame.args1
local first, last = 1,10
first = tonumber(frame.args'2')
last = tonumber(frame.args'3')

return table(frame, ranking, first, last)
end
--span     
-- ranking - the ranking to display (e.g. FIFA World Rankings)
-- country - country table is centred around
-- span=   - rows to display above and below country
function p.list2(frame)
getArgs(frame) -- returns args table having checked for content
loadData(frame)	
local ranking = frame.args1
local first, last = 1,10
local country = frame.args2           -- name or code of country to center table around
local span = frame.args3 or 2         -- number of rows to display above and below country (default:2)

if string.len(country) == 3 then        -- if three letter country code
		for _,u in pairs(data.alias) do
		    	if u1==country then 
		       		country = u2          -- if country code then use country name 
		       		break
		       	end
		    end   
end
for k,v in pairs(data.rankings) do      -- find position of country in rankings
if v1 == country then
	  first = v2-span
	  last = v2+span
end
end

return table(frame, ranking, first, last)
end
return p