local export = {}
local m_links = require("Module:links")
local lang = require("Module:languages").getByCode("zh")
local variety_data = require("Module:zh/data/dial")

local dots = {
	"e72c27", "2589e7", "96e725", "8328e7", "ece65a",
	"28dde7", "e727dc", "e78322", "20e680", "3b49d1", 
	"9b5b5a", "59869c", "809c59", "79599c", "9d9a5a", 
	"52a1a3", "9c5995", "9c7a59", "5a9b7a", "59619c", 
	"da716c", "6ca5da", "8fda6c", "a16dda", "e1d08a", 
	"6dd3da", "da6dd4", "dca875", "84d9ad", "726dda", 
	"783017", "164f73", "4d7313", "411777", "766312", 
	"167364", "7e187d", "744116", "16742b", "161f7a", 
	"e1a4a3", "a2c3e1", "bedc93", "bb9ade", "dcd993", 
	"93d8dc", "dc93d8", "dcb793", "93dcb6", "9395dc", 
	"b74945", "538bbd", "87b644", "844fbd", "cdc741", 
	"4db6bc", "b644af", "b67c44", "44b67a", "4448b6", 
	"a06d6d", "6f88a0", "8ba16d", "876ea0", "b0ae84", 
	"6d9ea1", "a16d9e", "a1866d", "6da186", "6d6da1", 
	"f18383", "83bbf1", "c3f183", "bb83f1", "f1eb82", 
	"83ebf1", "f282e9", "f1b883", "83f1b8", "8286f2", 
}

local grey = "ccccbf"

local elements = {}

elements.map_header = function(text)
	return tostring(
		mw.html.create( "h2" )
			:wikitext( text )
		:done()
	)
end

elements.map = function(points, legend)
	return tostring(
		mw.html.create( "div" )
			:addClass( "thumb" )
			:addClass( "zh-dial-map__container" )

			:tag( "div" )
				:addClass( "thumbinner" )
				:addClass( "zh-dial-map__frame" )
				-- these styles can't be moved to the .css file because .thumbinner has its own definitions
				:css( "overflow", "auto" )
				:css( "font-size", "1px" ) -- related to the positioning of the dots

				:tag( "div" )
					:addClass( "zh-dial-map__map" )
					:wikitext( '[[File:Chinese dialectal variation location map.svg|1200px|link=]]' )
					:wikitext( points )
				:done()
			:done()

			:tag( "div" )
				:addClass( "zh-dial-map__legend" )
				:wikitext( legend )
			:done()
		:done()
	)
end

elements.dot = function(d, loc_name, loc_info, top, left, colour)
	local term = mw.ustring.gsub(d.term, "[%[%]]", "")
	
	return tostring(
		mw.html.create( "span" )
			:attr( "data-word", term )
			:attr( "data-location-en", loc_name )
			:attr( "data-location-zh", loc_info.chinese )
			:attr( "data-group", loc_info.group )
			:addClass( "zh-dial-map__dot" )
			:addClass( (colour == grey and "zh-dial-map__dot-other" or nil) )

			:css( "top", top .. "em" ) -- The size of 1 em is tied to the font-size of .zh-dial-map__frame
			:css( "left", left .. "em" )
			:css( "background-color", "#" .. colour )
			:attr( "title", loc_name .. " (" .. loc_info.chinese .. ") " .. loc_info.group .. ": " .. term )

			-- without text in the <span> it seems like the wikitext render discards the whole <span>???
			-- and makes a link with no text at all??????
			:wikitext( "&nbsp;" )
		:done()
	)
end

elements.legend = function(d, colour, appendedText)
	return tostring(
		mw.html.create( "div" )
			:attr( "data-word", (d and d.term or "other") )
			:addClass( "zh-dial-map__legend-row" )
			:addClass( (colour == grey and "zh-dial-map__legend-row-other" or nil) )

			:tag( "span" )
				:addClass( "zh-dial-map__legend-row-dot" )
				:css( "background-color", "#" .. colour )

				:wikitext( "&nbsp;" ) -- please let me make empty spans
			:done()

			:wikitext( appendedText )
		:done()
	)
end

function export.make_map(frame)
	local syn_data = require("Module:zh/data/dial-syn/" .. frame.args[1]).list
	local prelim_data, data, points, legend = {}, {}, {}, {}
	local loc, cur = {}, {}
	for i = 1,#variety_data,1 do
		loc = variety_data[i]
		cur = syn_data[loc.key] or {""}
		if cur[1] ~= "" and loc.lat then
			for _, term in ipairs(cur) do
				term = mw.text.split(term, ":")
				if term[2] ~= "mT" and term[2] ~= "GT" then
					term = term[1]
					if prelim_data[term] then
						prelim_data[term].count = prelim_data[term].count + 1
						table.insert(prelim_data[term].locations, loc)
					else
						prelim_data[term] = { count = 1, locations = { loc } }
					end
				end
			end
		end
	end
	for term, term_data in pairs(prelim_data) do
		table.insert(data, { term = term, count = term_data.count, locations = term_data.locations })
	end
	table.sort(data, function(first, second) return first.count > second.count end)

	local prev_count, num = data[1].count, 1
	local greyed, greyed_count = false, 0
	local num_syn = table.maxn(data)
	--when need to have greyed points, grey out all synonyms with only 1 dialect point
	local have_greyed = num_syn > 80 and data[num_syn].count == 1
	for _, d in ipairs(data) do
		greyed = greyed or (have_greyed and d.count == 1) or (num > 70 and d.count ~= prev_count) or num > 80
		local colour = greyed and grey or dots[num]
		for _, loc in ipairs(d.locations) do
			local top_offset, left_offset = 0, 0
			if table.getn(syn_data[loc.key]) > 1 then
				top_offset = math.random(-300, 300) / 100
				left_offset = math.random(-300, 300) / 100
			end
			local top = ((55 - loc.lat) * 89520 / 5593) + top_offset --((55 - loc.lat) * 1200 * 746 / 799 / 70) + top_offset
			local left = ((loc.long - 70) * 16) + left_offset --((loc.long - 70) * 1200 / 75) + left_offset
			local loc_name = mw.ustring.gsub(loc.english or loc.key, "%((.*)%)$", "- %1")
			table.insert(points, '[[' .. d.term .. '|' .. elements.dot(d, loc_name, loc, top, left, colour) .. ']]')
		end

		if greyed then
			greyed_count = greyed_count + d.count
		else
			local link = m_links.full_link({
				lang = lang,
				term = mw.ustring.gsub(d.term, "(.+)_[1-9]", "%1"),
				alt = mw.ustring.gsub(d.term, "(.+)_([1-9])", "%1<sub>%2</sub>"),
				tr = "-",
			})
			table.insert(legend, elements.legend(d, colour, link .. "(" .. d.count .. ")"))
		end
		
		prev_count = d.count
		num = num + 1
	end
	
	if greyed_count > 0 then
		table.insert(legend, elements.legend(false, grey, "其他詞彙(" .. greyed_count .. ")"))
	end

	local map_header = elements.map_header(
		m_links.full_link(
			{
				lang = lang,
				term = mw.ustring.gsub(frame.args[1], "%-.*", ""),
				gloss = syn_data["meaning"],
				tr = '-'
			}
		) .. "的漢語方言地圖"
	)

	local note = "\n''注意:本地圖沒有對移動端進行良好適配,請使用電腦瀏覽本頁。''"
	local map = elements.map(table.concat(points), table.concat(legend))

	return map_header .. note .. map
end

return export