local linkPhone = require('Module:LinkPhone')
local isLatin   = require('Module:IsLatin')

local conf = {
	-- parameter name, function for getting Wikidata info, function for fallback
	{'counter', nil, function(parentArgs) return parentArgs['counter'] or parentArgs['type'] or 'listing' end},
	{'type', nil, function(parentArgs) return parentArgs['type'] or 'listing' end},
	{'name', function(frame, item) return item:getLabel() end},
	{'wikipedia', function(frame, item) return item:getLabel() end},
	{'alt', function(frame, item) return item:formatPropertyValues('P1705').value end},
	{'url', function(frame, item) return item:formatPropertyValues('P856').value end},
	{'email', function(frame, item) return item:formatPropertyValues('P968').value end},
	{'address', function(frame, item) return item:formatPropertyValues('P969').value end},
	{'lat', function(frame, item)
		if #item:getBestStatements( 'P625' ) == 0 then return nil end
		local statement = item:getBestStatements( 'P625' )[1]
		if statement.mainsnak.datavalue.value.globe ~= 'http://www.wikidata.org/entity/Q2' then return nil end
		return statement.mainsnak.datavalue.value.latitude
	end},
	{'long', function(frame, item)
		if #item:getBestStatements( 'P625' ) == 0 then return nil end
		local statement = item:getBestStatements( 'P625' )[1]
		if statement.mainsnak.datavalue.value.globe ~= 'http://www.wikidata.org/entity/Q2' then return nil end
		return statement.mainsnak.datavalue.value.longitude
	end},
	{'directions', function(frame, item)
		local statements = item:getBestStatements( 'P2795' )
		if #statements == 0 then return nil end
		local res = {}
		for _, statement in pairs(statements) do
			if statement.mainsnak.datavalue.value.language == 'en' then
				table.insert(res, statement.mainsnak.datavalue.value.text)	
			end
		end
		return table.concat(res, ', ')
	end},
	{'phone', function(frame, item) return item:formatPropertyValues('P1329').value end},
	{'tollfree'},
	{'fax', function(frame, item) return item:formatPropertyValues('P2900').value end},
	{'image', function(frame, item) 
		if #item:getBestStatements( 'P18' ) == 0 then return nil end 
		return item:getBestStatements( 'P18' )[1].mainsnak.datavalue.value
	end},
	{'hours'},
	{'price'},
	{'checkin'},
	{'checkout'},
	{'lastedit'},
	{'content', nil, function(parentArgs) return parentArgs[1] or nil end} 
}

local p = {}

