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 { IUnitType } from 'app/entities/unit-type/unit-type.model';
import { UnitTypeService } from 'app/entities/unit-type/service/unit-type.service';
import { IUnit } from '../unit.model';
import { UnitService } from '../service/unit.service';
import { UnitFormService, UnitFormGroup } from './unit-form.service';

@Component({
  standalone: true,
  selector: 'jhi-unit-update',
  templateUrl: './unit-update.component.html',
  imports: [SharedModule, FormsModule, ReactiveFormsModule],
})
export class UnitUpdateComponent implements OnInit {
  isSaving = false;
  unit: IUnit | null = null;

  unitTypesSharedCollection: IUnitType[] = [];

  protected unitService = inject(UnitService);
  protected unitFormService = inject(UnitFormService);
  protected unitTypeService = inject(UnitTypeService);
  protected activatedRoute = inject(ActivatedRoute);

  // eslint-disable-next-line @typescript-eslint/member-ordering
  editForm: UnitFormGroup = this.unitFormService.createUnitFormGroup();

  compareUnitType = (o1: IUnitType | null, o2: IUnitType | null): boolean => this.unitTypeService.compareEntity(o1, o2);

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

      this.loadRelationshipsOptions();
    });
  }

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

  save(): void {
    this.isSaving = true;
    const unit = this.unitFormService.getUnit(this.editForm);
    if (unit.id !== null) {
      this.subscribeToSaveResponse(this.unitService.update(unit));
    } else {
      this.subscribeToSaveResponse(this.unitService.create(unit));
    }
  }

  protected subscribeToSaveResponse(result: Observable<HttpResponse<IUnit>>): 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(unit: IUnit): void {
    this.unit = unit;
    this.unitFormService.resetForm(this.editForm, unit);

    this.unitTypesSharedCollection = this.unitTypeService.addEntityToCollectionIfMissing<IUnitType>(
      this.unitTypesSharedCollection,
      unit.unitType,
    );
  }

  protected loadRelationshipsOptions(): void {
    this.unitTypeService
      .query()
      .pipe(map((res: HttpResponse<IUnitType[]>) => res.body ?? []))
      .pipe(
        map((unitTypes: IUnitType[]) => this.unitTypeService.addEntityToCollectionIfMissing<IUnitType>(unitTypes, this.unit?.unitType)),
      )
      .subscribe((unitTypes: IUnitType[]) => (this.unitTypesSharedCollection = unitTypes));
  }
}
