import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialog } from '@angular/material/dialog';
import { EncService } from '../../../../services/enc.service';
import { EquipmentManagementService } from '../../../../services/equipment-management.service';
import { lastValueFrom } from 'rxjs';
import { EquipmentsListItem, EquipmentsListResponse } from '../../../../interfaces/equipment-management.interface';
import { EquipmentDetailsDialogComponent } from '../../new-equipment/equipment-details-dialog/equipment-details-dialog.component';

@Component({
    selector: 'app-station-location',
    templateUrl: './station-location.component.html',
    styleUrl: './station-location.component.css',
    standalone: false
})
export class StationLocationComponent implements OnInit {
  isLoading: boolean;
  hasError: boolean;
  errorStr: string;
  station: string;
  level: string;
  coords: string;

  equipments: EquipmentsListItem[];

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _encService: EncService,
    private _equipmentManagementService: EquipmentManagementService,
    private _dialog: MatDialog,
  ) {
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = '';
    this.station = '';
    this.level = '';
    this.coords = '';

    this.equipments = [];
  }

  ngOnInit(): void {
    this.init();
  }

  private async init(){
    this.station = this._data.station;
    this.level = this._data.level == 'A' ? 'andén' : 'vestíbulo';
    this.coords = await this._encService.decrypt(this._data.coords);

    this.consultStationEquipments();
  }

  private async consultStationEquipments(){
    try{
      let idUser = localStorage.getItem('idusuario')!;
      let equipments: EquipmentsListResponse = await lastValueFrom(this._equipmentManagementService.equipmentConsultStation(
        this._data.plane,
        this._data.coords,
        idUser,
        1,
      ));

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

      if(!this.hasError){
        let equipmentsArr: EquipmentsListItem[] = [];
        for(const equipment of equipments.response){
          let equipmentCodeDec = await this._encService.decrypt(equipment.CODIGO);
          equipment.TIPO = `${equipmentCodeDec} - ${equipment.TIPO}`;

          let equipmentIDDec = await this._encService.decrypt(equipment.ID_EQUIPO);
          equipment.MODELO = `${equipment.MODELO} (${equipmentIDDec})`;

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

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

  openEquipmentDetails(equipmentCode: string){
    this._dialog.open(EquipmentDetailsDialogComponent, {
      width: '920px',
      maxWidth: '920px',
      data: {
        code: equipmentCode,
      }
    });
  }
}
