import {Injectable} from "@angular/core";

export interface Flashmessage {
    body: string;
    delay?: number;
    type: "Error"|"Success"|"Info";
}

@Injectable({ providedIn: 'root' })
export class FlashmessagesService {
    toasts: Flashmessage[] = [];

    success(body: string) {
        this.show("Success", body);
    }

    error(body: string) {
        this.show("Error", body);
    }

    show(type: string, body: string) {
        if (type == "Success") {
            this.toasts.push({ type, body });
        } else if (type == "Error") {
            this.toasts.push({ type, body });
        } else {
            this.toasts.push({ type: "Info", body });
        }
    }

    remove(toast: Flashmessage) {
        this.toasts = this.toasts.filter(t => t != toast);
    }
}