import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ComponentInformationInput } from '../../../../interfaces/forms-management/form-management.interface';
import { FormControl, Validators } from '@angular/forms';
import { ResourcesService } from '../../../../services/resources.service';
import { EncService } from '../../../../services/enc.service';

@Component({
    selector: 'app-generate-input',
    templateUrl: './generate-input.component.html',
    styleUrl: './generate-input.component.css',
    standalone: false
})
export class GenerateInputComponent implements OnInit {
  @Input() public typeForm: 'R' | 'M';
  @Input() public componentInput: ComponentInformationInput;

  @Output() public dataValue: EventEmitter<FormControl> = new EventEmitter();

  public formControl: FormControl;

  public require: boolean;
  public readonly: boolean;
  public disable: boolean;
  public hasPermission: boolean;

  constructor(
    public resourcesService: ResourcesService,
    private _encService: EncService,
  ) {
    this.componentInput = { } as ComponentInformationInput;
    this.typeForm = 'R';

    this.require = false;
    this.readonly = false;
    this.disable = false;
    this.hasPermission = false;

    this.formControl = new FormControl(null);
  }

  async ngOnInit() {
    const VALIDATION = await this._setValidators();

    this.formControl = new FormControl({
      value: this.componentInput.VALOR_DEFECTO,
      disabled: this.disable,
    }, VALIDATION);
  }

  private async _setValidators() {
    let arrValidatos: any[] = [];

    if (this.componentInput.LONGITUD_CAMPO_MAXIMO !== null && this.componentInput.TIPO_CAMPO === 'text') {
      arrValidatos.push(Validators.maxLength(this.componentInput.LONGITUD_CAMPO_MAXIMO));
    }
    if (this.componentInput.LONGITUD_CAMPO_MINIMO !== null && this.componentInput.TIPO_CAMPO === 'text') {
      arrValidatos.push(Validators.minLength(this.componentInput.LONGITUD_CAMPO_MINIMO));
    }
    
    if (this.componentInput.LONGITUD_CAMPO_MAXIMO !== null && this.componentInput.TIPO_CAMPO === 'number') {
      arrValidatos.push(Validators.max(this.componentInput.LONGITUD_CAMPO_MAXIMO));
    }
    if (this.componentInput.LONGITUD_CAMPO_MINIMO !== null && this.componentInput.TIPO_CAMPO === 'number') {
      arrValidatos.push(Validators.min(this.componentInput.LONGITUD_CAMPO_MINIMO));
    }
    
    if (this.componentInput.PATRON_VALIDACION !== null) {
      arrValidatos.push(Validators.pattern(this.componentInput.PATRON_VALIDACION));
    }

    if (this.typeForm === 'R') {
      if (this.componentInput.DESHABILITAR !== null) {
        this.componentInput.DESHABILITAR.map((element: string) => {
          if (element === 'PARA_REGISTRO') {
            this.disable = true;
          }
        });
      }

      if (this.componentInput.REQUERIDO !== null) {
        this.componentInput.REQUERIDO.map((element: string) => {
          if (element === 'PARA_REGISTRO') {
            arrValidatos.push(Validators.required);
          }
        });
      }

      if (this.componentInput.SOLO_LECTURA !== null) {
        this.componentInput.SOLO_LECTURA.map((element: string) => {
          if (element === 'PARA_REGISTRO') {
            this.readonly = true;
          }
        });
      }
    } else if (this.typeForm === 'M') {
      if (this.componentInput.DESHABILITAR !== null) {
        this.componentInput.DESHABILITAR.map((element: string) => {
          if (element === 'PARA_MODIFICACION') {
            this.disable = true;
          }
        });
      }

      if (this.componentInput.REQUERIDO !== null) {
        this.componentInput.REQUERIDO.map((element: string) => {
          if (element === 'PARA_MODIFICACION') {
            arrValidatos.push(Validators.required);
          }
        });
      }

      if (this.componentInput.SOLO_LECTURA !== null) {
        this.componentInput.SOLO_LECTURA.map((element: string) => {
          if (element === 'PARA_MODIFICACION') {
            this.readonly = true;
          }
        });
      }
    } else {
      this.resourcesService.openSnackBar(`Opción no disponible`);
    }

    const CURRENT_USER: string = await this.resourcesService.getDecryptUser();
    const PERMISOS: string[] = this.componentInput.PERMISOS;
    for (const encUser of PERMISOS) {
      const USER: string = await this._encService.decrypt(encUser);
      if (CURRENT_USER === USER) {
        this.hasPermission = true;
      }
    }    

    if (!this.hasPermission) {
      this.disable = true;
    }

    return arrValidatos;
  }
}
