import { NotificationClass, NotificationClasses } from './notification-class';
import { NotificationModel } from './notification-element/notification-element.component';
import { Component, OnInit } from '@angular/core';

/**
 * Class which offers the notifications service.
 */
@Component({
	selector: 'app-notifications',
	templateUrl: 'notifications.component.html',
	styles: [`
		#notification-container {
			position: fixed;
			top: 50px;
			right: 20px;
			width: 300px;
			z-index: 1100;
		}
	`]
})
export class NotificationsComponent implements OnInit {
	/**
	 * Singleton of the notification service.
	 */
	private static component: NotificationsComponent;

	/**
	 * List of notifications model.
	 */
	notificationList: Array<NotificationModel> = [];

		/**
	 * Creates an error notification.
	 * @param {string} message The content of the notification.
	 */
	public static error(message: string): void {
		NotificationsComponent.notif(message, NotificationClasses.Error);
	}

	/**
	 * Creates a warning notification.
	 * @param {string} message The content of the notification.
	 */
	public static warn(message: string): void {
		NotificationsComponent.notif(message, NotificationClasses.Warn);
	}

	/**
	 * Creates an info notification.
	 * @param {string} message The content of the notification.
	 */
	public static info(message: string): void {
		NotificationsComponent.notif(message, NotificationClasses.Info);
	}

	/**
	 * Creates a success notification.
	 * @param {string} message The content of the notification.
	 */
	public static success(message: string): void {
		NotificationsComponent.notif(message, NotificationClasses.Success);
	}

	/**
	 * Create a notification. The {@code notifClass} param defines the category of
	 * the notification (info, error...).
	 * @param {string} message The content of the notification.
	 * @param {NotificationClass} notifClass The category of the notification.
	 */
	private static notif(message: string, notifClass: NotificationClass): void {
		const elem = new NotificationModel(message, notifClass);

		NotificationsComponent.component.notificationList.push(elem);

		setTimeout(() => {
			elem.hide();

			setTimeout(() => {
				NotificationsComponent.clearNotificationList();
			}, 900);
		}, 4500);
	}

	/**
	 * Clears the consumed notifications in the list.
	 * When a notification is created, a cooldown is set to hide it after a certain time period.
	 * In this cooldown, the notification have only its display as {@code none}, but the
	 * notification isn't remove from the list. This method removes it.
	 */
	private static clearNotificationList(): void {
		NotificationsComponent.component.notificationList.forEach(elem => {
			if (elem.notification.nativeElement.style.display === 'none') {
				const index = NotificationsComponent.component.notificationList.indexOf(elem);
				if (index > -1) {
					NotificationsComponent.component.notificationList.splice(index, 1);
				}
			}
		});
	}

	/**
	 * Set the reference of the singleton here because this component
	 * is created at the application startup.
	 */
	ngOnInit(): void {
		NotificationsComponent.component = this;
	}
}
