import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { EncService } from '../../../../services/enc.service';
import { SystemAdminService } from '../../../../services/system-admin.service';
import { UnitDetailsResponse } from '../../../../interfaces/counters-activators.interface';
import { lastValueFrom } from 'rxjs';

@Component({
    selector: 'app-unit-form',
    templateUrl: './unit-form.component.html',
    styleUrl: './unit-form.component.css',
    standalone: false
})
export class UnitFormComponent implements OnInit {
  isLoading: boolean;
  hasError: boolean;
  errorStr: string;
  isNew: boolean;

  magnitudeControl: FormControl;
  unitIDControl: FormControl;
  unitControl: FormControl;
  acronymControl: FormControl;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _encService: EncService,
    private _sysAdminService: SystemAdminService,
  ) {
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = '';
    this.isNew = true;

    this.magnitudeControl = new FormControl({value: '', disabled: true});
    this.unitIDControl = new FormControl({value: '', disabled: true});
    this.unitControl = new FormControl('', [Validators.required, Validators.maxLength(150)]);
    this.acronymControl = new FormControl('', [Validators.required, Validators.maxLength(10)]);
  }

  ngOnInit(): void {
    if(this._data.type == 'nuevo'){
      this.isNew = true;
      this.isLoading = false;

      this.magnitudeControl.setValue(this._data.magnitude);
    }else{
      this.isNew = false;
      this.magnitudeControl.setValue(this._data.magnitude);
      this.getUnit(this._data.idUnit);
    }
  }

  async getUnit(idUnitEnc: string){
    try{
      let idMagnitudeArr = this._data.magnitude.split('(').reverse();
      let idMagnitudeStr = idMagnitudeArr[0].replace(')', '');
      let idMagnitudeEnc = await this._encService.encrypt(idMagnitudeStr);

      let idUser = localStorage.getItem('idusuario')!;
      let unit: UnitDetailsResponse = await lastValueFrom(this._sysAdminService.getUnit(
        idMagnitudeEnc,
        idUnitEnc,
        idUser,
        1
      ));

      this.hasError = unit.error;
      this.errorStr = unit.msg;

      if(!this.hasError){
        let idUnitDec = await this._encService.decrypt(idUnitEnc);
        
        this.unitIDControl.setValue(idUnitDec);
        this.unitControl.setValue(unit.response.NOMBRE_UNIDAD);
        this.acronymControl.setValue(unit.response.ACRONIMO_UNIDAD);
      }

      this.isLoading = false;
    }catch(error: any){
      if(error.error == undefined){
        this.errorStr = 'Ocurrió un error inesperado.';
      }else if(error.error.msg == undefined){
        this.errorStr = 'Ocurrió un error inesperado.';
      }else{
        this.errorStr = error.error.msg;
      }

      this.hasError = true;
      this.isLoading = false;
    }
  }
}
