import { Component, Inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { DetailsRadioOption, IconButtonTypes } from '@iqser/common-ui';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { OverwriteFileOption, OverwriteFileOptions } from '@red/domain';

@Component({
    selector: 'redaction-overwrite-files-dialog',
    templateUrl: './overwrite-files-dialog.component.html',
    styleUrls: ['./overwrite-files-dialog.component.scss'],
})
export class OverwriteFilesDialogComponent {
    readonly iconButtonTypes = IconButtonTypes;
    readonly form: UntypedFormGroup;
    readonly options: DetailsRadioOption<OverwriteFileOption>[] = [
        {
            label: _('overwrite-files-dialog.options.full-overwrite.label'),
            value: OverwriteFileOptions.FULL_OVERWRITE,
            description: _('overwrite-files-dialog.options.full-overwrite.description'),
        },
        {
            label: _('overwrite-files-dialog.options.partial-overwrite.label'),
            value: OverwriteFileOptions.PARTIAL_OVERWRITE,
            description: _('overwrite-files-dialog.options.partial-overwrite.description'),
        },
        {
            label: _('overwrite-files-dialog.options.skip.label'),
            value: OverwriteFileOptions.SKIP,
            description: _('overwrite-files-dialog.options.skip.description'),
        },
    ];

    constructor(
        private readonly _translateService: TranslateService,
        private readonly _formBuilder: UntypedFormBuilder,
        public dialogRef: MatDialogRef<OverwriteFilesDialogComponent>,
        @Inject(MAT_DIALOG_DATA) public filename: string,
    ) {
        this.form = this._formBuilder.group({
            option: [this.options[0], Validators.required],
            rememberChoice: [false],
        });
    }

    get rememberChoice() {
        return this.form.get('rememberChoice').value;
    }

    cancel() {
        this.dialogRef.close();
    }

    selectOption(applyToAllFiles: boolean = false) {
        this.dialogRef.close({
            option: this.form.get('option').value.value,
            rememberChoice: this.rememberChoice,
            applyToAllFiles,
        });
    }
}
