--[[

{{#invoke:ملعب/Mr. Ibrahem/5|getdate|P569}}
{{#invoke:ملعب/Mr. Ibrahem/5|getdate|P570}}
--]]
local ModuleTime = require 'Module:wikidata2/time'
local p = {}
local o = {}
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 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'})
	if val and val ~= '' then 
	return val
	else end
end

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

function age(v, pr, add)
	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)
	if pr and pr ~='' then
		v = 'العمر ' .. v
	end
	return ' (' .. v ..' سنة )'
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
			if P569precision and P569precision  ~= '' 
				then 
					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)
								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)
											
								else
								dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
							end
						elseif P570precision == 10 or P570precision == '10'
							then
								dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname) .. age(mathyears(year, Dyear), '',  P570addon or P569addon)
						elseif P570precision == 9 or P570precision == '9' then
							dii = linkdate('P570',  Dyear .. (P570addon or '')) .. age(mathyears(year, Dyear), '',  P570addon or P569addon)
						else 
							dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
							end
				else
					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)
						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)
						elseif P569precision == '9' or P569precision == 9 then 
							val =  linkdate('P569',  year .. (P569addon or '')) .. age(mathyears(year, tonumber(os.date("%Y"))), 't', P569addon)
						else 
							val = ModuleTime.getdate( {time=Timev ,precision=tonumber(P569precision)} , {})
					end
			end
	--	end
	return val -- .. (P569addon or '')
end

function propert(precision,timev,propertyID,entity)
	 P569precision = getprop('P569','precision',entity)
	 P569time =   getprop('P569','q',entity)
	 P570precision = getprop('P570','precision',entity)
	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
	
		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