import { Injectable } from '@angular/core';
import { MatBottomSheet, MatBottomSheetRef } from '@angular/material/bottom-sheet';
import { NotificationsComponent } from '../components/notifications/notifications.component';
import { v4 as uuidv4 } from 'uuid';
import moment, { Moment } from 'moment';

@Injectable({
  providedIn: 'root',
})
export class NotificationsService {

  public messages: IMessage[] = [];
  private handler?: MatBottomSheetRef;

  constructor(
    private bottomSheet$: MatBottomSheet
  ) { }
  public add(message: IMessage): string {
    message.id = uuidv4();
    message.createdAt = moment();
    this.messages.push(message);
    this.sortMessages();
    if (this.messages.length === 1) {
      this.handler = this.bottomSheet$.open(NotificationsComponent, {
        hasBackdrop: false,
        disableClose: true
      });
    }
    setTimeout(() => {
      this.remove(message.id);
    }, message.duration ? message.duration : 5000);
    // close parent
    if (message.parentId) {
      setTimeout(() => {
        this.remove(message.parentId);
      }, 500);
    }
    return message.id;
  }
  private remove(id: string | undefined, checkDismiss: boolean = true) {
    if (checkDismiss) {
      const message = this.messages.find(x => x.id === id);
      if (message && message.dismiss) {
        message.dismiss();
      }
    }
    this.messages = this.messages.filter(x => x.id !== id);
    this.sortMessages();
    if (this.messages.length === 0 && this.handler) {
      this.handler.dismiss();
    }
  }
  private sortMessages() {
    this.messages = this.messages.sort((a, b) => {
      return a.createdAt && a.createdAt.isAfter(b.createdAt) ? 1 : -1;
    })
  }
  doAction(message: IMessage) {
    if (message.action) { message.action(); }
    this.remove(message.id, false);
  }
}

interface IMessage {
  id?: string;
  text: string;
  duration?: number;
  btn?: string;
  // eslint-disable-next-line @typescript-eslint/ban-types
  action?: Function;
  // eslint-disable-next-line @typescript-eslint/ban-types
  dismiss?: Function;
  createdAt?: Moment,
  parentId?: string;
}
