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

local p = {}

local trim = mw.text.trim
local split = mw.text.split
local tableParser = mw.text.listToText

local data = {}
local translation = {}
local english = {}
local order = {}
local langCode = ""
local titlearg

function check(string)
	return string and #tostring(string) > 0
end

function toTable(table)
	local bck = {}
	for k,v in pairs(table) do
		bck[k] = v
	end
	return bck
end

function transform(inp, toType)
	if type(inp) == toType then
		return inp
	end
	if toType == "string" then
		return tostring(inp)
	end
	if toType == "number" then
		return tonumber(inp)
	end
end

function translate(string)
	if string == "pagename" then
		string = titlearg or string
	end
	if string == "pagename" and data.translated then
		return data.translated[langCode] or translation[string] or english[string] or string
	end
	return translation[string] or english[string] or string
end

function replace(string, fnd, repl)
	local splits = split(string, fnd,true)
	local bck = splits[1]
	for i = 2, #splits do
		bck = bck .. repl .. splits[i]
	end
	return bck
end

function special(value, rules)
	-- Only run if a special tag is in the "order"
	local i = 1
	while rules[i] do
		-- Loop through all the rules until a good one is found
		-- (while cause it's a fake table)
		cur = rules[i]

		-- Use of "copy" cause the source needs to stay its type
		local copy
		if check(cur.transform) then
			copy = transform(value,cur.transform)
		else
			copy = value
		end

		-- Transformation of fake table to real table so the count parameter can be used
		if type(copy) == "table" then
			copy = toTable(copy)
		end

		local ok = true -- This rule is good until there is a problem
		local req = cur.require
		if check(req) then
			ok = ok and ( ( not check(req.count) ) or ( #copy == req.count) )
			ok = ok and ( ( not check(req.type) ) or ( type(copy) == req.type) )
			ok = ok and ( ( not check(req.regex) ) or not(mw.ustring.gmatch( copy, req.regex )() == nil) )
			--  http://dev.wikia.com/wiki/Lua_reference_manual/Standard_libraries#Patterns
			--  http://dev.wikia.com/wiki/Lua_reference_manual/Scribunto_libraries#Ustring_patterns
		end

		if ok then
			local table = {}
			if type(cur.result) == "string" then
				return replace(cur.result,"$",transform(copy,"string"))
			elseif type(cur.result) == "table" then
				for k,v in pairs(cur.result.format) do
					if type(v) == "string" then
						table[k] = replace(v,"$",copy)
						if table[k] == v then
							table[k] = translate(v)
						end
					elseif type(v) == "number" then
						table[k] = copy[v]
					end
				end
				return tableParser(table,
					translate(cur.result.separatorMid or "separatorMid"),
					translate(cur.result.separatorEnd or "separatorEnd")
				)
			end
		end
		i = i + 1
	end
	return "Data was suplied in the wrong format."
end

function addData(value, info)
	q = value or info.default

	if not(check(q)) then
		return {}
	end

	ret = {}
	ret.type = info.tag
	if check(info.label) then
		ret.label = translate(info.label)
	end


	if check(info.special) then
		ret.value = special(q, info.special)
	elseif type(q) == "string" then
		ret.value = translate(q)
	elseif type(q) == "table" then
		local tabl = {}
		for k,v in pairs(q) do
			table[k] = v
		end
		ret.value = tableParser(table,translate("separatorMid"), translate("separatorEnd"))
	end

	return ret
end

function loopTilNil(func,start)
	return func(start) and (loopTilNil(func,func(start)) or start)
end

function calcInfoBox(boxData)
	local nextLabel = 1
	local res = ""
	for i = 1,#boxData do
		current = boxData[i]
		if current and current.type then
			if current.type == "data" then
				local x = tostring(nextLabel)
				res = res .. "\n|  label" .. x .. " = " .. current.label .. "\n|   data" .. x .. " = " .. current.value
				nextLabel = nextLabel + 1
			elseif current.type == "image" then
				res = res .. "\n| " .. current.type .. " = [[File:" .. current.value .. "]]"
			else
				res = res .. "\n| " .. current.type .. " = " .. current.value
			end
		end
	end
	return "{{Infobox" .. res .. "\n}}"
end

function p.ModuleInfo(frame,bol)
	local page = ""
	local mainPage
	if(bol) then
		page = "Bat Grenades"
		langCode = "nl"
	else
		mw.log("this is not supported in debuger mate")
		mainPage = loopTilNil(
			function(start)
				return start:getParent()
			end
			,frame)
		titlearg = frame.args["Title"]
		page = frame.args["Title"] or mainPage:preprocess("{{Template:English Parent}}")
		langCode = frame.args["Language Code"] or mainPage:preprocess("{{Template:Language Code}}")
	end
	if not pcall(function() data = mw.loadData("Module:ModuleData/" .. page) end) then
		return "Couldn't find module:" .. page
	end
	if not pcall(function() order = mw.loadData("Module:ModuleData/Order") end) then
		return "An error has ocured, please contact staff\nErrorcode:ModuleData#orderNotFound"
	end
	if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/" .. langCode) end) then
		if not pcall(function() translation = mw.loadData("Module:ModuleData/Translations/en") end) then
			return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
		else
			english = translation
		end
	else
		if not pcall(function() english = mw.loadData("Module:ModuleData/Translations/en") end) then
			return "An error has ocured, please contact staff.\nErrorcode:ModuleData#translationNotFound"
		end
	end

	local mode = "latest"

	local boxData = {}

	if mode == "latest" then
		local i = 1
		while order[i] do
			boxData[i] = addData(data[1].moduleInfo[order[i].source], order[i])
			i = i + 1
		end
	end


	--[[
	local t = "<div style=\"width:280px; float:right;\"><tabber>\n"
	local i = 1
	while data[i] do
		if i > 1 then
			t = t .. "\n|-|"
			end
		t = t .. data[i].version .. "=<infobox>\n"
		local j = 1
		while order[j] do
			t = t .. addData(data[i].moduleInfo[order[j].source],order[j])
			j = j + 1
		end
		t = t .. "</infobox>\n"
		i = i + 1
	end
	]]
	local calc = calcInfoBox(boxData)
	if bol or frame.args.raw then
		return calc
	end
	return frame:preprocess(calc)
end

return p