require('strict');
local yesno = require('Module:Yesno')  
local p = {}
local g = {} -- for parameters with global scope in this module
g.goalscorers = {} -- table where selected and sorted players will be place
g.args = {}
g.totalGoals = 0
local data = {} -- module subpage data -- require('Module:Goalscorers/data/UEFA Euro 2016 qualifying');  
p.errorString = ""
function p.error_msg
if p.errorString ~= "" then
return '<span>'
-- '<code>&#124;_template=</code>'
.. p.errorString .. '</span>';
end
end
-- data for goals scored held in module subpages, e.g. "Module:Goalscorers/data/UEFA Euro 2016 qualifying"
-- parameters containing data help in three tables
data.rounds = {} -- group, play-off
data.goalscorers = {} -- player, country, goals in each round)
data.owngoalscorers = {} -- player, country, goals in each round)
data.updated = {} -- date of latest update (month, day, year)
---  
-- ############################ Parameter handing ###############################
p.getArgs - gets arguments from (invoke) or parent (template)  
local function getArgs
local parents = mw.getCurrentFrame:getParent  
for k,v in pairs(parents.args) do
--check content
if v and v ~= "" then
g.args[k]=mw.text.trim(v) --parents.args[k]
end
end
for k,v in pairs(.args) do
--check content
if v and v ~= "" then
g.args[k]= mw.text.trim(v) --parents.args[k]
end
end
-- allow empty caption to blank default
--if parents.args['caption'] then templateArgs['caption'] = parents.args['caption'] end
end  
-- ############################## Main function and other functions ######################
p.main - simple output of the data in the module in list form
p.addIntroductorySentence - add sentence on number of goals and matches, with goals per match
p.addFooterSentence - add footnote
p.getNumberMatches
p.owngoals - get own goals (no longer used?)
p._owngoals - core functionality for p.owngoals  
function p.main
getArgs
local dataTarget = g.args[1] or g.args['data']
if dataTarget then
data = require('Module:Goalscorers/data/'.. dataTarget) --or 'UEFA Euro 2016 qualifying'
return p.useModuleData -- data on goals taken from module subpage
else
return p.useTemplateData -- data on goals/assists taken from template
end  
end
function p.useModuleData  
--p.goalscorers = {} -- table where selected and sorted players will be place
g.totalGoals = 0
local ok = p.selectGoalscorers -- selected goalscorers meeting round and group criteris
if not ok then return p.error_msg end  
-- CHANGE: append own goals to list (data will now include goals and own goals (negative))
p.selectGoalscorers("OG")  
p.sortGoalscorers -- sort selected goalscorers by number of goal, then country  
local outputString = p.addIntroductorySentence .. p.outputGoalscorers .. p.addFooterSentence
-- .. "" --TODO add intermediate heading?
-- .. p._owngoals -- output list of goalscorers  
return p.error_msg or outputString
end  
function p.addIntroductorySentence -- add introductory text  
local totalGoalString = "மொத்தம் " .. g.totalGoals .. " இலக்குகள் அடிக்கப்பட்டன."
--There were [has been|have been|was|were] #GOALS goal(s) scored in #MATCHES match(s), for an average of #GOALS/#MATCHES per match.  
local matches, dateUpdated = p.getNumberMatches
local mdyFormat = yesno(g.args['mdy'])  
local Date = require('Module:Date')._Date  
local pluralGoals = "கள்"
local text1 = ""
if g.totalGoals == 1 then
pluralGoals = ""
if dateUpdated == 'complete' then text1 = "was" else text1 = "has been" end
else
if dateUpdated == 'complete' then text1 = "were" else text1 = "have been" end
end
local text = string.format("There %s %s goal%s scored", text1, mw.getLanguage('en'):formatNum(g.totalGoals), pluralGoals)  
local pluralMatches = "es"
if matches==1 then pluralMatches = "" end
if matches then
local average = g.totalGoals/tonumber(matches)
local precision = 3 -- display d.dd (three significant disgits)
if average < 1 then precision = 2 end -- display 0.dd (thwo significant disgits)
average = tostring (average)  
local pluralAverage = "s"
if tonumber(string.format("%.2f",average))==1 then pluralAverage = "" end
text = text .. string.format(" in %d match%s, for an average of %."..precision.."g goal%s per match", matches, pluralMatches, average, pluralAverage)
end  
if dateUpdated == 'complete' or dateUpdated == "" then
text = text .. "."
else
local dateFormat = 'dmy' -- default
if data.params and data.params['date_format'] then dateFormat = data.params['date_format'] end -- from data module
if mdyFormat == true then dateFormat = "mdy" else
if mdyFormat == false then dateFormat = "dmy" end -- template param overrides
end
text = text .. " (as of " .. Date(dateUpdated):text(dateFormat) .. ")."
end
text = p.addAdditionHeaderText(text, dateUpdated) -- handles template parameters bold, further, extra  
return text --totalGoalString
end
function p.addFooterSentence -- add notes at bottom  
local footerSentence = g.args['footer'] or ""
--footerSentence = "This is a footer sentence." -- test footer
if data.params then
local footer = data.params['footer'] or nil
if footer then
local = mw.getCurrentFrame
local processed = :preprocess(footer)
if g.notes then
footerSentence = footerSentence .. processed
end
end
end  
if footerSentence ~= "" then
footerSentence = '<div >' .. footerSentence .. '</div>'
end
return footerSentence
end
function p.getNumberMatches
local matches = g.args['matches']
local dateUpdated = data.updated['date'] or "1700-01-01" --'complete' -- assume completed if missing  
--local round = g.args['round'] or "all" -- round = all(empty)|group|playoffs
--local group = g.args['group'] or "all" -- group = all(empty), A,B,C etc
local round, group = p.getRoundAndGroup  
local allGroupGames = 0
local latestGroupDate = "1800-01-01"
if group and (round == "all" or group == "all") then -- count all the group games
for k,v in pairs(data.updated.group) do
allGroupGames = allGroupGames + v[1]
if v[2] ~= "complete" and v[2] > latestGroupDate then latestGroupDate = v[2] end -- update if later date
end
if latestGroupDate == "1800-01-01" then latestGroupDate = "complete" end -- no dates so must be complete
end
if group and (round == "all" and group ~= "all") then -- for totals of all rounds with only one group
allGroupGames = data.updated.group[group][1] -- number matches
latestGroupDate = data.updated.group[group][2] -- update date or completed
end
if round == "all" then -- all rounds and goals
matches=0
for k,v in pairs(data.updated) do
if k == "group" then
matches = matches + allGroupGames
if latestGroupDate ~= "complete" and latestGroupDate > dateUpdated then
dateUpdated = latestGroupDate -- update if later date
end
elseif p.validateRound(k) then
matches = matches + v[1]
if v[2] ~= "complete" and v[2] > dateUpdated then dateUpdated = v[2] end -- update if later date
end  
end
elseif round == "group" then -- group round only
if group == "all" then
matches = allGroupGames
dateUpdated = latestGroupDate
else -- single group only
matches = data.updated.group[group][1] -- number matches
dateUpdated = data.updated.group[group][2] -- update date or completed
end
else -- any other round
matches = data.updated[round][1] -- number matches
dateUpdated = data.updated[round][2] -- update date or completed
end  
if dateUpdated == "1700-01-01" then dateUpdated = "complete" end -- no dates so must be complete  
return matches, dateUpdated
end  
function p.owngoals -- need to check parameters if external call
getArgs
data = require('Module:Goalscorers/data/'.. g.args[1]) --or 'UEFA Euro 2016 qualifying'  
local outputString = p._owngoals
return p.error_msg or outputString
end
function p._owngoals -- internal call for own goals  
--p.goalscorers = {} -- table where selected and sorted players will be place  
p.selectGoalscorers("OG") -- selected goalscorers meeting round and group criteris  
p.sortGoalscorers -- sort selected goalscorers by number of goal, then country  
return p.outputGoalscorers(, "OG") -- output list of goalscorers  
end
function p.validateRound(round)