import { ChangeDetectorRef, Component, ViewChild } from '@angular/core';
import { digitalSignatureDialogTranslations } from '../../translations/digital-signature-dialog-translations';
import { BaseDialogComponent, DetailsRadioOption } from '@iqser/common-ui';
import { MatDialogRef } from '@angular/material/dialog';
import { PkcsSignatureConfigurationComponent } from './form/pkcs-signature-configuration/pkcs-signature-configuration.component';
import { KmsSignatureConfigurationComponent } from './form/kms-signature-configuration/kms-signature-configuration.component';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { HttpStatusCode } from '@angular/common/http';
import { DigitalSignatureOption, DigitalSignatureOptions } from '@red/domain';

const DEFAULT_DIALOG_WIDTH = '662px';
const KMS_SIGNATURE_DIALOG_WIDTH = '810px';

@Component({
    templateUrl: './configure-certificate-dialog.component.html',
    styleUrls: ['./configure-certificate-dialog.component.scss'],
})
export class ConfigureCertificateDialogComponent extends BaseDialogComponent {
    @ViewChild(PkcsSignatureConfigurationComponent) pkcsSignatureConfigurationComponent: PkcsSignatureConfigurationComponent;
    @ViewChild(KmsSignatureConfigurationComponent) kmsSignatureConfigurationComponent: KmsSignatureConfigurationComponent;

    readonly digitalSignatureOptions = DigitalSignatureOptions;
    readonly translations = digitalSignatureDialogTranslations;
    readonly options: DetailsRadioOption<DigitalSignatureOption>[] = [
        {
            label: _('digital-signature-dialog.options.pkcs.label'),
            value: DigitalSignatureOptions.PKCS,
            description: _('digital-signature-dialog.options.pkcs.description'),
            id: 'pkcs-option',
        },
        {
            label: _('digital-signature-dialog.options.kms.label'),
            value: DigitalSignatureOptions.KMS,
            description: _('digital-signature-dialog.options.kms.description'),
            id: 'kms-option',
        },
    ];

    isInConfiguration = false;

    constructor(
        protected readonly _dialogRef: MatDialogRef<ConfigureCertificateDialogComponent>,
        private readonly _changeDetectorRef: ChangeDetectorRef,
    ) {
        super(_dialogRef);

        this.form = this._formBuilder.group({
            option: [this.options[0]],
        });
    }

    get disabled(): boolean {
        return this.activeComponent?.disabled;
    }

    get activeComponent() {
        return this.selectedOption === DigitalSignatureOptions.PKCS
            ? this.pkcsSignatureConfigurationComponent
            : this.kmsSignatureConfigurationComponent;
    }

    get selectedOption(): DigitalSignatureOption {
        return this.form.get('option').value.value;
    }

    toggleIsInConfiguration() {
        this.isInConfiguration = !this.isInConfiguration;
        if (this.isInConfiguration && this.selectedOption === DigitalSignatureOptions.KMS) {
            this._dialogRef.updateSize(KMS_SIGNATURE_DIALOG_WIDTH);
        } else {
            this._dialogRef.updateSize(DEFAULT_DIALOG_WIDTH);
        }
        this._changeDetectorRef.detectChanges();
    }

    async save(): Promise<void> {
        try {
            await this.activeComponent.save();
            this._toaster.success(_('digital-signature-dialog.actions.save-success'));
            this._dialogRef.close(true);
        } catch (error) {
            console.log(error);
            if (error.status === HttpStatusCode.BadRequest) {
                this._toaster.error(_('digital-signature-dialog.actions.certificate-not-valid-error'));
            } else {
                this._toaster.error(_('digital-signature-dialog.actions.save-error'));
            }
        }
    }
}