function p.listing( frame) 
	local parentArgs = frame:getParent().args
	local item = nil
	
	if parentArgs['wikidata'] and string.match(parentArgs['wikidata'], '^Q%d+$') ~= nil then
		item = mw.wikibase.getEntity( parentArgs['wikidata'] )
	end
	local useWikidata = (item ~= nil and item.claims ~= nil)

	local params = {}
	for _,c in pairs(conf) do 
		if parentArgs[c[1]] ~= nil and parentArgs[c[1]] ~= '' then
			params[c[1]] = parentArgs[c[1]] -- take value from template
		else
			if useWikidata and c[2] ~= nil then
				params[c[1]] = c[2](frame, item) -- take value from Wikidata
			end
		end
		if params[c[1]] == nil and c[3] ~= nil then
			params[c[1]] = c[3]( parentArgs ) -- take value from fallback function
		end
		if params[c[1]] == '' then
			params[c[1]] = nil
		end
	end
	
	local elements = {}
	table.insert(elements, frame:expandTemplate{ title = 'marker', args = {counter = params['counter'], type = params['type'], name = params['name'], lat = params['lat'], long = params['long'], image = params['image'], url = params['url'], islisting = 'yes'} })
	if params['alt'] ~= nil then
		if isLatin.IsLatin({args = {params['alt']}}) == 'yes' then
			table.insert(elements, '&#32;(\'\'<span class="nickname listing-alt">'.. params['alt'] ..'</span>\'\')')	
		else
			table.insert(elements, '&#32;(<span class="nickname listing-alt">'.. params['alt'] ..'</span>)')
		end
	end
	if params['address'] ~= nil then
		if params['name'] ~= nil or params['url'] ~= nil then
			table.insert(elements, ',&#160;')	
		end
		table.insert(elements, '<span class="adr listing-address"><span class="street-address">'.. params['address'] ..'</span></span>')
	end
	if params['directions'] ~= nil then
		table.insert(elements, '&#32;(\'\'<span class="listing-directions">'..params['directions']..'</span>\'\')')	
	end
	local elements2 = {}
	if params['phone'] ~= nil then	
		table.insert(elements2, '<abbr title="phone">☎</abbr> <span class="tel listing-phone">'..  linkPhone.LinkPhone({args = {params['phone']}}) ..'</span>')
	end
	if params['tollfree'] ~= nil then
		table.insert(elements2, 'toll-free: <span class="tel listing-tollfree">'.. linkPhone.LinkPhone({args = {params['tollfree'], tollfree=true}}) .. '</span>')
	end
	if params['fax'] ~= nil then
		table.insert(elements2, '<span class="tel"><span class="type">fax</span>: <span class="value listing-fax">'.. linkPhone.LinkPhone({args = {params['fax'], fax=true}}) .. '</span></span>')
	end
	if params['mail'] ~= nil then
		table.insert(elements2, 'e-mail: <span class="email listing-email">[mailto:'..params['email']..' '..params['email']..']</span>')
	end
	if #elements2 > 0 then
		table.insert(elements, ',&#160;' .. table.concat(elements2, ',&#160;' ))
	end
	if params['hours'] ~= nil then
		table.insert(elements, '&#32;<span class="note listing-hours">'.. params['hours']..'</span>.')
	end
	if params['checkin'] ~= nil then
		table.insert(elements, '&#32;<span class="note">Check-in: <span class="listing-checkin">' .. params['checkin'] .. '</span>')
		if params['checkout'] ~= nil then
			table.insert(elements, ', check-out: <span class="listing-checkout">'.. params['checkout'] ..'</span>')	
		end
		table.insert(elements, '</span>')
	else
		if params['checkout'] ~= nil then
			table.insert(elements, '&#32;<span class="note">Check-out: <span class="listing-checkout">'.. params['checkout'].. '</span></span>.')
		end
	end
	if params['content'] ~= nil then
		table.insert(elements, '  <span class="note listing-content">'..params['content']..'</span>')
	end
	if params['price'] ~= nil then
		table.insert(elements, '&#32;<span class="note listing-price">'.. params['price']..'</span>.')
	end
	-- TODO: Metadata, hidden unicode, email tracking
	table.insert(elements, '<span class="listing-metadata">')
	if params['lastedit'] ~= nil then table.insert(elements, '&nbsp;(' ) end
	table.insert(elements, '<span class="listing-metadata-items">')
	if params['lastedit'] ~= nil then
		table.insert(elements, '<span class="listing-lastedit">updated '..  mw.getContentLanguage():formatDate(' M Y', params['lastedit']) .. '</span>')
	else
		table.insert(elements, '&nbsp;');
	end
	if useWikidata then
		table.insert(elements, '<span class="vcard-edit-button noprint">&#32;[[File:Wikidata-logo.svg|16px|class=listing-sister|link=:d:'.. item.id ..'|'.. item.id ..' on Wikidata]]</span>&nbsp;')	
	end
	if params['wikipedia'] ~= nil then
	    table.insert(elements, '<span class="vcard-edit-button noprint">&#32;&#32;[[File:Wikipedia-icon.png|15px|class=listing-sister|link=:w:'.. params['wikipedia'] ..'|'.. params['name']..' on Wikipedia]]</span>&nbsp;')
	end
	table.insert(elements, '</span>');
	if params['lastedit'] ~= nil then table.insert(elements, ')' ) end
	table.insert(elements, '</span>')
	
	-- from [[Module:HiddenUnicode]]
	for k,txt in pairs(params) do
		-- Note the quote ("") below is not empty and actually contains a hidden unicode character.
		if mw.ustring.match(txt,"‎") ~= nil then
	        table.insert(elements, '[[Category:Listing with hidden unicode character]]')
	        break
	    end
	end
	
	-- from [[Module:EmailTracking]]
	if params['email'] ~= nil and  mw.title.getCurrentTitle().namespace == 0 and mw.ustring.match(params['email'],'@.*@') ~= nil then
	   table.insert(elements, '[[Category:Listing with multiple email addresses]]<span class="phoneinfo" style="display:none;">MULTIPLE-EMAIL</span>')
	end
	
	return '<span class="vcard">' .. table.concat(elements) .. '</span>'
end

return p