import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import {
    ConfirmationDialogComponent,
    DialogConfig,
    DialogService,
    IConfirmationDialogData,
    LoadingService,
    TitleColors,
} from '@iqser/common-ui';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { TrashItem } from '@red/domain';
import { firstValueFrom } from 'rxjs';
import { TrashService } from '@services/entity-services/trash.service';

type DialogType = 'confirm';

@Injectable()
export class TrashDialogService extends DialogService<DialogType> {
    protected readonly _config: DialogConfig<DialogType> = {
        confirm: {
            component: ConfirmationDialogComponent,
        },
    };

    constructor(
        protected readonly _dialog: MatDialog,
        private readonly _loadingService: LoadingService,
        private readonly _trashService: TrashService,
    ) {
        super(_dialog);
    }

    confirmHardDelete(items: TrashItem[]): void {
        const data: IConfirmationDialogData = {
            title: _('confirmation-dialog.delete-items.title'),
            titleColor: TitleColors.WARN,
            question: _('confirmation-dialog.delete-items.question'),
            translateParams: {
                name: items[0].name,
                itemsCount: items.length,
            },
        };

        this.openDialog('confirm', data, async () => {
            this._loadingService.start();
            await firstValueFrom(this._trashService.hardDelete(items));
            this._loadingService.stop();
        });
    }
}
