<?php

function mp_filter_compare($a, $b) {
	return strcmp($a->_name, $b->_name);
}

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

/*
	menu: [ { _name:"", field_1:[], field_2:[]  }, ... ]
	fields: [ countries:"", cities:"", categories:"", ... ]
*/

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 = mp_already_in_menu($menu, $fields[$name]);
	if ($menu_item != null) {
		// no need to add name if already exist
		// add to lists of event info (cities, countries, ...)
		foreach ($fields as $key_field => $value) {
			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 to list of location index
		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 = [];
		array_push($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) {
		$fields = array(
			"pays"			=> $event->location->country,
			"villes"		=> $event->location->city,
			"categories"	=> $event->categorie,
			"mode"			=> ($event->irl)? "En présentiel" : "En ligne",
		);
		$index = $event->index;
		foreach ($fields as $key => $value) {
			if (! isset($filters->$key))
				$filters->$key = [];
			mp_fill_name($fields, $key, $filters->$key, $index);
		}
	}

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

	return $filters;
}

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

/*
	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;
*/

?>
