import { Component, Inject, ViewChild } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { User } from '@red/domain';
import { UserDetailsComponent } from './user-details/user-details.component';
import { BaseDialogComponent } from '@iqser/common-ui';

@Component({
    selector: 'redaction-add-edit-user-dialog',
    templateUrl: './add-edit-user-dialog.component.html',
})
export class AddEditUserDialogComponent extends BaseDialogComponent {
    resettingPassword = false;
    @ViewChild(UserDetailsComponent) private readonly _userDetailsComponent: UserDetailsComponent;

    constructor(protected readonly _dialogRef: MatDialogRef<AddEditUserDialogComponent>, @Inject(MAT_DIALOG_DATA) readonly user: User) {
        super(_dialogRef, !!user);
    }

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

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

    toggleResetPassword() {
        this.resettingPassword = !this.resettingPassword;
    }

    async save(): Promise<void> {
        await this._userDetailsComponent.save();
    }

    closeDialog(event) {
        this._dialogRef.close(event);
    }
}
