import { Component, HostListener, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MESMCCService } from '../../../services/mes/mesmcc/mesmcc.service';
import { AlertaComponent } from '../../resources/dialogos/alerta/alerta.component';
import { USERInterface } from '../../../interfaces/user-interface';
import { IAMService } from '../../../services/iam/iam.service';
import { ENCService } from 'src/app/services/enc/enc.service';
import { MESMCCInterface } from 'src/app/interfaces/mes/mesmcc/mesmcc-interface';
@Component({
  selector: 'app-mesmcc',
  templateUrl: './mesmcc.component.html',
  styleUrls: ['./mesmcc.component.css'],
})
export class MESMCCComponent implements OnInit {
  formGroup: FormGroup;
  hideContrasenaPrevia = true;
  hideNuevaContrasena = true;
  hideConfirmarContrasena = true;
  usuario_session: USERInterface = JSON.parse(
    localStorage.getItem('TIMUSERENC')!
  );
  contadorIntentos: number = 0;

  constructor(
    private _mesmccService: MESMCCService,
    public dialog: MatDialog,
    private _encService: ENCService
  ) {
    this.formGroup = this.createFormGroup();
  }

  ngOnInit(): void {}

  private createFormGroup() {
    return new FormGroup({
      numero_empleado: new FormControl(
        this._encService.desencriptar(this.usuario_session.IDUSUARIO),
        Validators.required
      ),
      prev_password: new FormControl('', [
        Validators.required,
        Validators.min(8),
      ]),
      new_password: new FormControl('', [
        Validators.required,
        Validators.min(8),
      ]),
      new_password_confirmation: new FormControl('', [
        Validators.required,
        Validators.min(8),
      ]),
    });
  }

  modificarContrasena(item: MESMCCInterface) {
    this._mesmccService.actualizarContrasena(item);
  }

  generatePassword() {
    let chars =
      '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let passwordLength = 12;
    let password = '';
    for (let i = 0; i <= passwordLength; i++) {
      let randomNumber = Math.floor(Math.random() * chars.length);
      password += chars.substring(randomNumber, randomNumber + 1);
    }

    this.new_password?.setValue(password);
    this.new_password_confirmation?.setValue(password);
  }

  openDialogForm(item: any = {}) {
    let dataAction = {
      titulo: 'Modificar contraseña',
      descripcion: '¿Realmente desea cambiar la contraseña?',
    };

    const dialogRef = this.dialog.open(AlertaComponent, {
      data: dataAction,
    });

    dialogRef.afterClosed().subscribe((result) => {
      if (result) {
        this.modificarContrasena(item);
      }
    });
  }

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

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

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

  
}
