Modul:WikidataAlder

local Date = require('Module:Date')._Date
local p = {}

function okProperty(prop)
	-- Sjekk at prop finnes
	if prop
		and prop['mainsnak']
		and prop['mainsnak']['datavalue'] 
		and prop['mainsnak']['datavalue']['value']
		and prop['mainsnak']['datavalue']['value']['time']
		and prop['mainsnak']['datavalue']['value']['precision']
	then 
		return true
	end
	return false
end

function checkForUnknownValue(prop)
	-- Sjekk om vi har "ukjent verdi"
	if prop
		and prop['mainsnak']
		and prop['mainsnak']['snaktype'] == 'somevalue' 
	then 
		return true
	end
	return false
end

function p.formatTime(timestamp)
    local year = tonumber(string.sub(timestamp, 2, 5))
    local month = tonumber(string.sub(timestamp, 7, 8))
    local day = tonumber(string.sub(timestamp, 10, 11))
        if string.sub(timestamp, 1, 1) == '-' then
        year = 0 - year
    end
    
    if year ~= nil and month ~= nil and day ~= nil then
    	return Date(tonumber(year), tonumber(month), tonumber(day))
    else 
    	return nil
    end
    
end

function findBestProperty(qv, pv)
    local entity = mw.wikibase.getEntity(qv)
    if not entity then
        return
    end
    if not entity['claims'] or not entity['claims'][pv] then
        return
    end
    local prop = entity['claims'][pv]
    local i = 0
    local oki = 0
    while i < #prop do
        i = i + 1
        local rank = prop[i]['rank']
        if rank == 'preferred' then
            return prop[i]
        end
        if rank == 'normal' then
            if oki == 0 then
                oki = i
            end
        end
    end
    if oki > 0 then
        return prop[oki]
    end
end

function getDate(frame, action)

	local propertyID

	if action == 'born' then
		propertyID = 'P569'
	elseif action == 'died' then
		propertyID = 'P570'
	else
		return '', nil
	end

	local qid = frame.args[1]
	if qid and qid == "" then
		qid = nil
	end

		
	local prop = findBestProperty(qid, propertyID)
	
	if not prop then
		return '';
	end

	if checkForUnknownValue(prop) then
		return 'unknown value', nil	
	end
	
	if okProperty(prop) then
		local timestamp = prop['mainsnak']['datavalue']['value']['time']
		local precision = prop['mainsnak']['datavalue']['value']['precision']
		return p.formatTime(timestamp), precision
	else
		return '', nil
	end

end

function p.alderInfoboks(frame)
	
	local timestampBorn, precisionBorn = getDate(frame, 'born')
	local timestampDied, precisionDied = getDate(frame, 'died')

	if (timestampBorn == 'unknown value' or timestampDied == 'unknown value') then
		return ''
	end

	if (timestampBorn == nil and timestampDied == nil) or precisionBorn == nil then
		return ''	
	end

	if timestampDied == '' and precisionBorn >= tonumber(11) then
		local localtime = mw.language.getContentLanguage():formatDate( 'Y-m-d', nil, true )
		local diff = Date(localtime) - timestampBorn
		-- capture error
		if diff == nil then
			return ''
		end
		return mw.text.nowiki('(' .. diff:age('y') .. ' år)')
	end
	
	if timestampDied ~= nil and precisionDied ~= nil and (precisionBorn >= tonumber(11) and precisionDied >= tonumber(11)) then
		local diff = timestampBorn - timestampDied
		return mw.text.nowiki('(' .. diff:age('y') .. ' år)')
	end

	return ''
	
end


return p