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

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

import { IVariableClassType } from '../variable-class-type.model';
import { VariableClassTypeService } from '../service/variable-class-type.service';
import { VariableClassTypeFormService, VariableClassTypeFormGroup } from './variable-class-type-form.service';
import { VariableClassFormService } from 'app/entities/variable-class/update/variable-class-form.service';

@Component({
  standalone: true,
  selector: 'jhi-variable-class-type-update',
  templateUrl: './variable-class-type-update.component.html',
  imports: [SharedModule, FormsModule, ReactiveFormsModule],
})
export class VariableClassTypeUpdateComponent implements OnInit {
  isSaving = false;
  variableClassType: IVariableClassType | null = null;
  currentTab: string = 'classes'; // default tab
  
  protected variableClassTypeService = inject(VariableClassTypeService);
  protected variableClassTypeFormService = inject(VariableClassTypeFormService);
  protected variableClassFormService = inject(VariableClassFormService);
  protected activatedRoute = inject(ActivatedRoute);

  // eslint-disable-next-line @typescript-eslint/member-ordering
  editForm: VariableClassTypeFormGroup = this.variableClassTypeFormService.createVariableClassTypeFormGroup();

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

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

  save(): void {
    this.isSaving = true;
    const variableClassType = this.variableClassTypeFormService.getVariableClassType(this.editForm);
    if (variableClassType.id !== null) {
      this.subscribeToSaveResponse(this.variableClassTypeService.update(variableClassType));
    } else {
      this.subscribeToSaveResponse(this.variableClassTypeService.create(variableClassType));
    }
  }

  setTab(tabId: string): void {
    this.currentTab = tabId;
  }
  
  /* Nested collection methods : VariableClasse's */
  get variableClasses(): FormArray {
    return this.editForm.get('variableClasses') as FormArray;
  }
  
  addVariableClass(): void {
    this.variableClasses.push(
      this.variableClassFormService.createVariableClassFormGroup()
    );
  }

  removeVariableClass(index: number): void {
    this.variableClasses.removeAt(index);
  }

  protected subscribeToSaveResponse(result: Observable<HttpResponse<IVariableClassType>>): 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(variableClassType: IVariableClassType): void {
    this.variableClassType = variableClassType;
    this.variableClassTypeFormService.resetForm(this.editForm, variableClassType);
	
    // variable.variableClasses FormArray
    variableClassType.variableClasses?.forEach(varClass => {
      this.variableClasses.push(this.variableClassFormService.createVariableClassFormGroup(varClass));
    });
  }
}
