--require "strict"

local p = {}

local lib = require 'Module:Wikidata/lib'

local function formatUnit(entityId)
	local Formatters = require 'Module:Wikidata/Formatters'
	return Formatters.formatRawValue(entityId, 'wikibase-entityid', { label = 'unitsymbol' })
end

function p.formatNumber(number)
	local integer, fractional
	local prefix = ''
	if number < 0 then
		number = -number
		prefix = '−'
	end
	number = tostring(number)
	if mw.ustring.find(number, '%.') then
		integer, fractional = mw.ustring.match(number, '^(.+)%.(.+)$')
	else
		integer = number
	end
	local length = mw.ustring.len(integer)
	local i = length % 3
	if i == 0 then
		i = 3
	end
	local formatted_num = mw.ustring.sub(integer, 1, i)
	while i < length do
		formatted_num = formatted_num .. '.' .. mw.ustring.sub(integer, i + 1, i + 3)
		i = i + 3
	end
	if fractional then
		local length = mw.ustring.len(fractional)
		local i = 3
		formatted_num = formatted_num .. ',' .. mw.ustring.sub(fractional, 1, 3)
		while i < length do
			formatted_num = formatted_num .. '' .. mw.ustring.sub(fractional, i + 1, i + 3)
			i = i + 3
		end
	end
	return prefix .. formatted_num
end

local function getUnitAndCoef(unit, options)
	local entityId = lib.getItemIdFromURI(unit)
	if not entityId or entityId == 'Q199' then
		return nil, 1
	end
	local coef
	if options.unit and entityId ~= options.unit then
		local WD = require 'Module:Wikidata'
		coef = WD.getRawValueFromLua{
			id = entityId,
			property = 'P2370',
			withunit = options.unit,
			limit = 1
		} or WD.getRawValueFromLua{
			id = entityId,
			property = 'P2442',
			withunit = options.unit,
			limit = 1
		}
		if coef then
			entityId = options.unit
		end
	end
	return entityId, coef or 1
end

function p.getRawValue(value, options)
	local _, coef = getUnitAndCoef(value.unit, options)
	return tonumber(value.amount) * coef
end

function p.formatRawValue(value, options)
	return p.formatNumber(value)
end

function p.formatValue(value, options)
	local unit, coef = getUnitAndCoef(value.unit, options)
	local amount = tonumber(value.amount) * coef
	local rawamount = amount

	local margin
	if lib.IsOptionTrue(options, 'showmargin') then
		if value.upperBound then
			margin = tonumber(value.upperBound) * coef - amount
		end
	end
	amount = p.formatNumber(amount)
	if margin then
		amount = amount .. '±' .. p.formatNumber(margin)
	end
	if tostring(options.showunit) ~= 'false' then
		if unit then
			mw.logObject(unit)
			if unit == 'Q7727' then
				return amount .. '&nbsp;' .. (tonumber(rawamount) > 1 and 'λεπτά' or 'λεπτό')
			elseif unit == 'Q11570' then
				return amount .. '&nbsp;' .. (tonumber(rawamount) > 1 and 'κιλά' or 'κιλό')
			elseif unit == 'Q11573' then
				return amount .. '&nbsp;' .. (tonumber(rawamount) > 1 and 'μέτρα' or 'μέτρο')
			elseif unit == 'Q174728' then
				return amount .. '&nbsp;' .. (tonumber(rawamount) > 1 and 'εκατοστά' or 'εκατοστό')
			end
			
			return amount .. '&nbsp;' .. formatUnit(unit) .. '[[Κατηγορία:Σελίδες που τραβάνε ετικέτες μονάδων μέτρησης με το Module:Wikidata]]'
		end
	end
	return amount
end

return p