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 { IVariableScope } from 'app/entities/variable-scope/variable-scope.model';
import { VariableScopeService } from 'app/entities/variable-scope/service/variable-scope.service';
import { IVariableCategory } from '../variable-category.model';
import { VariableCategoryService } from '../service/variable-category.service';
import { VariableCategoryFormService, VariableCategoryFormGroup } from './variable-category-form.service';

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

  variableScopesSharedCollection: IVariableScope[] = [];

  protected variableCategoryService = inject(VariableCategoryService);
  protected variableCategoryFormService = inject(VariableCategoryFormService);
  protected variableScopeService = inject(VariableScopeService);
  protected activatedRoute = inject(ActivatedRoute);

  // eslint-disable-next-line @typescript-eslint/member-ordering
  editForm: VariableCategoryFormGroup = this.variableCategoryFormService.createVariableCategoryFormGroup();

  compareVariableScope = (o1: IVariableScope | null, o2: IVariableScope | null): boolean =>
    this.variableScopeService.compareEntity(o1, o2);

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

      this.loadRelationshipsOptions();
    });
  }

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

  save(): void {
    this.isSaving = true;
    const variableCategory = this.variableCategoryFormService.getVariableCategory(this.editForm);
    if (variableCategory.id !== null) {
      this.subscribeToSaveResponse(this.variableCategoryService.update(variableCategory));
    } else {
      this.subscribeToSaveResponse(this.variableCategoryService.create(variableCategory));
    }
  }

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

    this.variableScopesSharedCollection = this.variableScopeService.addEntityToCollectionIfMissing<IVariableScope>(
      this.variableScopesSharedCollection,
      variableCategory.variableScope,
    );
  }

  protected loadRelationshipsOptions(): void {
    this.variableScopeService
      .query()
      .pipe(map((res: HttpResponse<IVariableScope[]>) => res.body ?? []))
      .pipe(
        map((variableScopes: IVariableScope[]) =>
          this.variableScopeService.addEntityToCollectionIfMissing<IVariableScope>(
            variableScopes,
            this.variableCategory?.variableScope,
          ),
        ),
      )
      .subscribe((variableScopes: IVariableScope[]) => (this.variableScopesSharedCollection = variableScopes));
  }
}
