import { HttpErrorResponse } from '@angular/common/http';
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatStepper } from '@angular/material/stepper';
import { Router } from '@angular/router';
import { StringMap } from 'quill';
import { lastValueFrom } from 'rxjs';
import { SHOWMULTIFORMComponent } from 'src/app/components/resources/dialogs/test/showmultiform/showmultiform.component';
import { OneMenuResponse } from 'src/app/components/test/plantilla1/plantilla1.component';
import { DataFields, DataForm, FormItems, ResponseDataForm, ResponseInformation } from 'src/app/interfaces/dynamic/dynamic-form.interface';
import { EncService } from 'src/app/services/enc/enc.service';
import { GenericService } from 'src/app/services/generic/generic.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';
import { ResponseData } from '../../form-dyn-management/form-dyn-management.component';

@Component({
  selector: 'app-getbgm-form',
  templateUrl: './module-management-form.component.html',
  styleUrls: ['./module-management-form.component.css'],
})
export class ModuleManagementFormComponent implements OnInit {

  public isLoading: boolean;
  public numberForm: string;
  public arrFields: Array<DataFields>;

  constructor(
    public dialog: MatDialog,
    private _router: Router,
    private _encService: EncService,
    private _dialogRef: MatDialogRef<any>,
    private _genericService: GenericService,
    public resourcesService: ResourcesService,
    @Inject(MAT_DIALOG_DATA) public dialogData: DialogModule
  ) {
    this.isLoading = false;
    this.numberForm = '0000000001';
    this.arrFields = [];
    this.getDynamicForm();
  }

  ngOnInit() {

  }

  private async setDataFormUpdate() {
    this.arrFields = this.arrFields.map((fieldsForm:DataFields) => {
      fieldsForm.form = fieldsForm.form.map((formData:FormItems) => {
        let column:string = formData.column;
        formData.value = this.dialogData.dataElement[column] === null ? '' : this.dialogData.dataElement[column];
        console.log(formData);
        return formData;
      })
      return fieldsForm;
    });
  }

  private async getDynamicForm() {
    this.isLoading = true;
    await lastValueFrom(this._genericService.generateForm(this.numberForm)).then(
      (responseData: ResponseDataForm) => {
        this.arrFields = responseData.response.FIELDS;
        if (this.dialogData.action === 'UPD') {
          this.setDataFormUpdate();
        }
      }, (error:HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
    this.isLoading = false;
  }

  public getInformation(dataInformation: string | Array<ResponseInformation>) {
    if (dataInformation === 'CLOSE') {
      this._dialogRef.close('CLOSE');
    } else if (typeof dataInformation !== 'string' && this.dialogData.action === 'REG'){
      this.registerDynamicForm(dataInformation);
    }
  }

  private async registerDynamicForm(dataValue: Array<ResponseInformation>) {
    await lastValueFrom(this._genericService.registerData({
      CODIGO_FORM: await this._encService.encriptar(this.numberForm),
      DATA_VALUE: JSON.stringify(dataValue),
      USER: this.resourcesService.getUser(),
      LINE: '1',
    })).then((responseData: ResponseData) => {
      this.resourcesService.openSnackBar('Registro Exitoso');
      this._dialogRef.close();
    }, (error: HttpErrorResponse) => this.resourcesService.checkErrors(error))
  }
}

export interface DialogModule {
  title: string;
  action: string;
  dataElement: any;
}
