<?php
/**
 * Define a notification.
 * @author Valentin CARRUESCO
 * @category Plugin
 * @license copyright
 */
class Notification extends Entity{
	public $id,$label,$html,$type,$meta,$end,$start,$pinned;
	protected $TABLE_NAME = 'notification';
	public $fields =
	array(
		'id' => 'key',
		'label' => 'string',
		'html' => 'longstring',
		'type' => 'string',
		'meta' => 'longstring',
		'start' => 'date',
		'end' => 'date',
		'pinned' => 'int'
	);

	function __construct(){
 		parent::__construct();
 		$this->pinned = 0;
	}

	public static function types($key = null){
		$types = array(
			'notice' => array(
				'label' =>'Information',
				'color' =>'#007fdc',
				'icon'  =>'far fa-flag',
				'description'  =>"Notifications d'informations mineures",
				'default_methods' => array(
					'interface' => true,
					'mail' => false
				)
			),
			'announcement' => array(
				'label' =>'Annonce',
				'color' =>'#30336b',
				'icon'  =>'fas fa-bullhorn',
				'description'  =>"Annonce générale",
				'default_methods' => array(
					'interface' => true,
					'mail' => false
				)
			),
			'alert' => array(
				'label' =>'Alerte',
				'color' =>'#ff3100',
				'icon'  =>'fas fa-fire',
				'description'  =>"Notifications importantes ou critiques",
				'default_methods' => array(
					'interface' => true,
					'mail' => false
				)
			)
		);

		Plugin::callHook('notification_type',array(&$types));
		
		return $key ? $types[$key] : $types; 
	}

	public static function emit($infos){
		$sendTypes = array();
		Plugin::callHook("notification_methods", array(&$sendTypes));

		$default = array(
			'label' => "Notification sans titre",
			'html' => "",
			'type' => "notice",
			'pinned' => 0,
			'meta' => array(),
			'start' => time(),
			'end' => strtotime('+3month'),
			'recipients' => array()
		);
		$infos = array_merge($default,$infos);

		$notification = new Notification();
		$notification->label = $infos['label'];
		$notification->html = $infos['html'];
		$notification->type = $infos['type'];
		$notification->pinned = $infos['pinned'];
		$notification->meta = json_encode($infos['meta']);
		$notification->end = $infos['end'];
		$notification->start = $infos['start'];
		$notification->save();


		// pour chaque destinataire, vérification des préférences
		foreach ($infos['recipients'] as $recipient) {
			//$userPreferences = Notification::getNotificationPreferences($recipient);
			$userPreferences = Notification::settingPreferences($recipient);
			// pour chaque type d'envoi
			foreach ($sendTypes as $sendType) {
				if(!$userPreferences[$infos['type']][$infos['type'].'_'.$sendType['slug']]) continue;
				$sendType['callback']($recipient, $infos,$notification);
			}
		}
	}

	public static function clear(){
		foreach (Notification::loadAll(array('end:<'=>time())) as $notification) {
			UserNotification::delete(array('notification'=>$notification->id));
			Notification::deleteById($notification->id);
		}
	}

	public static function settingPreferences($user, $userPreferences=array()){
		require_once(__DIR__.SLASH.'UserNotification.class.php');

		// récupération préférences par défault
		$defaultPreferences = array();
		foreach(Notification::types() as $slug => $type){
			$defaultMethods = array();
			foreach ($type['default_methods'] as $method => $state)
				$defaultMethods[$slug.'_'.$method] = $state;

			$defaultPreferences[$slug] = $defaultMethods;
		}

		if(empty($userPreferences)){
			// récupération préférences utilisateur
			$preferences = UserPreference::loadAll(array('user'=>$user, 'key'=>'notification_categories'));
			$userPreferences = array();
			foreach($preferences as $preference)
				$userPreferences = json_decode($preference->value, JSON_OBJECT_AS_ARRAY);
		}

		$notificationPreferences = array();
		foreach ($defaultPreferences as $slug => $type){
			$tab = array();
			$notificationPreferences[$slug] = $defaultPreferences[$slug];
			if(!isset($userPreferences[$slug])) continue;
			foreach ($type as $method => $state)
				$tab[$method] = (isset($userPreferences[$slug][$method])) ? $userPreferences[$slug][$method] : $defaultPreferences[$slug][$method];
			$notificationPreferences[$slug] = $tab;
		}
		return $notificationPreferences;
	}
}
?>