
// https://googlemaps.github.io/js-markerclusterer/classes/MarkerClusterer.html
// add true for noDraw
// bounds : https://stackoverflow.com/questions/19304574/center-set-zoom-of-map-to-cover-all-visible-markers/19304625#19304625



function array_first_not_in_second(first, second) {
	let temp_array = [];
	for (let index of first) {
		if (second.indexOf(index) == -1) {
			temp_array.push(index);
		}
	}
	return temp_array;
}

/**
 * 
 */

function filter_selection_indexes(menu, indexes, reverse, add) {

	if (indexes.length === 0) {
	// if array of index is empty, delete menu
		delete g_indexes[menu];
	}
	else if (reverse) {
	// if reverse is true, delete all indexes in g_indexes.menu
		// https://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized
		if ( typeof(g_indexes[menu]) !== "undefined" && g_indexes[menu] !== null ) {
			// creates an array with all values of g_indexes[menu] minus indexes
			let temp_array = array_first_not_in_second(g_indexes[menu], indexes);
			if (temp_array.length === 0)
				delete g_indexes[menu];
			else
				g_indexes[menu] = [].concat(temp_array);
		}
	}
	else {
		if (add && typeof(g_indexes[menu]) !== "undefined") {
		// add array of menus to g_indexes{}
			// creates an array with all values of indexes that are not in g_indexes[menu] already
			let temp_array = array_first_not_in_second(indexes, g_indexes[menu]);
			g_indexes[menu] = g_indexes[menu].concat(temp_array);
		}
		else {
		// replace or create array of menu in g_indexes{}
			g_indexes[menu] = [].concat(indexes);
		}
	}

	// loop through all arrays of g_indexes to find intersection
	// take first one as comparison
	let keys = Object.keys(g_indexes);
	let intersection = [];
	let compare = [];
	keys.forEach((key, i) => {
		if (i == 0) {
			intersection = g_indexes[key];
		}
		else {
			let temp = [];
			compare = g_indexes[key];
			for (let index of intersection) {
				if (compare.indexOf(index) != -1)
					temp.push(index);
			}
			intersection = [].concat(temp);
		}
	});
	return intersection;
}



function is_element_unchecked(element) {
	if (typeof(element) === "undefined")
		return false;
	if (typeof(element.type) === "undefined")
		return false;
	if (element.type === "checkbox") {
		return ! element.checked;
	}
	return false;
}

/**
 * if zoom_in is true:
 *   zoom to new selection,
 *   even if already visible in current view
 */

function filter_show_only_selection(element, indexes, menu, add = false, zoom_in = true) {

	g_infowindow.close();

	// BAD WORKAROUND solution because we can't actually put onclick event inside select options
	// on chrome, so I have to recover the indexes another way
	if (element.type == "select-one") {
		if (element.value == menu)
			indexes = [];
		else {
			indexes = element.children[element.selectedIndex].title;
			indexes = JSON.parse(indexes);
		}
	}

	let reverse = is_element_unchecked(element);

	let index_array = filter_selection_indexes(menu, indexes, reverse, add);

	let indexes_count = index_array.length;

	if (indexes_count !== 0) {
	// if index array, hide all other markers, and if zoomin, zoom to new markers
		g_marker_cluster.clearMarkers(true);

		let marker = g_markers[0];
		let current_bounds = g_map.getBounds();
		let bounds = new google.maps.LatLngBounds();

		let outside_bounds = false;
		for (let index of index_array) {
			marker = g_markers[index];
			position = marker.getPosition();
			if (! current_bounds.contains(position))
				outside_bounds = true;
			bounds.extend(position);

			g_marker_cluster.addMarker(marker, true);
		}
		if (zoom_in || outside_bounds) {
			if (indexes_count === 1) {
				g_map.setCenter(position);
				g_map.setZoom(max_zoom);
			}
			else if (indexes_count > 1) {
				console.log("index_bound");
				g_map.fitBounds(bounds);
			}
		}

	}
	else if ( Object.keys(g_indexes).length === 0 ) {
	// object is empty, there are no filters
		g_map.setCenter(coordinates_default);
		g_map.setZoom(2);
		g_marker_cluster.addMarkers(g_markers, true);
	}
	else { 
	// filters intersection lets no markers on the map
		g_marker_cluster.clearMarkers(true);
	}

	g_marker_cluster.render();
}

function filter_show_all() {
	g_infowindow.close();

	g_indexes = {};
	g_marker_cluster.clearMarkers(true);
	g_marker_cluster.addMarkers(g_markers);
	/* dont use fitBounds because it's not well centered */
	//g_map.fitBounds(g__init_bounds);
	g_map.setCenter(coordinates_default);
	g_map.setZoom(2);
}

