Documentation for this module may be created at Module:Sandbox/Ajuanca/doc

-- Ajuanca Google Code-in 2019, Introduction to Lua in Wikipedia.

local p = {}

function p.hello( frame )
	return "Hello, world!"
end

p.Hi = function(frame)
	strName = frame.args.name or "Jimmy"
end

function p.converttemp(frame)
	cels = tonumber(frame.args.celsius) or 0
	fahr = (((cels * 9) / 5) + 32)
	kelvin = cels + 273.15
	str = cels .. " degrees Celsius is " .. fahr .. " degrees Fahrenheit."
	str = str .. " That's the same as say " .. kelvin .. " Kelvin."
	if cels > 9 then str = str .. " It is warm." elseif cels < 9 then str = str .. " It is cold." else str = str .. " It isn't neither warm nor cold." end
	return str
end

-- Task 4
-- Timestable function
function p.timestable(frame)
	local random = math.random(-12, 12)
	local numb = tonumber( frame.args.numb ) or random
	local out = "<h4> The " .. numb .. " times table. </h4>"
	out = out .. "When no number is given or no parameter is sended, the default value is used. Instead of 2, the default number is generated randomly between -12 and 12, and in this case is " .. random .. ".<br>"
	for i = 1, 12 do
		out = out .. i .. " times " .. numb .. " equals " .. i * numb .. "<br>"
	end
	return out
end

-- People function
function p.people(frame)
	local friends = {"Agnetha", "Betty", "Carlos", "Davinder", "Eloise", "Martin", "Doug", "Diego"}
	local msg = ""
	for i = 1, #friends do 
		msg = msg .. "Hello " .. friends[i] .. "<br>"
	end
	return msg
end

-- Task 5
-- Sent function
function p.sent(frame)
	local txt = frame.args.text or ""
	local firstLetterCapitalized = string.upper(string.sub(txt, 1, 1))
	local out = firstLetterCapitalized .. string.sub(txt, 2)
	return out
end

-- Unpack function
function p.unpack(frame)
	local dmy = frame.args.dmydate or ""
	local d, m, y = string.match(dmy, "(%d+) (%w+) (%d+)")
	local annualPosition = ordinalMonth(m)
	return "Year = " .. y .. "<br>Day = " .. d .. "<br>Month = " .. m .. ", that is the " .. annualPosition .. " month.<br>"
end

