import { HttpErrorResponse } from '@angular/common/http';
import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { Observable, lastValueFrom, map, startWith } from 'rxjs';
import { AlertComponent } from 'src/app/components/resources/dialogs/alert/alert.component';
import { DispatchData, ResponseDataDispatch } from 'src/app/interfaces/acquisition-management/dispatch.interface';
import { RequestLines, RequestOrder } from 'src/app/interfaces/acquisition-management/order.interface';
import { CorrectiveWorkOrdersListItem, CorrectiveWorkOrdersListResponse } from 'src/app/interfaces/corrective-maintenance.interface';
import { PreventiveWorkOrdersActive, PreventiveWorkOrdersActiveResponse } from 'src/app/interfaces/preventive-work-orders-active.interface';
import { ResponseData } from 'src/app/interfaces/response-data';
import { DispatchService } from 'src/app/services/acquisition-management/dispatch.service';
import { OrderService } from 'src/app/services/acquisition-management/order.service';
import { CorrectiveMaintenanceService } from 'src/app/services/corrective-maintenance.service';
import { EncService } from 'src/app/services/enc/enc.service';
import { PreventiveMaintenanceService } from 'src/app/services/preventive-maintenance.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';

@Component({
  selector: 'app-form-purchase-line',
  templateUrl: './form-purchase-line.component.html',
  styleUrls: ['./form-purchase-line.component.css']
})
export class FormPurchaseLineComponent implements OnInit {
  public arrPreventiveMaintenance: Array<PreventiveWorkOrdersActive>;
  public preventiveMaintenanceStr: Array<string>;
  public arrCorrectiveMaintenance: Array<CorrectiveWorkOrdersListItem>;
  public correctiveMaintenanceStr: Array<string>;
  public arrProject: Array<any>;
  public formGroup: FormGroup;
  public arrDispatch: Array<DispatchData>;
  
  public isLoading: boolean;
  public isLoadingForm: boolean;

  public filteredPreventiveMaintenance!: Observable<Array<string>>;
  public filteredCorrectiveMaintenance!: Observable<Array<string>>;
  constructor(
    @Inject(MAT_DIALOG_DATA) public requestLines: RequestLines,
    public resourcesService: ResourcesService,
    private _preventiveMaintenanceService: PreventiveMaintenanceService,
    private _correctiveMaintenanceService: CorrectiveMaintenanceService,
    private _dialogRef: MatDialogRef<boolean>,
    private _encService: EncService,
    private _router: Router,
    private _dialog: MatDialog,
    private _orderService: OrderService,
    private _distpachService: DispatchService,
  ) {
    this.arrPreventiveMaintenance = [];
    this.arrCorrectiveMaintenance = [];
    this.preventiveMaintenanceStr = [];
    this.correctiveMaintenanceStr = [];

    this.arrProject = [];
    this.arrDispatch = [];

    this.formGroup = this.createFormGroup();
    this.isLoading = false;
    this.isLoadingForm = false;
  }

  async ngOnInit() {
    this.isLoading = true;
    this.isLoadingForm = true;
    await this.getPreventiveMaintenance();
    await this.getCorrectiveMaintenance();
    await this.getDispatchActive();
    this.isLoading = false;
    this.isLoadingForm = false;


    this.formGroup.controls['PROPOSITO'].valueChanges.subscribe((proposito) => {
      console.log(proposito);
      if (proposito === 'MANT-PREV') {
        this.formGroup.controls['ID_MANT_PREV'].enable();
        this.formGroup.controls['ID_MANT_PREV'].setValidators(Validators.required);
        this.formGroup.controls['ID_MANT_CORR'].setValue('');
        this.formGroup.controls['ID_MANT_CORR'].disable();
        this.formGroup.controls['ID_MANT_CORR'].clearValidators();
        this.formGroup.controls['ID_MANT_PREV'].updateValueAndValidity();
        this.formGroup.controls['ID_MANT_CORR'].updateValueAndValidity();
      } else if (proposito === 'MANT-CORR') {
        this.formGroup.controls['ID_MANT_CORR'].enable();
        this.formGroup.controls['ID_MANT_CORR'].setValidators(Validators.required);
        this.formGroup.controls['ID_MANT_PREV'].disable();
        this.formGroup.controls['ID_MANT_PREV'].setValue('');
        this.formGroup.controls['ID_MANT_PREV'].clearValidators();
        this.formGroup.controls['ID_MANT_PREV'].updateValueAndValidity();
        this.formGroup.controls['ID_MANT_CORR'].updateValueAndValidity();
      } else if (proposito === 'NA') {
        this.formGroup.controls['ID_MANT_PREV'].disable();
        this.formGroup.controls['ID_MANT_CORR'].disable();
        this.formGroup.controls['ID_MANT_PREV'].setValue('');
        this.formGroup.controls['ID_MANT_CORR'].setValue('');
        this.formGroup.controls['ID_MANT_PREV'].clearValidators();
        this.formGroup.controls['ID_MANT_CORR'].clearValidators();
        this.formGroup.controls['ID_MANT_PREV'].updateValueAndValidity();
        this.formGroup.controls['ID_MANT_CORR'].updateValueAndValidity();
      }
    });

    this.filteredPreventiveMaintenance = this.formGroup.controls['ID_MANT_PREV'].valueChanges.pipe(
      startWith(''),
      map((value) => {
        value = value === null || value === undefined ? '' : value;
        const filterValue = value.toLowerCase();
        return this.preventiveMaintenanceStr.filter((item) => item.toLowerCase().includes(filterValue));
      })
    )
    this.filteredCorrectiveMaintenance = this.formGroup.controls['ID_MANT_CORR'].valueChanges.pipe(
      startWith(''),
      map((value) => {
        value = value === null || value === undefined ? '' : value;
        const filterValue = value.toLowerCase();
        return this.correctiveMaintenanceStr.filter((item) => item.toLowerCase().includes(filterValue));
      })
    )
  }

