require("Module:No globals")
local getArgs = require('Module:Arguments').getArgs

local p = {}

-- Converte una stringa di valori numerici separati da "," in una lista
-- Eventuali valori non numerici sono ignorati
local function read_array(args, base_name, list_name)
	local array = {}
	if args[list_name] then
		array = mw.text.split(string.gsub(args[list_name], "%s", ""), ",")
	end
	-- se l'array ha lunghezza nulla uso il vecchio metodo di inserimento
	if #array == 0 then 
		for i= 1,20 do
			local number = tonumber(args[base_name .. tonumber(i)])
			if number then
				array[#array+1] = number
			end
		end
	end
	return array
end

function p._demografia(args)
	local graph = {
		version = 2,
		width = 450,
		height = 250,
		padding = "auto",
		data = {
			{
				name = "table",
				transform = {
					{ type = "formula", field = "population", expr = "round(datum.population)" },
					{ type = "sort", by = "year" }
				},
				values = { }
			},
			{
				name = "table2",
				transform = {
					{ type = "formula", field = "population", expr = "round(datum.population)" },
					{ type = "sort", by = "year" }
				},
				values = {  }
			}
		},
		scales = {
			{
				name = "x",
				type = "linear",
				range = "width",
				zero = false,
				___domain = {fields = {{data = "table",field = "year"}, {data = "table2",field = "year"}}}
			},
			{
				name = "y",
				type = "linear",
				range = "height",
				nice = true,
				___domain = {fields = {{data = "table",field = "population"}, {data = "table2",field = "population"}}}
			}
		},
		axes = {
			{
				type = "x",
				scale = "x",
				format = "d",
				title = "Anno",
				grid = true,
			},
			{
				type = "y",
				scale = "y",
				title = "Abitanti",
				grid = true,
				layer = "back"
			}
		},
		marks = {
			{
				type = "line",
				from = {data = "table" },
				properties = {
					enter = {
						interpolate = {value = "linear"},
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population"},
						stroke = {value = "#fc8"},
						strokeWidth = {value = 3}
					}
				}
			},
			{
				type = "symbol",
				from = {data = "table2"},
				properties = {
					enter = {
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population"},
						stroke = {value = "#f80"},
						fill = {value = "#fff"},
						size = {value = 12}
					}
				}
			},
			{
				type = "symbol",
				from = {data = "table"},
				properties = {
					enter = {
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population"},
						stroke = {value = "#800"},
						fill = {value = "#fff"},
						size = {value = 30}
					}
				}
			},
			{
				type = "text",
				from = {data = "table"},
				properties = {
					enter = {
						x = {scale = "x",field = "year"},
						y = {scale = "y",field = "population", offset = -8},
						align = {value = "center"},
						fill = {value = "#000"},
						text = {field = "population" }
					}
				}
			},
			{
				type = "text",
				from = {
					data = "table",
					transform = {
						{
						 type = "aggregate",
						 summarize = {year = {"min","max"}}
						}
					}
				},
				properties = {
					enter = {
						x = {signal = "width",mult = 0.5},
						y = {value = -10},
						text = {
						 template = ""
						},
						fill = {value = "black"},
						fontSize = {value = 16},
						align = {value = "center"},
						fontWeight = {value = "bold"}
					}
				}
			}
		}
	}
	-- generazione del grafico
	local titolo = args.titolo or args[1] or "Abitanti censiti<br />"
	local populations = read_array(args, "p", "popolazione")
	local years = read_array(args, "a", "anni")
	local populations2 = read_array(args, "pb", "popolazione2")
	local years2 = read_array(args, "b", "anni2")
	--TODO error checking
	local values1 = { }
	for i =1,#populations do
		values1[i] = { year = years[i], population = populations[i] }
	end
	graph['data'][1]['values'] =  values1
	local values2 = { }
	for i =1,#populations2 do
		values2[i] = { year = years2[i], population = populations2[i] }
	end
	graph['data'][2]['values'] =  values2
	if args.dimx then 
	   local width = tonumber(args.dimx)
	   if width then graph['width'] = width end
	end
	if args.dimy then 	
	   local height = tonumber(args.dimy)
	   if height then graph['height'] = height end
	end
	--if true then return mw.text.jsonEncode(graph) end
	local template_text = "Censimenti dal {{datum.min_year}} al {{datum.max_year}}"
	if #values2 > 0 then 
		graph['marks'][5]['properties']['enter']['text']['template'] = template_text .. " e dati annuali"
	else
		graph['marks'][5]['properties']['enter']['text']['template'] = template_text 
	end
	return titolo .. mw.getCurrentFrame():extensionTag('graph', mw.text.jsonEncode(graph))
end

function p.demografia(frame)
	local args = getArgs(frame)
	return p._demografia(args)
end

return p