import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { MessageService } from 'primeng/api';

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

    constructor(private translate: TranslateService, private messageService: MessageService) { }

    showInfoMessageTrans(message: string) {
        this.translate.get([ "LABEL.INFO", message ]).subscribe(o => {
            const values = Object.values(o) as string[];
            this.messageService.add({severity:'info', summary: values[0], detail: values[1]});
        });
    }

    showWarnMessageTrans(message: string) {
        this.translate.get([ "LABEL.WARN", message ]).subscribe(o => {
            const values = Object.values(o) as string[];
            this.messageService.add({severity:'warn', summary: values[0], detail: values[1]});
        });
    }

    showErrorMessageTrans(message: string) {
        this.translate.get([ "LABEL.DANGER", message ]).subscribe(o => {
            const values = Object.values(o) as string[];
            this.messageService.add({severity:'error', summary: values[0], detail: values[1]});
        });
    }

    showInfoMessage(message: string) {
        this.translate.get("LABEL.INFO").subscribe(o => {
            this.messageService.add({severity:'info', summary: o, detail: message});
        });
    }

    showWarnMessage(message: string) {
        this.translate.get("LABEL.WARN").subscribe(o => {
            this.messageService.add({severity:'warn', summary: o, detail: message});
        });
    }

    showErrorMessage(message: string) {
        this.translate.get("LABEL.DANGER").subscribe(o => {
            this.messageService.add({severity:'error', summary: o, detail: message});
        });
    }

    isBlank(str: any) {
        return (str === undefined || str === null || /^\s*$/.test(str));
    }

    getBase64Header(type: string) : string {
        if (type == "image/svg+xml") {
            return "data:image/svg+xml;base64,"
        }
        else if (type == 'image/png') {
            return "data:image/png;base64,"
        }
        else if (type == 'image/jpg') {
            return "data:image/jpg;base64,"
        }
        else if (type == 'image/jpeg') {
            return "data:image/jpeg;base64,"
        }

        return "";
    }
}
