import { Component, AfterViewInit } from '@angular/core';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Router } from '@angular/router';
import { IAMService } from 'src/app/services/iam/iam.service';
import { MCOService } from 'src/app/services/mco/mco.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { USERInterface } from 'src/app/interfaces/user-interface';
import { ENCService } from 'src/app/services/enc/enc.service';


@Component({
  selector: 'app-mco',
  templateUrl: './mco.component.html',
  styleUrls: ['./mco.component.css']
})
export class MCOComponent implements AfterViewInit {
  usuario_session:USERInterface = JSON.parse(localStorage.getItem('TIMUSERENC')!);
  anio: number;
  isLoggedIn:Boolean = false;
  menuMCO:Boolean = false;
  opened: boolean = false;
  pantallaGrande:Boolean =  !this.breakpointObserver.isMatched('(max-width: 1700px)');

  empresa:string = "";
  razonSocial: string = ""
  constructor( 
    private router: Router, 
    private breakpointObserver:BreakpointObserver, 
    private _IAMService:IAMService,
    private _mcoService:MCOService,
    private _encService: ENCService,
    private _snackBar: MatSnackBar) {
    this.anio = new Date().getFullYear();
    this.isLoggedIn = (localStorage.getItem('jwt') !== "");

    this.empresa = this._encService.desencriptar( this.usuario_session.RFCEMPRESA);
    this.razonSocial = this._encService.desencriptar(this.usuario_session.RAZONSOCIAL);
  }

  ngAfterViewInit(): void {
    this.consultarAnio();
  }

  consultarAnio() {  
    // 1: Enero, 2:Febrero, ......, 12:Diciembre
    let mes_actual = new Date().getMonth() + 1;
    if (mes_actual == 12) {
      this._mcoService.consultarAnio().subscribe((res:any) => {
        if (res.error) {
          const snackRef = this.snackAlert(`Los días feriados para el año ${ new Date().getFullYear() +1 } no han sido configurados`, "Verificar");
          snackRef.onAction().subscribe(result => {
            this.router.navigateByUrl('/admin/mcomdf');
          })
        }
      }, error => {
        if (!error.ok) {
          this.snackAlert('Ocurrió un error inesperado');
        }
        if (error.error.msg != undefined) {
          this.snackAlert(error.error.msg);
        }
        if (error.status == 408) {
          this.snackAlert('Conexion lenta');
        }
      });
    }
  }

  imagenLogo(){
    if (window.location.href.includes("ferrariventilatori")) {
      return `https://ferrariventilatori.mx/vacaciones/assets/img/${this.empresa}-NO-BG.png`
      
    }else{
      return `../assets/img/${this.empresa}-NO-BG.png`
    }
  }

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

  public activateMenu( response:Boolean ){
    this.isLoggedIn = response;
  }

  onResize(event:any) {
    this.pantallaGrande = event.target.innerWidth > 1700
  }
  
  logout() {
    this.isLoggedIn = false;
    this._IAMService.logout();
  }

  private snackAlert(mensaje: string, boton: string  = "Cerrar") {
    return this._snackBar.open(mensaje, boton, {
      duration: 4000,
    });
  } 
}
