--[[

{{#invoke:ملعب/Mr. Ibrahem/5|getdate|P569}}
{{#invoke:ملعب/Mr. Ibrahem/5|getdate|P570}}
--]]
local ModuleTime = require 'Module:wikidata2/time'
local tempname = 'تاريخ الوفاة والعمر'
local p = {}
local o3 = '[[category:صفحات بها تواريخ بحاجة لمراجعة]]'
function getdatepart(time , option)
	if time and time ~='' then
		if option =='y' then
			return tonumber(string.sub(time, 2, 5))
	    elseif option =='m' then
			return tonumber(string.sub(time, 7, 8))
	    elseif option =='d' then
			return tonumber(string.sub(time, 10, 11))
		end
	end
end

function category(property, y, m, d)
	if mw.title.getCurrentTitle().namespace ~= 0 then return '' end
	if property == 'P569' 
		then prf = 'مواليد '
			if d and m then cat2 = prf .. d.. ' ' .. m end
	elseif property == 'P570' 
		then prf = 'وفيات '
	end	
	if y then cat = ' [[تصنيف:'.. prf .. y ..']]' end
	if cat2 then cate =  ' [[تصنيف:'.. cat2 ..']]' end
	return (cat or '') ..  (cate or '')
end

function getprop(propertyID,modifytime,entity)
	local val = formatStatements( {property =propertyID ,entityId=entity , modifytime =modifytime, noref='true' , firstvalue='true' })
	mw.log( val )
	return val
end

function mathyears(year, Ryear, month, Rmonth)
--	Ryear = Date( "%Y" )
--	Rmonth =Date( "%m" )
--	Rday = os.date( "%e" )
	--	if string.sub(Ryear, 1, 1) == '-' then Ryear = '+' .. string.sub(Ryear, 2) end
	
	if month and month ~=''
	then
		if Rmonth < month
		then 
			val = Ryear - year - 1
		else 
			if Rmonth > month
				then val = Ryear - year
				else val = Ryear - year - 1 ..'&ndash;' .. Ryear - year
			end
		end
	else
		val = Ryear - year - 1 ..'&ndash;' .. Ryear - year
		--mw.log( "val.." .. val )
	end
		
	if (Ryear - year) < 0 
		then val = val .. o3 
	elseif (Ryear - year) > 150 
		then val = val .. o3 
	elseif (year - Ryear) > 150 
		then val = val .. o3 
	end
	
	return val
end

local agecat = { "111","112","116","95","96","97","98","99","100","101","102","103","104","105","106","107","108","110","75","91" }

function age(v, pr, add, property)
	local v_old = v
	if add and add ~='' then return '' else end
	--	if string.sub(Ryear, 1, 1) == '-' then Ryear = '+' .. string.sub(Ryear, 2) end
	--	val =  mathyears(year, Ryear, month, Rmonth)
	v2 = tostring(v)
	if pr and pr ~='' then
		v = 'العمر ' .. v
	end
	ii =  ' (' .. v ..' سنة)'
	if property and property == "P570" then 
		Age_cat = false
		for k, l in pairs(agecat) do
			if v2 == l then 
				Age_cat = true
			end
		end
		if Age_cat then
			ii = ii .. '[[category:وفيات بعمر ' .. v .. ']]'
		end
	end
	local start = mw.ustring.find( v, o3, 1, true )
	if start == 0 or start == nil then
		return ii
	else
		return o3
	end
end

function mathfulldate(yb, mb, db, Yd, Md, Dd)
	yd = Yd or tonumber(os.date( "%Y" ))
	md = Md or tonumber(os.date( "%m" ))
	dd = Dd or tonumber(os.date( "%e" ))
	function ma(f,s)
		if f < s 
			then return '1'
			elseif f == s then return '1'
			else return '0'
		end
	end
	function foo(f,s)
		if f < s 
			then return '1'
			else return '0'
		end
	end	
	if not foo(db,db) == '0' or not ma(md,mb) == '0'
		then vv = '1'
		else vv = '0'
	end
	val = (yd)-(yb)-(vv)
	if (yd - yb) > 150 then val = val .. o3 elseif (yb - yd) > 150 then val = val .. o3 end
	return val
end

function linkdate(property, y, m, d)
			if y then year = '[['..y ..']]' end
			if m 
				then 
					if d
					then md = '[['.. d ..' ' .. m ..']] '
					else md = '[['.. m ..']] '
					end
				else 
				year = 'سنة ' .. year
			end	
		return (md or '') .. year .. category(property, y, m, d)
	end	
	
function getP570(P570precision,Timev,entity,P569precision,P569time)
--	local P569precision = P569pre or getprop('P569','precision',entity)
--	local P569time =   P569tim or getprop('P569','q',entity)
	if string.sub(Timev, 1, 1) == '-' then
		timev = '+' .. string.sub(Timev, 2)
		P570addon =  ' ق م'
	else 
		timev= Timev
	end
	if P569time and P569time ~= '' then
		if string.sub(P569time, 1, 1) == '-' then
			P569time = '+' .. string.sub(P569time, 2)
			P569addon =  ' ق م'
		end 
	end
	local Dyear = getdatepart(timev,'y')
	local Dmonth =getdatepart(timev,'m')
	local Dmonthname =mw.getContentLanguage():formatDate( 'F', timev )
	local Dday =getdatepart(timev,'d')
	local year = getdatepart(P569time ,'y')
	local month =getdatepart(P569time ,'m')
	local monthname =mw.getContentLanguage():formatDate( 'F', P569time )
	local day =getdatepart(P569time ,'d')
	--	if P570precision and P570precision~= '' 
	--		then
	--mw.log( 'P569precision: '.. P569precision )
	--mw.log( 'P570precision: '.. P570precision )
	if P569precision and P569precision  ~= ''  then 
		-- Death date is full
		if P570precision == 11 or P570precision == '11' then 
			if P569precision == 11 or P569precision == '11' then 
				--doo = mathfulldate(year, month, day, Dyear, Dmonth, Dday)
				--dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname, Dday) .. age(doo, '', P570addon or P569addon , "P570")
				 dii  = mw.getCurrentFrame():expandTemplate{ title = tempname , args = { Dyear, Dmonth, Dday , year, month, day} } 
			elseif P569precision == 10 or P569precision == '10' or P569precision == 9 or P569precision == '9' then  
				dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname, Dday) .. age(mathyears(year, Dyear), '',  P570addon or P569addon, "P570")
			else
				dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
			end
			
		-- Death date is year
		elseif P570precision == 10 or P570precision == '10' then
			dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname) .. age(mathyears(year, Dyear), '',  P570addon or P569addon, "P570")
			
		elseif P570precision == 9 or P570precision == '9' then
			--#######################
			if P569precision == 10 or P569precision == '10' or P569precision == 9 or P569precision == '9' then  
				dii = linkdate('P570',  Dyear .. (P570addon or '')) .. age(mathyears(year, Dyear), '',  P570addon or P569addon, "P570")
			else
				dii = linkdate('P570',  Dyear .. (P570addon or ''))
			end
			--#######################
		else
			dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
		end
	else
		-- no P569 date
		if P570precision == 11 or P570precision == '11' then 
			dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname, Dday)
		elseif P570precision  == 10 or P570precision  == '10' then 
				dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname)
		elseif P570precision == 9 or P570precision == '9' then 
			dii  =linkdate('P570',  Dyear .. (P570addon or ''))
		else 
			dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
		end
	end
	--		else
	--	end
	return dii -- .. (P570addon or '')
