import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { GetNomenclature, ResponseDataDetailsNomenclature } from '../../../../interfaces/inventory-management/nomenclature-management.interface';
import { ResourcesService } from '../../../../services/resources.service';
import { InventoryManagementService } from '../../../../services/inventory-management/inventory-management.service';
import { EncService } from '../../../../services/enc.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
    selector: 'app-details-nomenclature',
    templateUrl: './details-nomenclature.component.html',
    styleUrl: './details-nomenclature.component.css',
    standalone: false
})
export class DetailsNomenclatureComponent implements OnInit {
  public formGroup: FormGroup;
  public isLoading: boolean;
  
  constructor (
    @Inject(MAT_DIALOG_DATA) public nomenclature: GetNomenclature,
    public resourcesService: ResourcesService,
    private _inventoryManagementService: InventoryManagementService,
    private _encService: EncService,
  ) {
    this.formGroup = this._createFormGroup();
    this.isLoading = false;
  }

  private _createFormGroup(): FormGroup {
    return new FormGroup({
      'CLAVE_SITIO': new FormControl(null),
      'SITIO': new FormControl(null),
      'CLAVE_UBICACION': new FormControl(null),
      'UBICACION': new FormControl(null),
      'CLAVE_POSICION': new FormControl(null),
      'POSICION': new FormControl(null),
      'CLAVE_OCUPACION': new FormControl(null),
      'OCUPACION': new FormControl(null),
      'CLAVE_ELEMENTO': new FormControl(null),
      'ELEMENTO': new FormControl(null),
      'CLAVE_FAMILIA': new FormControl(null),
      'FAMILIA': new FormControl(null),
      'CLAVE_SUBFAMILIA': new FormControl(null),
      'SUBFAMILIA': new FormControl(null),
      'CLAVE_ESTADO': new FormControl(null),
      'ESTADO': new FormControl(null),
      'CLAVE_TIPO': new FormControl(null),
      'TIPO': new FormControl(null),
      'CLAVE_MODELO': new FormControl(null),
      'MODELO': new FormControl(null),
    });
  }

  async ngOnInit() {
    this.isLoading = true;
    await this.getDetailsNomenclature();
    this.isLoading = false;
  }

  private async getDetailsNomenclature() {
    const encStock = await this._encService.encrypt(this.nomenclature.ID_STOCK);
    const user: string = this.resourcesService.getUser();
    const line: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._inventoryManagementService.getDetailsNomenclature(encStock, user, line)).then(
      (responseData: ResponseDataDetailsNomenclature) => {
        if (!responseData.error) {
          this.formGroup.setValue(responseData.response);
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.getDetailsNomenclature();
        }
      }
    );
  }
}
