import { ChangeDetectionStrategy, Component, inject, ViewChild } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { AddEditEntityComponent } from '@shared/components/add-edit-entity/add-edit-entity.component';
import { BaseDialogComponent, IconButtonTypes } from '@iqser/common-ui';
import { Roles } from '@users/roles';

interface DialogData {
    readonly dossierTemplateId: string;
}

@Component({
    templateUrl: './add-entity-dialog.component.html',
    styleUrls: ['./add-entity-dialog.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AddEntityDialogComponent extends BaseDialogComponent {
    readonly iconButtonTypes = IconButtonTypes;
    readonly roles = Roles;
    readonly data = inject<DialogData>(MAT_DIALOG_DATA);
    @ViewChild(AddEditEntityComponent, { static: true }) private readonly _addEditEntityComponent: AddEditEntityComponent;

    constructor(protected readonly _dialogRef: MatDialogRef<AddEntityDialogComponent>) {
        super(_dialogRef, false);
    }

    get valid(): boolean {
        return this._addEditEntityComponent.valid;
    }

    get changed(): boolean {
        return this._addEditEntityComponent.changed;
    }

    async save(): Promise<void> {
        try {
            await this._addEditEntityComponent.save();
            this._dialogRef.close(true);
        } catch (e) {
            console.error(e);
        }
    }
}
