import { NgIf } from '@angular/common';
import { Component, Inject } from '@angular/core';
import { ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { MatCheckbox } from '@angular/material/checkbox';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { DetailsRadioOption } from '@common-ui/inputs/details-radio/details-radio-option';
import { DetailsRadioComponent } from '@common-ui/inputs/details-radio/details-radio.component';
import { IconButtonComponent, IconButtonTypes } from '@iqser/common-ui';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { OverwriteFileOption, OverwriteFileOptions } from '@red/domain';

@Component({
    selector: 'redaction-overwrite-files-dialog',
    templateUrl: './overwrite-files-dialog.component.html',
    styleUrls: ['./overwrite-files-dialog.component.scss'],
    standalone: true,
    imports: [NgIf, TranslateModule, ReactiveFormsModule, DetailsRadioComponent, MatCheckbox, IconButtonComponent],
})
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,
        });
    }
}
