<?php

function mp_filter_compare($a, $b) {

	$compare = strcmp($a->_name, $b->_name);

	if ($a->_name === "Autre") {
		if ($compare !== 0)
			return 1;
	}
	if ($b->_name === "Autre") {
		if ($compare !== 0)
			return -1;
	}

	return $compare;
}

function mp_already_in_menu(&$menu, $name) {
	foreach ($menu as $field) {
		if ($field->_name == $name)
			return $field;
	}
	return null;
}


/**
 * creates the menus
 * from the events fields
 * and add the index of the event as it is in location (events when they are sorted)
 *
 * fields: [ countries:"", cities:"", categories:"", ... ]
 * name  : field's values -> countries, cities, categories, ...
 * menu  : [ { _name:"", field_1:[], field_2:[]  }, ... ]
 * index : index of this event in locations[] array
 */
function mp_fill_name($fields, $name, &$menu, $index) {
	if ($fields[$name] == null)
		return;
	if (gettype($fields[$name]) != 'string')
		return;
	if (strlen($fields[$name]) == 0)
		return;
	// menu_item, ex: for menu "countries" -> france
	$menu_item = mp_already_in_menu($menu, $fields[$name]);
	if ($menu_item != null) {
		// add to this menu item, eg "Austria", the infos of this
		// event, like "city" or "category", if not there already
		foreach ($fields as $key_field => $value) {
			// no need to add name if already exist
			if ($key_field == $name)
				continue;
			if (! isset($menu_item->$key_field) )
				$menu_item->$key_field = [];
			if (strlen($value) != 0) {
				if (! in_array($value, $menu_item->$key_field) )
					array_push($menu_item->$key_field, $value);
			}
		}
		// add location index, if not there already
		if (! in_array($index, $menu_item->indexes) ) {
			array_push($menu_item->indexes, $index);
		}
	}
	else {
		$menu_item = (object)[];
		$menu_item->_name = $fields[$name];
		// add lists of event info (cities, countries, ...)
		foreach ($fields as $key_field => $value) {
			if ($key_field == $name)
				continue;
			$menu_item->$key_field = [];
			if (strlen($value) != 0)
				array_push($menu_item->$key_field, $value);
		}
		// add list of location index
		$menu_item->indexes = [$index];

		// and add this item to list of menu
		array_push($menu, $menu_item);
	}
}

function mp_get_filters(&$events) {
	$filters = (object)[];

	foreach ($events as $event) {

		// no index means no coordinates
		$index = $event->index;
		if ($index === null)
			continue;

		// create array of menus
		$fields = array(
			"Pays"			=> $event->location->country,
			"Catégories"	=> $event->categorie,
			"mode"			=> ($event->irl)? "En présentiel" : "En ligne",
		);

		// fill all menu with other menus without doubles
		foreach ($fields as $name => $value) {
			if (! isset($filters->$name))
				$filters->$name = [];
			mp_fill_name($fields, $name, $filters->$name, $index);
		}
	}

	foreach ($filters as $key => $value) {
		usort($filters->$key, 'mp_filter_compare');
	}

	return $filters;
}

/*
         {                                                 }
         {                [                             ]  }
filters: {  - countries : [ {  - _name     : ""  }      ]  }
         {                [ {  - categories: []  }      ]  }
         {                [ {  - indexes   : []  }, ... ]  }
         {                [                             ]  }
         {                                                 }
         {  - categories:                                  }
         {  - modes     :                                  }
         {                                                 }
*/

/*
	event					: {}
		- heure_de_debut	: "";
		- heure_de_fin		: "";
		- categorie			: "";
		- date				: "";
		- pays				: "";
		- ville				: "";
		- adresse			: "";
		- prenom			: "";
		- nom				: "";
		- irl				: bool;
		- id				: x;
		- index				: x;
		- title				: "";
		- location			: {}
			- street		: "";
			- city			: "";
			- country		: "";
			- address		: "";
			- approximate	: bool;
			- coordinates	: {}
				- lat		: x;
				- lng		: x;
*/

?>