end

function getP569(P569precision,Timev,entity,P570precision)
--	local P570precision = P570pre or getprop('P570','precision',entity)   
--	local P570time = getprop('P570','q',entity)
	local P569addon = ''
	if string.sub(Timev, 1, 1) == '-' then
		timev = '+' .. string.sub(Timev, 2)
		P569addon =  ' ق م'
	else 
		timev= Timev
	end
	local year =  getdatepart(timev,'y')
	local month = getdatepart(timev,'m')
	local monthname = mw.getContentLanguage():formatDate( 'F', timev )
	local day = getdatepart(timev,'d')

	--	if P569precision and P569precision ~= '' 
	--	then
	if P570precision and P570precision ~= ''
		then 
			if P569precision == 11 or  P569precision == '11'
				then 
					val = linkdate('P569',  year .. (P569addon or ''), monthname, day)
				elseif P569precision == 10 or  P569precision == '10'
					then 
						val = linkdate('P569',  year .. (P569addon or ''), monthname)
				elseif P569precision == 9 or P569precision == '9' then 
					val = linkdate('P569',  year .. (P569addon or ''))
				else 
					val = ModuleTime.getdate( {time=Timev ,precision=tonumber(P569precision)} , {})
			end
	else 
		if P569precision == 11 or  P569precision == '11' 
			then 
				doo = mathfulldate(year, month, day)
				val = linkdate('P569',  year .. (P569addon or ''), monthname, day) .. age(doo, '', P569addon, "P569")
			elseif P569precision == 10 or P569precision == '10' 
				then  
					val =  linkdate('P569',  year .. (P569addon or ''), monthname)
					.. age(mathyears(year, tonumber(os.date("%Y")), month, tonumber(os.date("%m"))), '', P569addon, "P569")
			elseif P569precision == '9' or P569precision == 9 then 
				val =  linkdate('P569',  year .. (P569addon or '')) .. age(mathyears(year, tonumber(os.date("%Y"))), 't', P569addon, "P569")
			else 
				val = ModuleTime.getdate( {time=Timev ,precision=tonumber(P569precision)} , {})
		end
	end
	return val -- .. (P569addon or '')
end

function propert(precision,timev,propertyID,entity)
	local P569precision = getprop('P569','precision',entity)
	local P569time =   getprop('P569','q',entity)
	--local P570precision = getprop('P570',c,entity)
	local P570precision = formatStatements( {property ='P570' ,entityId=entity , modifytime ='P570', noref='true' , firstvalue='true' , novalue = 'vv',somevalue = 'vv'})
	local P569addon = ''
--	if string.sub(P569time, 1, 1) == '-' then
--		P569time = '+' .. string.sub(P569time, 2)
--		P569addon =  ' ق م'
--	end
	if propertyID == 'P569' then
		val = getP569(precision, timev, entity, P570precision)
	elseif propertyID == 'P570' then
		val =  getP570(precision,timev,entity,P569precision,P569time)
	end
	return val-- .. (addon or '')
end

function p.getdate( datavalue ,datatype , options)
	local  propertyID = options.property
	local entity = mw.wikibase.getEntityObject(options.entityId)

	if datavalue.type == 'time'  
	then
		precision = tonumber(datavalue.value.precision)
		timev = datavalue.value.time
	     if precision == 9 or precision == 10 then
	    	timev = string.gsub(timev ,'-00T','-01T')
    	end

		tt = propert(precision,timev,propertyID,entity.id)
	return tt
	end
end

function p.test(frame)
	
	local propertyID = frame.args.property
	if propertyID and propertyID ~='' 
		then
			if propertyID == 'P569' 
				then
					val= getP569(frame.args.P569pre,frame.args.P569time,entity,frame.args.P570pre)
				elseif propertyID == 'P570' 
					then
						val= getP570(frame.args.P570pre,frame.args.P570time,entity,frame.args.P569pre,frame.args.P569time)
			end

		return val
	end
end


return p