import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetNomenclature, ResponseDataGetNomenclature } from '../../../interfaces/inventory-management/nomenclature-management.interface';
import { ResourcesService } from '../../../services/resources.service';
import { InventoryManagementService } from '../../../services/inventory-management/inventory-management.service';
import { MatDialog } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
import { DetailsNomenclatureComponent } from './details-nomenclature/details-nomenclature.component';

@Component({
    selector: 'app-nomenclature-management',
    templateUrl: './nomenclature-management.component.html',
    styleUrl: './nomenclature-management.component.css',
    standalone: false
})
export class NomenclatureManagementComponent implements OnInit {
  public isLoading: boolean;
  public isLoadingForm: boolean;
  public txtSearch: string;
  public dataSource: MatTableDataSource<GetNomenclature>;
  public arrNomenclature: GetNomenclature[];

  constructor (
    public resourcesService: ResourcesService,
    private _inventoryManagementService: InventoryManagementService,
    private _dialog: MatDialog,
  ) {
    this.isLoading = false;
    this.isLoadingForm = false;
    this.txtSearch = '';
    this.dataSource = new MatTableDataSource<GetNomenclature>();
    this.arrNomenclature = [];
  }

  async ngOnInit() {
    await this.getNomenclatureManagement();
  }

  public async getNomenclatureManagement() {
    this.isLoading = true;
    const USER: string = this.resourcesService.getUser();
    const LINE: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._inventoryManagementService.getNomenclatureManagement(USER, LINE)).then(
      (responseData: ResponseDataGetNomenclature) => {
        if (!responseData.error) {
          this.dataSource.data = responseData.response;
          this.arrNomenclature = responseData.response;
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.getNomenclatureManagement();
        }
      }
    );
    this.isLoading = false;
  }

  public applyFilter() {
    
    const dataValue: string = this.txtSearch.toString().trim().toUpperCase();
    
    if (dataValue.length > 0) {
      let arrDataValue: GetNomenclature[] = [];
      this.arrNomenclature.forEach((nomenclature: GetNomenclature) => {      

        let exists: boolean = false;
        
        const arrValues = Object.values(nomenclature);
        arrValues.forEach((value: string) => {
          value = value.toString().trim().toUpperCase();
          if (value.includes(dataValue)) {
            exists = true;
          }
        });

        if (exists) {
          arrDataValue.push(nomenclature);
        }
      });

      this.dataSource.data = arrDataValue;
    } else {
      this.dataSource.data = this.arrNomenclature;
    }
  }

  public viewDetails(nomenclature: GetNomenclature) {
    this._dialog.open(DetailsNomenclatureComponent, {
      disableClose: true,
      data: nomenclature,
      width: `550px`,
      maxWidth: `550px`,
    });
  }
}