  private createFormGroup() {
    return new FormGroup({
      PROPOSITO: new FormControl( 'NA', Validators.required),
      ID_MANT_PREV: new FormControl({ value: '', disabled: true }),
      ID_MANT_CORR: new FormControl({ value: '', disabled: true }),
      DESPACHO: new FormControl( '', Validators.required),
    });
  }

  public alert() {
    this._dialog.open(AlertComponent, {
      data: {
        title: `Confirmación`,
        description: `¿Está seguro de registrar la orden de compra?`,
        icon: `warning`,
      }
    }).afterClosed().subscribe((result) => {
      if (result) {
        this.createOrder()
      }
    });
  }

  private async createOrder() {
    this.isLoadingForm = true;
    const objRequestOrder: RequestOrder | false = this.createRequestOrder();
    if (objRequestOrder !== false) {
      await lastValueFrom(this._orderService.generateOrders(objRequestOrder)).then(
        (responseData: ResponseData) => {
          if (!responseData.error) {
            this.resourcesService.openSnackBar(`¡Registro de Orden Exitosa!`);
            this._router.navigate(['/sam/GEAD/ADQU/SEPE']);
            this._dialogRef.close(true);
          } else {
            this.resourcesService.openSnackBar(`${responseData.error}`);
          }
        }, (httpErrorResponse:HttpErrorResponse) => this.resourcesService.checkErrors(httpErrorResponse)
      );
    }
    this.isLoadingForm = false;
  }

  private createRequestOrder(): RequestOrder | false {

    let numeroSolicitud = this.requestLines.ID_LINEA_SOLICITUD.toString().trim();
    
    let despacho = this.formGroup.controls['DESPACHO'].value;
    despacho = despacho.toString().trim();

    let proposito = this.formGroup.controls['PROPOSITO'].value;

    let idMantPrev = this.formGroup.controls['ID_MANT_PREV'].value;

    let idMantCorr = this.formGroup.controls['ID_MANT_CORR'].value;

    if (proposito === 'MANT-PREV' && idMantPrev !== null && idMantPrev !== '' && idMantPrev !== undefined) {
      idMantPrev = idMantPrev.trim();
      idMantPrev = idMantPrev.replace('#', '');
      idMantCorr = null;
    } else if (proposito === 'MANT-CORR' && idMantCorr !== null && idMantCorr !== '' && idMantCorr !== undefined) {
      idMantCorr = idMantCorr.trim();
      idMantCorr = idMantCorr.replace('#', '');
      idMantPrev = null;
    } else if (proposito === 'NA') {
      idMantCorr = null;
      idMantPrev = null;
    } else {
      this.resourcesService.openSnackBar(`Ocurrió un error al verificar el propósito.`)
      return false;
    }

    let requestOrder:RequestOrder = {
      NUMERO_SOLICITUD: numeroSolicitud,
      DESPACHO: despacho,
      PROPOSITO: proposito,
      ID_MANT_PREV: idMantPrev,
      ID_MANT_CORR: idMantCorr,
      NUMERO_LINEA: this.resourcesService.getLineNumber(),
      USUARIO: this.resourcesService.getUser(),
    }
    console.log(requestOrder);
    return requestOrder;
  }

  private async getPreventiveMaintenance() {
    const idUser:string = this.resourcesService.getUser();
    const line:number = parseInt(this.resourcesService.getLineNumber());
    await lastValueFrom(this._preventiveMaintenanceService.getActiveWorkOrders(idUser, line)).then(
      (responseData: PreventiveWorkOrdersActiveResponse) => {
        if ( !responseData.error ) {
          this.arrPreventiveMaintenance = responseData.response;
          this.preventiveMaintenanceStr = this.arrPreventiveMaintenance.map(
            (preventiveWorkOrdersActive: PreventiveWorkOrdersActive) => `#${ preventiveWorkOrdersActive.IDORDEN }` )
        } else {
          this.resourcesService.openSnackBar(responseData.msg);
        }
      },(error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
  }

  private async getCorrectiveMaintenance() {
    const idUser:string = this.resourcesService.getUser();
    const line:number = parseInt(this.resourcesService.getLineNumber());
    await lastValueFrom(this._correctiveMaintenanceService.getWorkOrders(idUser, line)).then(
      async (responseData: CorrectiveWorkOrdersListResponse) => {
        if ( !responseData.error ) {
          this.arrCorrectiveMaintenance = responseData.response;
          this.correctiveMaintenanceStr = [];
          await Promise.all(
            this.arrCorrectiveMaintenance.map(async (correctiveWorkOrdersListItem: CorrectiveWorkOrdersListItem) => {
              let decOrder = await this._encService.decrypt(correctiveWorkOrdersListItem.ID_ORDEN);
              this.correctiveMaintenanceStr.push(`#${decOrder}`);
            })
          )
        } else {
          this.resourcesService.openSnackBar(responseData.msg);
        }
      },(error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
  }


  private async getDispatchActive() {
    await lastValueFrom(this._distpachService.getDispatchActive()).then(
      (responseData: ResponseDataDispatch) => {
        if ( !responseData.error ) {
          this.arrDispatch = responseData.response;
          if (this.arrDispatch.length === 0) {
            this.resourcesService.openSnackBar('No hay Despachos registrados en SAM');
          }
        } else {
          this.resourcesService.openSnackBar(responseData.msg);
        }
      },(error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
  }
}