-- Unpack function for US date format
function p.unpackUS(frame)
	local mdy = frame.args.mdydate or ""
	local m, d, y = string.match(mdy, "(%w+) (%S+) (%d+)")
	d = string.sub(d, 1, #d - 1)
	local annualPosition = ordinalMonth(m)
	return "Year = " .. y .. "<br>Day = " .. d .. "<br>Month = " .. m .. ", that is the " .. annualPosition .. " month.<br>"
end

-- Get month position
function ordinalMonth(month)
	local normalizedMonth = string.lower(month)
	local annualPosition
	if normalizedMonth == "january" then annualPosition = "first" elseif normalizedMonth == "february" then annualPosition = "second" elseif normalizedMonth == "march" then annualPosition = "third" elseif normalizedMonth == "april" then annualPosition = "fourth" elseif normalizedMonth == "may" then annualPosition = "fifth" elseif normalizedMonth == "june" then annualPosition = "sixth" elseif normalizedMonth == "july" then annualPosition = "seventh" elseif normalizedMonth == "august" then annualPosition = "eighth" elseif normalizedMonth == "september" then annualPosition = "nineth" elseif normalizedMonth == "october" then annualPosition = "tenth" elseif normalizedMonth == "november" then annualPosition = "eleventh" elseif normalizedMonth == "december" then annualPosition = "twelfth" end 
	return annualPosition
end

-- Task 6
-- Langs function.
function p.langs(frame)
	local langslist = mw.language.fetchLanguageNames()
	local out = ""
	local count = 0
	for k, v in pairs(langslist) do
		out = out .. k .. " - " .. v .. "<br>"
		count = count + 1
	end
	return out .. "<br>= " .. count .. " languages"
end

-- Fallbacklangs function.
function p.fallbacklangs(frame)
	local fallbackLanguages = mw.language.getFallbacksFor(frame.args.langcode)
	local out = ""
	local correctThere = ""
	local count = 0
	for k, v in pairs(fallbackLanguages) do
		out = out .. k .. " - " .. v .. "<br>"
		count = count + 1
	end
	if count == 1 then correctThere = "There's " else correctThere = "There're " end
	return "<h5>" .. frame.args.langcode .. " fallback languages</h5>" .. out .. correctThere .. count .. " possible plan-Bs languages."
end

-- Page title function.
p.pgtitle = function(frame)
	local title = frame.args.title
	local ttlobj = mw.title.new(title)
	local txt = ttlobj.text
	return txt
end

-- Page information function.
function p.pginfo(frame)
	local titleOfPage = frame.args.titleOfPage
	local titleObject = mw.title.new(titleOfPage)
	local output = ""
	if titleObject == nil then output = "You need to give a value to the titleOfPage attribute." else 
		if titleObject.exists then 
			output = titleOfPage .. " exists"
			if titleObject.isRedirect then output = output .. " and is a redirect." elseif titleObject.isTalkPage then 
			output = output .. " and is a talk page." elseif titleObject.isSubpage then output = output .. " and is a subpage."
			else output = output .. " and isn't a redirect." end 
		else output = titleOfPage .. " doesn't exist and isn't a redirect." end end
	return  output
end

-- Reutrn list with languages organized depending on number of fallback-languages.
function p.orgnanizeLanguages()
	local allLanguages = mw.language.fetchLanguageNames()
	local numberedLanguages = {}
	local out = ""
	for k, v in pairs(allLanguages) do
		element = {language = v, languageTag = k, numberOfFallbacks=tonumber(#mw.language.getFallbacksFor(k))}
		table.insert(numberedLanguages, element)
	end
	--table.sort(numberedLanguages, sortByFallbacks)
	function sortByFallbacks(a,b)
		if a.numberOfFallbacks>b.numberOfFallbacks then
			return true
		else
			return false
		end
	end
	for x, p in ipairs(numberedLanguages)do
		for c, s in ipairs(p)do
			out = out .. x
		end
	end
	return out
end

-- Task 7
-- Get date.
function p.getdate(frame)
	local qid = frame.args.qid
	local prop = frame.args.prop
	local valtbl = mw.wikibase.getBestStatements(qid, prop)
	local timestamp = valtbl[1].mainsnak.datavalue.value.time
	local y, m, d = string.match(timestamp, "(%d+)-(%d+)-(%d+)")
	local str = "The year is " .. y .. ", the month is " .. m .. " and the day is " .. d .. ".<br>This date in an ISO-style is "
	.. y .. "-" .. m .. "-" .. d .. "."
	return str
end

-- Readable date
function p.getfulldate(frame)
	local monthname = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }
	local qid = frame.args.qid
	local prop = frame.args.prop
	local valtbl = mw.wikibase.getBestStatements(qid, prop)
	local timestamp = valtbl[1].mainsnak.datavalue.value.time
	local y, m, d = string.match(timestamp, "(%d+)-(%d+)-(%d+)")
	local str = d .. " " .. monthname[tonumber(m)] .. " " .. y
	return str
end

-- Get structure of item
function p.getitem(frame)
	local qid = frame.args.qid
	local prop = frame.args.prop
	local valtbl = mw.wikibase.getBestStatements(qid, prop)
	local idstamp = valtbl[1].mainsnak.datavalue.value.id
	local title = mw.wikibase.getLabel(idstamp)
	-- local tbl = mw.wikibase.entity:formatPropertyValues('P42')
	return title
end

return p