import { Injectable, Injector } from '@angular/core';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { UploadStatusOverlayComponent } from '../upload-status-overlay/upload-status-overlay.component';

@Injectable()
export class StatusOverlayService {
    private readonly _uploadStatusOverlayRef: OverlayRef;

    constructor(private readonly _overlay: Overlay, private readonly _injector: Injector) {
        this._uploadStatusOverlayRef = this._overlay.create();
    }

    openUploadStatusOverlay() {
        const component = new ComponentPortal(UploadStatusOverlayComponent, null, this._createUploadInjector());
        if (!this._uploadStatusOverlayRef.hasAttached()) {
            this._uploadStatusOverlayRef.attach(component);
        }
    }

    private _createUploadInjector() {
        return Injector.create({
            providers: [{ provide: OverlayRef, useValue: this._uploadStatusOverlayRef }],
            parent: this._injector,
        });
    }
}
