import { Component, Inject, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { InformationsData, ResponseDataInformationProvider } from 'src/app/interfaces/acquisition-management/provider.interface';
import { FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { ResourcesService } from 'src/app/services/resources/resources.service';
import { ProviderService } from 'src/app/services/acquisition-management/provider.service';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
  selector: 'app-information-provider',
  templateUrl: './information-provider.component.html',
  styleUrls: ['./information-provider.component.css']
})
export class InformationProviderComponent implements OnInit {

  public formGroupInformation: FormGroup;
  private providerInformation: InformationsData;

  public isLoading: boolean;
  constructor(
    @Inject(MAT_DIALOG_DATA) public providerNumber: InformationsData | string,
    private _providerService: ProviderService,
    public resourcesService: ResourcesService,
  ) {
    
    this.formGroupInformation = this.createFormGroupInformation();
    this.providerInformation = {} as InformationsData;
    this.isLoading = false;
  }

  private setDataValue() {
    if (typeof this.providerNumber !== 'string') {
      this.formGroupInformation.controls['NUMERO_PROVEEDOR'].setValue(this.providerNumber.PROV_NUPR)
      this.formGroupInformation.controls['RAZON_SOCIAL'].setValue(this.providerNumber.PROV_NOCO)
      this.formGroupInformation.controls['CORREO'].setValue(this.providerNumber.PROV_CORR)
      this.formGroupInformation.controls['NOMBRE'].setValue(this.providerNumber.PROV_NOMB)
      this.formGroupInformation.controls['PRIMER_APELLIDO'].setValue(this.providerNumber.PROV_APPA)
      this.formGroupInformation.controls['SEGUNDO_APELLIDO'].setValue(this.providerNumber.PROV_APMA)
      this.formGroupInformation.controls['LADA1'].setValue(this.providerNumber.PROV_LAD1)
      this.formGroupInformation.controls['TELEFONO1'].setValue(this.providerNumber.PROV_TEL1)
      this.formGroupInformation.controls['LADA2'].setValue(this.providerNumber.PROV_LAD2)
      this.formGroupInformation.controls['TELEFONO2'].setValue(this.providerNumber.PROV_TEL2)
      this.formGroupInformation.controls['REGIMEN_FISCAL'].setValue(`(${this.providerNumber.REFI_CRFI}) ${this.providerNumber.REFI_DRFI}`)
      this.formGroupInformation.controls['TIPO'].setValue(this.providerNumber.PROV_TIPO)
      this.formGroupInformation.controls['TIPO_CONTRIBUYENTE'].setValue(this.providerNumber.PROV_TICO)
      this.formGroupInformation.controls['RFC'].setValue(this.providerNumber.PROV_XRFC)
      this.formGroupInformation.controls['TAXID'].setValue(this.providerNumber.PROV_XTAX)
      this.formGroupInformation.controls['SITIOWEB'].setValue(this.providerNumber.PROV_SIWE)
      this.formGroupInformation.controls['ESTADO'].setValue(this.providerNumber.PROV_ESTA)
      this.formGroupInformation.controls['CALLE'].setValue(this.providerNumber.PROV_CALL)
      this.formGroupInformation.controls['EXTERIOR'].setValue(this.providerNumber.PROV_NUEX)
      this.formGroupInformation.controls['INTERIOR'].setValue(this.providerNumber.PROV_NUIN)
      this.formGroupInformation.controls['CODIGO_POSTAL'].setValue(this.providerNumber.PROV_COPO)
      this.formGroupInformation.controls['LOCALIDAD'].setValue(this.providerNumber.PROV_LOCA)
      this.formGroupInformation.controls['COLONIA'].setValue(this.providerNumber.PROV_COLO)
      this.formGroupInformation.controls['MUNICIPIO'].setValue(this.providerNumber.PROV_MUNI)
      this.formGroupInformation.controls['ENTIDAD'].setValue(this.providerNumber.PROV_ENTI)
      this.formGroupInformation.controls['PAIS'].setValue(this.providerNumber.PROV_PAIS)

    }
  }

  ngOnInit(): void {
    if (typeof this.providerNumber === 'string') {
      this.getProviderByNumber();
    } else {
      this.setDataValue();
    }
  }

