import { MaintenanceStatusResponse } from './../../../interfaces/maintenance-status.interface';
import { SystemAdminService } from './../../../services/system-admin.service';
import { LoginPlace } from './../../../interfaces/user-connections.interface';
import { GeolocationResponse, LocalityInfo } from './../../../interfaces/location-response.interface';
import { EncService } from './../../../services/enc/enc.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { LoginResponse } from './../../../interfaces/login.interface';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { take, lastValueFrom } from 'rxjs';
import { LoginDataService } from 'src/app/services/login/login-data.service';
import { LoginService } from 'src/app/services/login/login.service';
import { ValidationsService } from 'src/app/services/validations.service';
import { GeolocationService } from '@ng-web-apis/geolocation';
import { GeolocationService as Gservice } from 'src/app/services/geolocation.service';
import { Permissions } from 'src/app/interfaces/permissions.interface';

@Component({
  selector: 'app-login-old',
  templateUrl: './login-old.component.html',
  styleUrls: ['./login-old.component.css']
})
export class LoginOldComponent implements OnInit {
  formGroup: FormGroup;
  hide = true;

  constructor(
    private router: Router,
    private _serviceLogin: LoginService,
    private _validations: ValidationsService,
    private _loginDataService: LoginDataService,
    private _snackBar: MatSnackBar,
    private readonly geolocation$: GeolocationService,
    private _encService: EncService,
    private _geolocationService: Gservice,
    private _systemAdminService: SystemAdminService,
  ) {
    this.formGroup = this.createFormGroup();
  }

  openSnackBar(message: string){
    this._snackBar.open(message, "Cerrar", {
      duration: 5000
    })
  }

  ngOnInit(): void {
  }

  onResize(event: any) {}

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

  procesarDatos(): any {
    let datos: any = {
      email: this.email?.value,
      password: this.password?.value,
    };
    return datos;
  }

  login() {
    if(!this.email?.valid){
      this.openSnackBar("El correo electrónico es un campo obligatorio.");
      return;
    }

    if(!this.password?.valid){
      this.openSnackBar("La contraseña es un campo obligatorio.");
      return;
    }

    this.geolocation$.pipe(take(1)).subscribe(position => {
      let userPosition: LoginPlace = {
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        accuracy: position.coords.accuracy
      };

      this.requestLogin(userPosition);
    });
  }

  async requestLogin(position: LoginPlace){
    try{
      let pwdEnc = await this._encService.encriptar(this.password?.value);
      let loginPlace: GeolocationResponse | boolean = await this._getPlace(position);

      if(!loginPlace){
        this.openSnackBar('No se pudo obtener la ubicación del usuario, por favor intente más tarde.');
      }else{
        let location: GeolocationResponse = loginPlace as GeolocationResponse;

        let lati = position.latitude;
        let long = position.longitude;
        let prec = position.accuracy;

        let ciud = location.locality;
        let esta = location.city;
        let pais = location.countryName;

        let loginResponse: LoginResponse = await lastValueFrom(this._serviceLogin.login({
          email: this.email?.value,
          password: pwdEnc,
          linea: 1,
          latitude: lati,
          longitude: long,
          accuracy: prec,
          city: ciud,
          state: esta,
          country: pais,
        }));


        if (loginResponse.error == false) {
          let shortEnc = await lastValueFrom(this._encService.shortEncrypt(loginResponse.response.IDUSUARIO));
          let maintenanceStatus: MaintenanceStatusResponse = await lastValueFrom(this._systemAdminService.getMaintenanceStatus(
            shortEnc.response.encrypted, 1, loginResponse.response.TOKEN
          ));

          if(maintenanceStatus.response.activated){
            let permisosDec = await this._encService.desencriptar(loginResponse.response.PERMISOS);
            let permisosArr: Permissions = JSON.parse(permisosDec);

            let systemAdmin = permisosArr.permissions.filter(mod => mod.id == 'S002V01M01ADSI')[0];
            if(systemAdmin.access != 0){
              let maintenanceMode = systemAdmin.children.filter(func => func.id == 'S002V01F05MOMA')[0];

              if(maintenanceMode.access != 0){
                let activateMaintenance = maintenanceMode.children.filter(scr => scr.id == 'S002V01P01AMMA')[0];

                if(activateMaintenance.access == 2){
                  let maintenance = await this._encService.encriptar(JSON.stringify({
                    idUser: loginResponse.response.IDUSUARIO,
                    maintenance: 'enabled',
                  }));

                  this._loginDataService.saveLoginInformation(
                    loginResponse.response.TOKEN,
                    loginResponse.response.IDUSUARIO,
                    loginResponse.response.NOMBREUSUARIO,
                    loginResponse.response.CORREO,
                    loginResponse.response.PERFIL,
                    loginResponse.response.PERMISOS
                  );

                  this.router.navigate(['sam/home'], {
                    queryParams: {
                      data: maintenance
                    }
                  });
                }else{
                  this.openSnackBar("Lo sentimos, en este momento el sistema se encuentra en modo mantenimiento.");
                }
              }else{
                this.openSnackBar("Lo sentimos, en este momento el sistema se encuentra en modo mantenimiento.");
              }
            }else{
              this.openSnackBar("Lo sentimos, en este momento el sistema se encuentra en modo mantenimiento.");
            }
          }else{
            this._loginDataService.saveLoginInformation(
              loginResponse.response.TOKEN,
              loginResponse.response.IDUSUARIO,
              loginResponse.response.NOMBREUSUARIO,
              loginResponse.response.CORREO,
              loginResponse.response.PERFIL,
              loginResponse.response.PERMISOS
            );

            this.router.navigate(['sam/home']);
          }
        }
      }

    }catch(error: any){
      let msgError = "";

      if(error.error !== undefined){
        msgError = error.error.msg;
      }else{
        msgError = "Ocurrió un error inesperado.";
      }

      this.openSnackBar(msgError);
    }
  }

  async _getPlace(coords: LoginPlace): Promise<GeolocationResponse | boolean>{
    try{
      let location: GeolocationResponse = await lastValueFrom(this._geolocationService.getLocationName(coords));

      return location;
    }catch(error: any){
      let msgError = "";

      if(error.error !== undefined){
        msgError = error.error.msg;
      }else{
        msgError = "Ocurrió un error inesperado.";
      }

      this.openSnackBar(msgError);

      return false;
    }
  }

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

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