import { Component, Inject } from '@angular/core';
import { UntypedFormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { getCurrentUser } from '@users/user.service';
import { ISmtpConfiguration } from '@red/domain';
import { BaseDialogComponent } from '@iqser/common-ui';

@Component({
    selector: 'redaction-smtp-auth-dialog',
    templateUrl: './smtp-auth-dialog.component.html',
})
export class SmtpAuthDialogComponent extends BaseDialogComponent {
    readonly #currentUser = getCurrentUser();

    constructor(
        protected readonly _dialogRef: MatDialogRef<SmtpAuthDialogComponent>,
        @Inject(MAT_DIALOG_DATA) readonly data: ISmtpConfiguration,
    ) {
        super(_dialogRef);
        this.form = this._getForm();
        this.initialFormValue = this.form.getRawValue();
    }

    save() {
        this._dialogRef.close(this.form.getRawValue());
    }

    private _getForm(): UntypedFormGroup {
        return this._formBuilder.group({
            user: [this.data?.user || this.#currentUser.email, [Validators.required]],
            password: [this.data?.password, Validators.required],
        });
    }
}