  private createFormGroupInformation(): FormGroup {
    return new FormGroup({
      NUMERO_PROVEEDOR: new FormControl(''),
      RAZON_SOCIAL: new FormControl(''),
      CORREO: new FormControl(''),
      NOMBRE: new FormControl(''),
      PRIMER_APELLIDO: new FormControl(''),
      SEGUNDO_APELLIDO: new FormControl(''),
      LADA1: new FormControl(''),
      TELEFONO1: new FormControl(''),
      LADA2: new FormControl(''),
      TELEFONO2: new FormControl(''),
      REGIMEN_FISCAL: new FormControl(''),
      TIPO: new FormControl(''),
      TIPO_CONTRIBUYENTE: new FormControl(''),
      RFC: new FormControl(''),
      TAXID: new FormControl(''),
      SITIOWEB: new FormControl(''),
      ESTADO: new FormControl(''),
      CALLE: new FormControl(''),
      EXTERIOR: new FormControl(''),
      INTERIOR: new FormControl(''),
      CODIGO_POSTAL: new FormControl(''),
      LOCALIDAD: new FormControl(''),
      COLONIA: new FormControl(''),
      MUNICIPIO: new FormControl(''),
      ENTIDAD: new FormControl(''),
      PAIS: new FormControl(''),
    })
  }

  private async getProviderByNumber() {
    if (typeof this.providerNumber === 'string') {
      this.isLoading = true;
      await lastValueFrom(this._providerService.getProviderByNumber(this.providerNumber)).then(
        (responseData: ResponseDataInformationProvider) => {
          if ( !responseData.error ) {
            this.providerInformation = responseData.response;


            this.formGroupInformation.controls['NUMERO_PROVEEDOR'].setValue(this.providerInformation.PROV_NUPR);
            this.formGroupInformation.controls['RAZON_SOCIAL'].setValue(this.providerInformation.PROV_NOCO);
            this.formGroupInformation.controls['CORREO'].setValue(this.providerInformation.PROV_CORR);
            this.formGroupInformation.controls['NOMBRE'].setValue(this.providerInformation.PROV_NOMB);
            this.formGroupInformation.controls['PRIMER_APELLIDO'].setValue(this.providerInformation.PROV_APPA);
            this.formGroupInformation.controls['SEGUNDO_APELLIDO'].setValue(this.providerInformation.PROV_APMA);
            this.formGroupInformation.controls['LADA1'].setValue(this.providerInformation.PROV_LAD1);
            this.formGroupInformation.controls['TELEFONO1'].setValue(this.providerInformation.PROV_TEL1);
            this.formGroupInformation.controls['LADA2'].setValue(this.providerInformation.PROV_LAD2);
            this.formGroupInformation.controls['TELEFONO2'].setValue(this.providerInformation.PROV_TEL2);
            this.formGroupInformation.controls['REGIMEN_FISCAL'].setValue(`(${this.providerInformation.REFI_CRFI}) ${this.providerInformation.REFI_DRFI}`);
            this.formGroupInformation.controls['TIPO'].setValue(this.providerInformation.PROV_TIPO);
            this.formGroupInformation.controls['TIPO_CONTRIBUYENTE'].setValue(this.providerInformation.PROV_TICO);
            this.formGroupInformation.controls['RFC'].setValue(this.providerInformation.PROV_XRFC);
            this.formGroupInformation.controls['TAXID'].setValue(this.providerInformation.PROV_XTAX);
            this.formGroupInformation.controls['SITIOWEB'].setValue(this.providerInformation.PROV_SIWE);
            this.formGroupInformation.controls['ESTADO'].setValue(this.providerInformation.PROV_ESTA);
            this.formGroupInformation.controls['CALLE'].setValue(this.providerInformation.PROV_CALL);
            this.formGroupInformation.controls['EXTERIOR'].setValue(this.providerInformation.PROV_NUEX);
            this.formGroupInformation.controls['INTERIOR'].setValue(this.providerInformation.PROV_NUIN);
            this.formGroupInformation.controls['CODIGO_POSTAL'].setValue(this.providerInformation.PROV_COPO);
            this.formGroupInformation.controls['LOCALIDAD'].setValue(this.providerInformation.PROV_LOCA);
            this.formGroupInformation.controls['COLONIA'].setValue(this.providerInformation.PROV_COLO);
            this.formGroupInformation.controls['MUNICIPIO'].setValue(this.providerInformation.PROV_MUNI);
            this.formGroupInformation.controls['ENTIDAD'].setValue(this.providerInformation.PROV_ENTI);
            this.formGroupInformation.controls['PAIS'].setValue(this.providerInformation.PROV_PAIS);

          } else {
            this.resourcesService.openSnackBar(responseData.msg);
          }
        }, (error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
      );
      this.isLoading = false;
    }
  }

}
