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

@Component({
    selector: 'app-generate-radio',
    templateUrl: './generate-radio.component.html',
    styleUrl: './generate-radio.component.css',
    standalone: false
})
export class GenerateRadioComponent implements OnInit {
  public formControl: FormControl;
  @Input() public typeForm: 'R' | 'M';
  @Input() public componentRadio: ComponentInformationRadio;

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

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

  constructor ( 
    public resourcesService: ResourcesService,
    private _encService: EncService,
  ) {
    this.formControl = new FormControl(null);
    this.typeForm = 'R';
    this.componentRadio = {} as ComponentInformationRadio;

    this.disable = false;
    this.require = false;
    this.hasPermission = false;
  }
  
  async ngOnInit() {

    const VALIDATION = await this._setValidators();
    this.formControl = new FormControl({
      value: null,
      disabled: this.disable,
    }, VALIDATION);

    this.formControl.valueChanges.subscribe((value) => {
      this.dataValue.emit(value);
    });
    
  }

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

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

      if (this.componentRadio.REQUERIDO !== null) {
        this.componentRadio.REQUERIDO.map((element: string) => {
          if (element === 'PARA_REGISTRO') {
            arrValidatos.push(Validators.required);
            this.require = true;
          }
        });
      }
    } else if (this.typeForm === 'M') {
      if (this.componentRadio.DESHABILITAR !== null) {
        this.componentRadio.DESHABILITAR.map((element: string) => {
          if (element === 'PARA_MODIFICACION') {
            this.disable = true;
          }
        });
      }

      if (this.componentRadio.REQUERIDO !== null) {
        this.componentRadio.REQUERIDO.map((element: string) => {
          if (element === 'PARA_MODIFICACION') {
            arrValidatos.push(Validators.required);
            this.require = true;
          }
        });
      }
    } else {
      this.resourcesService.openSnackBar(`Opción no disponible`);
    }

    const CURRENT_USER: string = await this.resourcesService.getDecryptUser();
    const PERMISOS: string[] = this.componentRadio.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;
  }
}
