let census;
let db;

let f = 0;
let industries;

noise.seed(Math.random());


function countField(db, field) {
	let dictionary = [];
	let c = 0;

	for (var i = 0; i < db.length; i++) {
		industries = db[i][field];
		categories = industries.split("|");
		for (var j = 0; j < categories.length; j++) {
			if (!(categories[j] in dictionary) ) {
				dictionary[categories[j]] = 0;
				c++;
			} else {
				dictionary[categories[j]] += 1;
			}
		}
	}
	dictionary.length = c;
	return dictionary;

}

let s;

function salaryToNumber(str) {

	let final = 0;
	str = str.replaceAll(",", "");
	s = str;
	if (str.indexOf(" to ") != -1) {
		intervals = str.split(" to ");
		intervals[0] = parseInt(intervals[0].substr(1, intervals[0].length));
		intervals[1] = parseInt(intervals[1].substr(1, intervals[1].length));

		final = (intervals[0] + intervals[1]) /2;

	} else if (str.substr(str.length-1, str.length) == "+") {
		final = 200000;
	} else if (str.substr(0, 4) == "Less") {
		final = 25000;
	}

	return final;
}

let dictionary = [];
	let count_salary = [];
	let count;
let avg;


//Funções tiradas de forums online
function randomInterval(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min); 
}

function map(number, inMin, inMax, outMin, outMax) {
    return (number - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}

function index_num(el) {
    if (!el) return -1;
    var i = 0;
    do {
        i++;
    } while (el = el.previousElementSibling);
    return i;
}

//Fim de funções tiradas de forums

function averageSalary(db) {
	dictionary = [];
	count_salary = [];
	let c = 0;

	for (var i = 0; i < db.length; i++) {
		hours = db[i]["work_hours"];
		if (!(hours in dictionary) ) {
			dictionary[hours] = 0;
			count_salary[hours] = 0;
			c++;
		} else {
			dictionary[hours] += salaryToNumber(db[i]["salary"]);
			count_salary[hours] += 1;
		}
		
	}
	dictionary.length = c;

	for (var i = 0; i < dictionary.length; i++) {
		dictionary[Object.keys(dictionary)[i]] = dictionary[Object.keys(dictionary)[i]] / count_salary[Object.keys(dictionary)[i]];
	}


	return dictionary;

}

function satisfactionByWorkHours(db) {
	dictionary = [];
	satis_dictionary = [];
	satisfaction_nums = [];
	let c = 0;

	for (var i = 0; i < db.length; i++) {
		hours = db[i]["work_hours"];
		satisf = db[i]["satisfaction"];

		if (!(hours in dictionary)) {
			
			dictionary[hours] = [];

			if ((satisf in dictionary[hours])) {
				dictionary[hours][satisf] += 1;
				dictionary[hours].length += 1;
			} else {
				dictionary[hours][satisf] = 0;
				c++;
			}

		} else {

			if ((satisf in dictionary[hours])) {
				dictionary[hours][satisf] += 1;
				dictionary[hours].length += 1;
			} else {
				dictionary[hours][satisf] = 0;
				//c++;
			}
			//dictionary[hours][satisf] = 2;
		}
		
	}
	

	for (var i = 0; i < Object.keys(dictionary).length; i++) {
		dictionary[Object.keys(dictionary)[0]] = dictionary[Object.keys(dictionary)[0]].sort();

	//varia = ordered_avg.splice(4, 1);
	//ordered_avg.unshift(varia[0]);
	}

	console.log(c);
	dictionary.length = c;



	return dictionary;

}

function groupToDictionary(field) {
	group = d3.group(db, d => d[field]);
	n = 0;
	dic = [];

	group.forEach(function(d, i) {
		dic[n] = i; 
		n++;
	})

	return dic;
}

function groupToDictionaryKeys(field) {
	group = d3.group(db, d => d[field]);
	n = 0;
	dic = {};

	group.forEach(function(d, i) {
		dic[i] = []; 
		n++;
	})

	return dic;
}

function initializeWith(arr) {
	cleanArr = {};

	let options = [];
	options = [0, false];


	for (var i = arr.length - 1; i >= 0; i--) {
		cleanArr[arr[i]] = [0, false];
	}
	return cleanArr;
}



let line;

d3.csv("design_census.csv", function(d) {
	return {
		industries: d.industries,
		salary: d.salary,
		work_hours: d.work_hours,
		satisfaction: d.satisfaction,
			age: d.age,

	race: d.race,

	gender: d.gender,

	["LGBTQIA+"]: d["LGBTQIA+"],

	opportunities: d.opportunities,

	jobs_before: d.jobs_before,

	design_work_years: d.design_work_years
	}
}).then(function(data) {

	db = data;

	canvas_01();

	canvas_02();

	canvas_03();

	document.getElementById("industries").onmousemove = function(ev) {
		canvas_01_onmousemove(ev);
	};

	document.getElementById("profile").onmousemove = function(ev) {
		canvas_02_onmousemove(ev);
	};

})