import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ConditionData, DataDialogCondition } from '../../../../../../interfaces/acquisition-management/provider.interface';

@Component({
    selector: 'app-condition-provider-form',
    templateUrl: './condition-provider-form.component.html',
    styleUrl: './condition-provider-form.component.css',
    standalone: false
})
export class ConditionProviderFormComponent implements OnInit {
  public formGroup: FormGroup;
  public titleDialog: string;
  public isLoading: boolean;
  public isLoadingForm: boolean;

  public arrCondition: string[];
  
  constructor(
    @Inject(MAT_DIALOG_DATA) public dataDialog: DataDialogCondition,
    private _dialogRef: MatDialogRef<ConditionData>
  ) {
    
    if (dataDialog.typeDialog === 'REGISTER') {
      this.titleDialog = 'Registro de Condición';
    } else if (dataDialog.typeDialog === 'UPDATE') {
      this.titleDialog = 'Modificación de Condición';
    }else{
      this.titleDialog = 'Condición';
    }
    this.isLoading = false;
    this.isLoadingForm = false;
    this.formGroup = this.createFromGroup();
    this.arrCondition = [
      'Condiciones de Pago',
      'Condiciones de Entrega',
      'Condiciones de Compra',
    ];
  }

  private createFromGroup(): FormGroup {
    return new FormGroup({
      ID_CONDICION: new FormControl(this.dataDialog.dataSource.length + 1, Validators.required),
      TIPO_CONDICION: new FormControl('', Validators.required),
      INFORMACION: new FormControl('', Validators.required),
      ESTADO: new FormControl('Nuevo', Validators.required),
    });
  }

  ngOnInit(): void {
    this.setDataForm();
  }

  private setDataForm() {
    if (this.dataDialog.typeDialog === 'UPDATE' || this.dataDialog.typeDialog === 'ONLYREAD') {
      this.isLoading = true;
      this.formGroup.controls['ID_CONDICION'].setValue(this.dataDialog.dataElement.ID_CONDICION);
      this.formGroup.controls['TIPO_CONDICION'].setValue(this.dataDialog.dataElement.TIPO_CONDICION);
      this.formGroup.controls['INFORMACION'].setValue(this.dataDialog.dataElement.INFORMACION);
      if (this.dataDialog.dataElement.ESTADO !== 'Nuevo') {
        this.formGroup.controls['ESTADO'].setValue('Modificado');
      }
      this.isLoading = false;
    }

    let arrSelectedCondition: Array<string | null> = this.dataDialog.dataSource.map((element) => {
      if (element.ESTADO !== 'Eliminado') {
        return element.TIPO_CONDICION
      }
      return null;
    });
    arrSelectedCondition = arrSelectedCondition.filter((element) => element !== null);

    let conditions: string[] = [];
    for (const element of this.arrCondition) {
      if (!arrSelectedCondition.includes(element)) {
        conditions.push(element)
      }
    }
    this.arrCondition = conditions;
  }

  public registerCondition() {
    this.isLoadingForm = true;
    let arrDataSource: Array<ConditionData> = this.dataDialog.dataSource;
    arrDataSource.push(this.formGroup.value);
    this._dialogRef.close(arrDataSource);
    this.isLoadingForm = false;
  }
  
  public updateCondition() {
    this.isLoadingForm = true;  
    let arrDataSource: Array<ConditionData> = this.dataDialog.dataSource;
    arrDataSource = arrDataSource.map((element: ConditionData) => {
      if (element.ID_CONDICION === this.formGroup.controls['ID_CONDICION'].value) {
        element = this.formGroup.value;
      }
      return element;
    });
    this._dialogRef.close(arrDataSource);
    this.isLoadingForm = false;
  }
}
