local p = {}

function p.progress (frame)
	local args = frame.args
	local index = args[1]
	local level = 0
	local level_bar = 0
	
	if not index or index == "" then
		return "Li no do live n’ est nén dné."	
	end
	
	local indexpage = mw.title.new(index, "Index")
	local indexlevel = 0
	local content = indexpage:getContent()
	if content ~= nil and content ~="" then
		indexlevel = mw.ustring.match(content, "Progress=([A-Z])")
	end
	
	if indexlevel =="T" then
		level = 200
		level_bar = 100
	elseif indexlevel == "V" then
		level = 100
		level_bar = 100
	elseif indexlevel =="C"  and mw.ustring.find(index, "%.djvu$") then
		local l = mw.title.new(index, "Media")
		if l.exists then
			local npages = #l.file.pages
			local cpages = npages
			local slevel = 4
			if npages then
				local i=1
				local page = nil
				while (i<=npages) do
					page = mw.title.new(index.."/"..i, "Page")
					local content = page:getContent()
					if content ~= nil and content ~="" then
						local pagelevel = tonumber(mw.ustring.match(content, "[0-4]"))
						if pagelevel < slevel then
							slevel = pagelevel
						end
						if pagelevel >= 3 then
							level_bar = level_bar+1
						end
					else
						cpages = cpages - 1
					end
					i = i+1
				end
				level = math.floor(cpages*100/npages)
				level_bar = math.floor(level_bar*100/npages)
				if level == 100 and slevel == 1 then
					level = 99
				elseif level == 100 and slevel == 4 then
					level = 200
				end
			end
		end
	end
	
	if args[2] ~= nil and args[2] == "icone" then
		local levelimg = {
			[0] = {"00%.svg", "Nén co ataké"},
			[25] = {"25%.svg", "Nén co ataké"},
			[50] = {"50%.svg", "Nén co ataké"},
			[75] = {"75%.svg", "Nén co ataké"},
			[100] = {"100%.svg", "Nén co ataké"},
			[200] = {"Nuvola apps mozilla.png", "Coridjî et rverifyî"}
		}
	
		if level > 0 then
			if level < 50 then
				level = 25
			elseif level < 75 then
				level = 50
			elseif level < 100 then
				level = 75
			end
		end
		level = "[[Image:"..levelimg[level][1].."|10px|alt="..levelimg[level][2].."|link=]]"
	elseif args[2] ~= nil and args[2] == "bar" then
		div_p = mw.html.create("div")
		div_p:css({
			display="inline-block",
			position="relative",
			width="250px",
			height="14px",
			border="solid 1px #ddd",
			background="#eee",
			["vertical-align"]="middle"
		})
		div_t = mw.html.create("div")
		div_t:wikitext(level_bar .. "%")
		div_t:css({
			position="absolute",
			top=0,
			bottom=0,
			left=0,
			right=0,
			border="solid 1px white",
			["text-align"]="center",
			["font-size"]="10px",
			["line-height"]="12px",
			["z-index"]=3
		})
		div_y = mw.html.create("div")
		div_y:css({
			position="absolute",
			top=0,
			bottom=0,
			background="#ffe867",
			width= (level_bar.."%"),
			["z-index"]=2
		})
		div_p:node(div_y)
		div_p:node(div_t)
		level = div_p
	end
	
	return level
end

return p