import { Component, inject, OnInit } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { finalize, map } from 'rxjs/operators';

import SharedModule from 'app/shared/shared.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { IInputDataUpload } from 'app/entities/input-data-upload/input-data-upload.model';
import { InputDataUploadService } from 'app/entities/input-data-upload/service/input-data-upload.service';
import { IInputDataUploadLog } from '../input-data-upload-log.model';
import { InputDataUploadLogService } from '../service/input-data-upload-log.service';
import { InputDataUploadLogFormService, InputDataUploadLogFormGroup } from './input-data-upload-log-form.service';

@Component({
  standalone: true,
  selector: 'jhi-input-data-upload-log-update',
  templateUrl: './input-data-upload-log-update.component.html',
  imports: [SharedModule, FormsModule, ReactiveFormsModule],
})
export class InputDataUploadLogUpdateComponent implements OnInit {
  isSaving = false;
  inputDataUploadLog: IInputDataUploadLog | null = null;

  inputDataUploadsSharedCollection: IInputDataUpload[] = [];

  protected inputDataUploadLogService = inject(InputDataUploadLogService);
  protected inputDataUploadLogFormService = inject(InputDataUploadLogFormService);
  protected inputDataUploadService = inject(InputDataUploadService);
  protected activatedRoute = inject(ActivatedRoute);

  // eslint-disable-next-line @typescript-eslint/member-ordering
  editForm: InputDataUploadLogFormGroup = this.inputDataUploadLogFormService.createInputDataUploadLogFormGroup();

  compareInputDataUpload = (o1: IInputDataUpload | null, o2: IInputDataUpload | null): boolean =>
    this.inputDataUploadService.compareEntity(o1, o2);

  ngOnInit(): void {
    this.activatedRoute.data.subscribe(({ inputDataUploadLog }) => {
      this.inputDataUploadLog = inputDataUploadLog;
      if (inputDataUploadLog) {
        this.updateForm(inputDataUploadLog);
      }

      this.loadRelationshipsOptions();
    });
  }

  previousState(): void {
    window.history.back();
  }

  save(): void {
    this.isSaving = true;
    const inputDataUploadLog = this.inputDataUploadLogFormService.getInputDataUploadLog(this.editForm);
    if (inputDataUploadLog.id !== null) {
      this.subscribeToSaveResponse(this.inputDataUploadLogService.update(inputDataUploadLog));
    } else {
      this.subscribeToSaveResponse(this.inputDataUploadLogService.create(inputDataUploadLog));
    }
  }

  protected subscribeToSaveResponse(result: Observable<HttpResponse<IInputDataUploadLog>>): void {
    result.pipe(finalize(() => this.onSaveFinalize())).subscribe({
      next: () => this.onSaveSuccess(),
      error: () => this.onSaveError(),
    });
  }

  protected onSaveSuccess(): void {
    this.previousState();
  }

  protected onSaveError(): void {
    // Api for inheritance.
  }

  protected onSaveFinalize(): void {
    this.isSaving = false;
  }

  protected updateForm(inputDataUploadLog: IInputDataUploadLog): void {
    this.inputDataUploadLog = inputDataUploadLog;
    this.inputDataUploadLogFormService.resetForm(this.editForm, inputDataUploadLog);

    this.inputDataUploadsSharedCollection = this.inputDataUploadService.addEntityToCollectionIfMissing<IInputDataUpload>(
      this.inputDataUploadsSharedCollection,
      inputDataUploadLog.inputDataUpload,
    );
  }

  protected loadRelationshipsOptions(): void {
    this.inputDataUploadService
      .query()
      .pipe(map((res: HttpResponse<IInputDataUpload[]>) => res.body ?? []))
      .pipe(
        map((inputDataUploads: IInputDataUpload[]) =>
          this.inputDataUploadService.addEntityToCollectionIfMissing<IInputDataUpload>(
            inputDataUploads,
            this.inputDataUploadLog?.inputDataUpload,
          ),
        ),
      )
      .subscribe((inputDataUploads: IInputDataUpload[]) => (this.inputDataUploadsSharedCollection = inputDataUploads));
  }
}
