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, FormControl } from '@angular/forms';

import { IUnit } from 'app/entities/unit/unit.model';
import { UnitService } from 'app/entities/unit/service/unit.service';
import { IUnitConverter } from '../unit-converter.model';
import { UnitConverterService } from '../service/unit-converter.service';
import { UnitConverterFormService, UnitConverterFormGroup } from './unit-converter-form.service';

import { CodeMirrorFieldComponent } from 'app/resilient/codemirrorfield/codemirrorfield.component'

@Component({
  standalone: true,
  selector: 'jhi-unit-converter-update',
  templateUrl: './unit-converter-update.component.html',
  styleUrl: './unit-converter-update.component.css',
  imports: [SharedModule, FormsModule, ReactiveFormsModule, CodeMirrorFieldComponent],
})
export class UnitConverterUpdateComponent implements OnInit {
  isSaving = false;
  isCollapsed = true;
  unitConverter: IUnitConverter | null = null;

  unitsSharedCollection: IUnit[] = [];

  protected unitConverterService = inject(UnitConverterService);
  protected unitConverterFormService = inject(UnitConverterFormService);
  protected unitService = inject(UnitService);
  protected activatedRoute = inject(ActivatedRoute);

  // eslint-disable-next-line @typescript-eslint/member-ordering
  editForm: UnitConverterFormGroup = this.unitConverterFormService.createUnitConverterFormGroup();

  compareUnit = (o1: IUnit | null, o2: IUnit | null): boolean => this.unitService.compareEntity(o1, o2);

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

      this.loadRelationshipsOptions();
    });
  }
  
  toggleCollapse(): void {
    this.isCollapsed = !this.isCollapsed;
  }
  
  previousState(): void {
    window.history.back();
  }

  save(): void {
    this.isSaving = true;
    const unitConverter = this.unitConverterFormService.getUnitConverter(this.editForm);
    if (unitConverter.id !== null) {
      this.subscribeToSaveResponse(this.unitConverterService.update(unitConverter));
    } else {
      this.subscribeToSaveResponse(this.unitConverterService.create(unitConverter));
    }
  }

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

    this.unitsSharedCollection = this.unitService.addEntityToCollectionIfMissing<IUnit>(
      this.unitsSharedCollection,
      unitConverter.fromUnit,
      unitConverter.toUnit,
    );
  }

  protected loadRelationshipsOptions(): void {
    this.unitService
      .query()
      .pipe(map((res: HttpResponse<IUnit[]>) => res.body ?? []))
      .pipe(
        map((units: IUnit[]) =>
          this.unitService.addEntityToCollectionIfMissing<IUnit>(units, this.unitConverter?.fromUnit, this.unitConverter?.toUnit),
        ),
      )
      .subscribe((units: IUnit[]) => (this.unitsSharedCollection = units));
  }
}
