import { Component, inject, Input } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { defaultDialogConfig, EntitiesService } from '@iqser/common-ui';
import { DossierState, IDossierState } from '@red/domain';
import { PermissionsService } from '@services/permissions.service';
import {
    AddEditDossierStateDialogComponent,
    AddEditDossierStateDialogData,
} from '../add-edit-dossier-state-dialog/add-edit-dossier-state-dialog.component';
import {
    ConfirmDeleteDossierStateDialogComponent,
    ConfirmDeleteDossierStateDialogData,
} from '../confirm-delete-dossier-state-dialog/confirm-delete-dossier-state-dialog.component';

@Component({
    selector: 'redaction-dossier-states-table-item',
    templateUrl: './dossier-states-table-item.component.html',
    styleUrls: ['./dossier-states-table-item.component.scss'],
})
export class DossierStatesTableItemComponent {
    readonly #dialog = inject(MatDialog);
    readonly #entitiesService = inject(EntitiesService);
    @Input() state: DossierState;
    readonly permissionsService = inject(PermissionsService);

    openConfirmDeleteStateDialog(dossierState: DossierState) {
        const data: ConfirmDeleteDossierStateDialogData = {
            toBeDeletedState: dossierState,
            otherStates: this.#entitiesService.all.filter(s => s.id !== dossierState.id),
            dossierCount: dossierState.dossierCount,
        };

        this.#dialog.open<ConfirmDeleteDossierStateDialogComponent, ConfirmDeleteDossierStateDialogData>(
            ConfirmDeleteDossierStateDialogComponent,
            {
                ...defaultDialogConfig,
                data,
            },
        );
    }

    openEditStateDialog(dossierState: IDossierState) {
        this.#dialog.open<AddEditDossierStateDialogComponent, AddEditDossierStateDialogData>(AddEditDossierStateDialogComponent, {
            ...defaultDialogConfig,
            data: {
                dossierState,
                dossierTemplateId: dossierState.dossierTemplateId,
            },
        });
    }
}
