import logService from '../../services/logService';

export abstract class PresentableView {

    protected abstract logTag: string;

    public abstract presentSelf(): Promise<boolean>;

    public dismissSelf(injectedElem: HTMLElement | null, timeout: number): Promise<void> {
        return new Promise<void>((resolve) => {
            if (!injectedElem) {
                resolve();
            } else {
                injectedElem.classList.add('animated');
                injectedElem.classList.add('fadeOut');
                setTimeout(() => {
                    if (injectedElem && injectedElem.parentNode) {
                        injectedElem.parentNode.removeChild(injectedElem);
                    }
                    resolve();
                }, timeout);
            }
        });
    }

    protected logError(message: string) {
        logService.logError(this.formatMessage(message));
    }

    protected logInfo(message: string) {
        logService.logInfo(this.formatMessage(message));
    }

    protected log(message: string) {
        logService.log(this.formatMessage(message));
    }

    private formatMessage(message: string): string {
        return `${this.logTag} : ${message}`;
    }
}