import { Component, Inject, OnInit } from '@angular/core';
import { EmployeeInfo, ResponseDataEmployee } from '../../../../interfaces/personal-managment/employee.interface';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { EncService } from '../../../../services/enc.service';
import { EmployeeService } from '../../../../services/personal-management/employee.service';
import { FunctionsService } from '../../../../services/functions.service';
import { lastValueFrom } from 'rxjs';

@Component({
    selector: 'app-employee-details-dialog',
    templateUrl: './employee-details-dialog.component.html',
    styleUrl: './employee-details-dialog.component.css',
    standalone: false
})
export class EmployeeDetailsDialogComponent implements OnInit {
  idEmployee: string;
  employeeName: string;

  isLoading: boolean;
  hasError: boolean;
  errorStr: string;
  employee: EmployeeInfo | null;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _encService: EncService,
    private _employeeService: EmployeeService,
    private _functionsService: FunctionsService,
  ){
    this.idEmployee = "";
    this.employeeName = "";

    this.isLoading = true;
    this.hasError = false;
    this.errorStr = "";
    this.employee = null;
  }

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

  async init(){
    let employeeName = await this._encService.decrypt(this._data.employeeName);
    this.employeeName = employeeName;

    this.getEmployee(this._data.idEmployee);
  }

  async getEmployee(idEmployee: string){
    try{
      let idUser = localStorage.getItem('idusuario')!;
      const line: number = 1;
      let employee: ResponseDataEmployee = await lastValueFrom(this._employeeService.getEmployeeById(idEmployee, idUser, line));
      
      this.hasError = employee.error;
      this.errorStr = employee.msg;

      if(!this.hasError){
        this.employee = employee.response;
        this.employee.USER_ID = await this._encService.decrypt(this.employee.USER_ID);

        if(this.employee.WORKTEAM_ID != null){
          this.employee.WORKTEAM_ID = await this._encService.decrypt(this.employee.WORKTEAM_ID);
        }

        if(this.employee.RFC != null){
          this.employee.RFC = await this._encService.decrypt(this.employee.RFC);
        }

        if(this.employee.ZIP_CODE != null){
          this.employee.ZIP_CODE = await this._encService.decrypt(this.employee.ZIP_CODE);
        }

        if(this.employee.CONTACT_TELEPHONE != null){
          this.employee.CONTACT_TELEPHONE = await this._encService.decrypt(this.employee.CONTACT_TELEPHONE);
        }
        
        this.employee.FECREG = this.buildDate(this.employee.FECREG);

        if(this.employee.FECMOD != null){
          this.employee.FECMOD = this.buildDate(this.employee.FECMOD);
        }

        let specialtiesArr: string[] = JSON.parse(this.employee.SPECIALITY);
        let specialtiesStr = "";
        for(const specialty of specialtiesArr){
          let specialtyDec = await this._encService.decrypt(specialty);
          specialtiesStr += `${specialtyDec}, `;
        }

        specialtiesStr = specialtiesStr.substring(0, specialtiesStr.length - 2);

        this.employee.SPECIALITY = specialtiesStr;
        this.employee.SPECIALITY_ARR = specialtiesArr;
      }

      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;
    }
  }

  buildDate(date: string): string{
    return this._functionsService.orderDate(date)!;
  }
}
