import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ComponentInformationTextarea } 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-textarea',
    templateUrl: './generate-textarea.component.html',
    styleUrl: './generate-textarea.component.css',
    standalone: false
})
export class GenerateTextareaComponent implements OnInit {
  @Input() public typeForm: 'R' | 'M';
  @Input() public componentTextarea: ComponentInformationTextarea;

  @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.componentTextarea = { } as ComponentInformationTextarea;
    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.componentTextarea.VALOR_DEFECTO,
      disabled: this.disable,
    }, VALIDATION);
  }

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

    if (this.componentTextarea.LONGITUD_CAMPO_MAXIMO !== null) {
      arrValidatos.push(Validators.maxLength(this.componentTextarea.LONGITUD_CAMPO_MAXIMO));
    }
    if (this.componentTextarea.LONGITUD_CAMPO_MINIMO !== null) {
      arrValidatos.push(Validators.minLength(this.componentTextarea.LONGITUD_CAMPO_MINIMO));
    }
    
    if (this.componentTextarea.LONGITUD_CAMPO_MAXIMO !== null) {
      arrValidatos.push(Validators.max(this.componentTextarea.LONGITUD_CAMPO_MAXIMO));
    }
    if (this.componentTextarea.LONGITUD_CAMPO_MINIMO !== null) {
      arrValidatos.push(Validators.min(this.componentTextarea.LONGITUD_CAMPO_MINIMO));
    }
    
    if (this.componentTextarea.PATRON_VALIDACION !== null) {
      arrValidatos.push(Validators.pattern(this.componentTextarea.PATRON_VALIDACION));
    }

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

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

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

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

      if (this.componentTextarea.SOLO_LECTURA !== null) {
        this.componentTextarea.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.componentTextarea.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;
  }
}
