import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { UnprogrammedVisitDetails, UnprogrammedVisitResponse } from 'src/app/interfaces/unprogrammed-visit.interface';
import { EncService } from 'src/app/services/enc/enc.service';
import { FunctionsService } from 'src/app/services/functions.service';
import { PreventiveMaintenanceService } from 'src/app/services/preventive-maintenance.service';

@Component({
  selector: 'app-visit-details',
  templateUrl: './visit-details.component.html',
  styleUrls: ['./visit-details.component.css']
})
export class VisitDetailsComponent implements OnInit {
  isLoading: boolean;
  hasError: boolean;
  errorStr: string;
  idVisit: number;
  vistDetails: UnprogrammedVisitDetails | null;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _encService: EncService,
    private _prevMaintService: PreventiveMaintenanceService,
    private _functionsService: FunctionsService,
  ) { 
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = '';
    this.idVisit = 0;
    this.vistDetails = null;
  }

  ngOnInit(): void {
    this.idVisit = this._data.idVisit;
    this.getVisitInfo();
  }

  async getVisitInfo(){
    try{
      let idUser = localStorage.getItem('idusuario');
      let shortEnc = await lastValueFrom(this._encService.shortEncrypt(idUser!));
      let idVisitEnc = await this._encService.encriptar(`${this.idVisit}`);
      let idVisitShort = await lastValueFrom(this._encService.shortEncrypt(idVisitEnc));
      let visit: UnprogrammedVisitResponse = await lastValueFrom(this._prevMaintService.getUnprogrammedVisit(
        idVisitShort.response.encrypted,
        shortEnc.response.encrypted,
        1
      ));

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

      if(!this.hasError){
        this.vistDetails = visit.response;
      }

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

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

  getPersonal(personalStr: string): string{
    let personalArr = JSON.parse(personalStr);
    let personal = "";

    personalArr.forEach((element: string) => {
      personal += `${element}, `;
    });

    personal = personal.substring(0, personal.length - 2);
    return personal;
  }

  getTools(materialStr: string): string{
    return "Desarrollo pendiente";
  }

  getResources(materialStr: string): string{
    return "Desarrollo pendiente";
  }

  getStatus(statusStr: string): string{
    if(statusStr == 'P'){
      return 'Pendiente de autorización';
    }else if(statusStr == 'A'){
      return 'Autorizado';
    }else if(statusStr == 'F'){
      return 'Finalizado';
    }else if(statusStr == 'R'){
      return 'Rechazado';
    }else if(statusStr == 'C'){
      return 'Cancelado';
    }
    return "";
  }

  orderDate(dateStr: string): string{
    return this._functionsService.orderDate(dateStr);
  }
  
  getComments(commentsStr: string, index: string): string{
    let commentsArr = JSON.parse(commentsStr);
    if(commentsArr[index] != undefined){
      return commentsArr[index];
    }
    return "";
  }
}
