local p = {}
local lang = "wa"
local imgM = "Gray - replace this image male.svg"
local imgF = "Gray - replace this image female.svg"
local NS = mw.title.getCurrentTitle().namespace
local CatNS = mw.site.namespaces.Category.name

function p.main(frame)
	local id = nil
	local html = mw.html.create()
	if frame.args.wd ~= nil and frame.args.wd ~= "" then 
		id = frame.args.wd
	end
	
	if NS ~= 100 then
		local msg = frame:preprocess(
			   'Cisse pådje ci n’ est nén dvins l’ espåce di lomaedje « Oteur ». '
			.. "A rlomer  '''[[Oteur:{{PAGENAME}}]]'''." )
		html:wikitext(frame:expandTemplate{ 
			title = 'Bindea', 
			args = { sore = 'asteme', msg }
		})
		return html
	end
	
	-- data from Wikidata
	local item = mw.wikibase.getEntity(id)
	
	-- si l' pådje n' est nén raloyeye a Wikidata
	if item == nil then
		local msg = frame:preprocess(
			   'Cisse pådje ci n’ est nén co raloyeye a Wikidata. '
			.. 'Cwand çoula serè fwait, rivnoz vaici et ' 
			.. '[{{SERVER}}{{localurl:{{FULLPAGENAME}}|action=purge}} purdjîz l’ pådje].' )
		html:wikitext(frame:expandTemplate{ 
			title = 'Bindea', 
			args = { sore = 'asteme', msg }
		})
		html:wikitext('[[Category:Oteurs nén raloyîs a Wikidata]]')
		return html
	end

	html:wikitext('[['..CatNS..':Oteurs]]')
	local I = mw.text.split( mw.title.getCurrentTitle().text, ' ', true )
    I = I[#I]
	I = mw.ustring.sub(I , 1, 1)
	I = mw.ustring.upper(I)
	I = mw.ustring.gsub( I, "[ÉÈ]", "E")
	if frame.args.index ~= nil and frame.args.index ~= "" then 
		Iauto = I
		I = mw.ustring.upper(frame.args.index)
		if Iauto == I then
			html:wikitext("[[Category:Oteurs k' on pout rsaetchî l' paramete indek]]")
		end
	end
	div_index = mw.html.create( 'div' )
	div_index:wikitext('[[:Category:Oteurs-'..I..'|Oteurs-'..I..']]')
	html:wikitext('[['..CatNS..':Oteurs-'..I..']]')
	if id ~= nil then
		div_index:wikitext('[[Category:Oteurs avou on paramete Wikidata]]')
	end
	
	div_author = mw.html.create( 'div' )
	div_author:css({
		["margin-bottom"] = "1em",
		["border-top"] = "5px solid #355391",
		["background"] = "#DFE5E9",
		["overflow"] = "auto",
		["padding"] = "10px 20px",
		["box-shadow"] = "0 0 10px #ddd",
		["min-height"] = "150px"
	})

	div_author_text = mw.html.create( 'div' )
	div_author_text:css({
		["margin-left"] = "200px"
	})
	
	div_iw = mw.html.create( 'div' )
	div_iw:css({
		["float"] = "right"
	})

	-- index
	if div_index ~= nil then div_iw:node( div_index ) end
	
	-- Wikidata
	if item.id~= nil then
		div_iw:wikitext( '<div>[[:d:'..item.id..'|Wikidata]]</div>' )
	end
	-- Wikipedia
	local iwd = item:getSitelink( lang..'wiki' )
	if iwd ~= nil then
		div_iw:wikitext( '<div>[[:w:'..lang..':'..iwd..'|Wikipedia]]</div>' )
	end
	-- Wiccionaire
	local iwkt = item:getSitelink( 'wawiktionary' )
	if iwkt ~= nil then
		div_iw:wikitext( '<div>[[:wikt:wa:'..iwkt..'|Wiccionaire]]</div>' )
	end
	-- Commons
	local iwc = item:formatPropertyValues('P373').value
	if iwc ~= nil and iwc ~= '' then
		div_iw:wikitext( '<div>[[:c:Category:'..iwc..'|Commons]]</div>' )
	end
	div_author:node( div_iw )
	
	-- Image (ome/feme)
	local genderWD = item:getBestStatements('P21')
	local img = imgM
	if  genderWD[1] ~= nil 
	and genderWD[1].mainsnak.datavalue ~= nil 
	and genderWD[1].mainsnak.datavalue.value.id == 'Q6581072' then
		img = imgF
		html:wikitext('[['..CatNS..':Otoresses]]')
	end
	
	local imgWD = item:getBestStatements('P18')
	if imgWD[1] ~= nil and imgWD[1].mainsnak.datavalue ~= nil then
		img = imgWD[1].mainsnak.datavalue.value
	else
		html:wikitext("[[Category:Oteurs sins imådje]]")
	end
	div_author:wikitext( '[[File:'..img..'|left|128px]]' )
 
	local fullnameWD = item:getLabel(lang)
	local fullnameWD_fr = item:getLabel('fr')
	if fullnameWD == nil then fullnameWD = "?" end
	div_author_name = mw.html.create( 'div' )
	div_author_fname = mw.html.create( 'span' )
	div_author_fname:css({
		["font-weight"] = "bold",
		["font-family"] = "sherif",
		["font-size"] = "200%"
	})
	div_author_fname:wikitext(fullnameWD)
	div_author_name:node( div_author_fname )
	
	local natWD = item:getBestStatements('P31')
	local i = 1 ; local noteur = 1
	while( natWD[i] ~= nil and natWD[i].mainsnak.datavalue ~= nil ) do
		if natWD[i].mainsnak.datavalue.value.id == 'Q10648343' then
			noteur = 2
		end
		i = i + 1 
	end
	
	if noteur == 1 then
		local akaWD = item:getBestStatements('P742')
		local i = 1
		local akas = {}
		local altname = ""
		while( akaWD[i] ~= nil and akaWD[i].mainsnak.datavalue ~= nil ) do
			local aka = akaWD[i].mainsnak.datavalue.value
			local title = mw.title.makeTitle('Oteur', aka)
			if not title.exists then
			   	html:wikitext('[['..CatNS..':Oteurs avou on no d’ pene a redjibler]]')
			end
			table.insert(akas, aka)
			if i == 10 then break end
			i = i + 1 
		end
		if fullnameWD_fr ~= nil or #akas > 0 then
			if fullnameWD_fr ~= nil and fullnameWD_fr ~= "" and fullnameWD_fr ~= fullnameWD  then
				altname = '<abbr title="no e francès">n.f.</abbr> ' .. fullnameWD_fr
			end
			if #akas > 0 then
				if altname ~= "" then altname = altname..' ; ' end
				altname = altname .. '<abbr title="no d’ pene">n.p.</abbr> ' .. table.concat(akas, ', ')
			end
			if altname ~= "" then
				div_author_name:wikitext(' ('..altname..')')
			end
		end
		div_author_text:node( div_author_name )
		
		local birthdate = '';
		local deathdate = '';
		local birthWD = item:getBestStatements('P569')
		local deathWD = item:getBestStatements('P570')
		if birthWD == nil or birthWD[1] == nil or birthWD[1].mainsnak.datavalue == nil then birthdate = "?" else 
			if birthWD[1].mainsnak.datavalue.value.precision == 7 then
				birthdate = (tonumber(string.sub(birthWD[1].mainsnak.datavalue.value.time, 2, 3))+1).."<sup>inme</sup> sièke"
			elseif birthWD[1].mainsnak.datavalue.value.precision == 8 then
				birthdate = string.sub(birthWD[1].mainsnak.datavalue.value.time, 2, 4).."x"
			else
				birthdate = tonumber(string.sub(birthWD[1].mainsnak.datavalue.value.time, 2, 5))
			end
			if string.sub(birthWD[1].mainsnak.datavalue.value.time, 1, 1) =="-" then
				birthdate = birthdate .. ' dvant Dj.C.'
			end
		end
		if deathWD == nil or deathWD[1] == nil or deathWD[1].mainsnak.datavalue == nil then 
			deathdate = "?" 
			html:wikitext('[['..CatNS..':Oteurs sins date di moirt]]')
		else 
			if deathWD[1].mainsnak.datavalue.value.precision == 7 then
				deathdate = (tonumber(string.sub(deathWD[1].mainsnak.datavalue.value.time, 2, 3))+1).."<sup>inme</sup> sièke"
			elseif deathWD[1].mainsnak.datavalue.value.precision == 8 then
				deathdate = string.sub(deathWD[1].mainsnak.datavalue.value.time, 2, 4).."x"
			else
				deathdate = tonumber(string.sub(deathWD[1].mainsnak.datavalue.value.time, 2, 5))
			end
			if string.sub(deathWD[1].mainsnak.datavalue.value.time, 1, 1) =="-" then
				deathdate = deathdate .. ' dvant Dj.C.'
			end
		end
		div_author_dates = mw.html.create( 'div' )
		div_author_dates:wikitext(birthdate.." — "..deathdate)
		div_author_text:node( div_author_dates )
	else
		div_author_text:node( div_author_name )
	end
	
	if noteur > 1 then
		local oteursWD = item:getBestStatements('P527')
		local i =1
		local oteurs = {}
		while( oteursWD[i] ~= nil and oteursWD[i].mainsnak.datavalue ~= nil ) do
			local oteur = mw.wikibase.getLabel(oteursWD[i].mainsnak.datavalue.value.id)
			table.insert(oteurs, "[[:Author:"..oteur.."|"..oteur.."]]")
			i = i + 1 
		end
		div_author_oteurs = mw.html.create( 'div' )
		div_author_oteurs:wikitext(table.concat(oteurs, ", "))
		div_author_text:node( div_author_oteurs )
	end
	
	local descWD = item:getDescription( lang )
	if descWD == nil then descWD = "''i gn a co nou discrijhaedje so Wikidata''" end
	div_author_des = mw.html.create( 'div' )
	div_author_des:wikitext(descWD)
	div_author_text:node( div_author_des )
	
	div_author:node( div_author_text )
	html:node( div_author )
	return  html
end

function p.tt(id)
	local item = mw.wikibase.getEntity(id)
	local deathWD = item:getBestStatements('P570')
	return deathWD
end

return p