import { Component, AfterViewInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';

import { AppComponent } from 'src/app/app.component';
import { ENCService } from 'src/app/services/enc/enc.service';
import { IAMService } from 'src/app/services/iam/iam.service';

@Component({
  selector: 'app-iam',
  templateUrl: './iam.component.html',
  styleUrls: ['./iam.component.css'],
})
export class IAMComponent implements AfterViewInit {
  formGroup: FormGroup;
  hide = true;
  anio: number = 0;
  btnLoginDisable = false;
  solicitud_datos: string = '';
  solicitud: any = {};
  empresa: string = '';

  public rfc_class: string = 'TIMKEN';

  constructor(
    public router: Router,
    private _IAMService: IAMService,
    private activatedRoute: ActivatedRoute
  ) {
    this.anio = new Date().getFullYear();    
    this.activatedRoute.params.subscribe((params) => {
      this.solicitud_datos =
        params['solicitud_datos'] == undefined
          ? ''
          : params['solicitud_datos'].replace('data=', '');
    });

    /* this.activatedRoute.params.subscribe((params) => {
      this.solicitud_datos = params['solicitud_datos'] == undefined
          ? ''
          : params['solicitud_datos'].replace('data=', '');
    });
    */
    this.empresa = localStorage.getItem('RFCEMPRESA')!;
    this.formGroup = this.createFormGroup();
    this.cargarColores();
  }

  private createFormGroup() {
    return new FormGroup({
      email: new FormControl('', [Validators.required]),
      password: new FormControl('', Validators.required),
    });
  }

  ngAfterViewInit(): void {
    this.cargarColores();
   
    // Checar si ya existe una sesión activa
    let cont = 0;
    setInterval(() => {
      //let rfc_actual = localStorage.getItem('RFCEMPRESA');
      //localStorage.setItem('RFCEMPRESA', 'TME700618RC7');
      if (cont == 0) {
        this.solicitud_datos = this.solicitud_datos;
        cont++;
      }
      let jwt = localStorage.getItem('jwt');
      if (
        (jwt !== undefined || jwt !== null) &&
        !location.href.includes(';data=') &&
        location.href.includes('/iam')
      ) {
        //this.router.navigate(['/mesmpr']);
      }
    }, 1000);
  }

  private cargarColores() {
    let rfc = localStorage.getItem('RFCEMPRESA');
    if (rfc === null || rfc === undefined || rfc === '') {
      this.rfc_class = 'ITTEC';
      this.imagenLogoITTEC();
    } else {
      this.rfc_class = rfc;
    }
  }

  public imagenLogo(rfc_empresa: string) {
    if (window.location.href.includes('ferrariventilatori')) {
      return `https://ferrariventilatori.mx/vacaciones/assets/img/${rfc_empresa}.png`;
    } else {
      return `../../../assets/img/${rfc_empresa}.png`;
    }
  }

  get email() {
    return this.formGroup.get('email');
  }
  get password() {
    return this.formGroup.get('password');
  }

  async login() {
    this.btnLoginDisable = true;
    await this._IAMService.login(this.formGroup.value, this.solicitud_datos);
    this.btnLoginDisable = false;
  }

  enableBtnLogin() {
    this.btnLoginDisable = false;
  }

  imagenLogoITTEC() {
    if (window.location.href.includes('ferrariventilatori')) {
      return 'https://ferrariventilatori.mx/vacaciones/assets/img/ITTEC-FOOTER.png';
    } else {
      return '../assets/img/ITTEC-FOOTER.png';
    }
  }
}
