import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetStockBarcode, ResponseDataGetStockBarcode } from '../../../interfaces/inventory-management/barcode-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 { ViewBarcodeComponent } from './view-barcode/view-barcode.component';
import { FormBarcodeComponent } from './form-barcode/form-barcode.component';

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

  constructor(
    public resourcesService: ResourcesService,
    private _inventoryManagementService: InventoryManagementService,
    private _dialog: MatDialog,
  ) {
    this.isLoading = false;
    this.isLoadingForm = false;
    this.txtSearch = '';
    this.dataSource = new MatTableDataSource<GetStockBarcode>();
    this.arrStockBarcode = [];
  }
  
  async ngOnInit() {
    await this.getStockBarcode();
  }

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

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

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

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

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

  public viewBarcode(barcode:string) {
    this._dialog.open(ViewBarcodeComponent, {
      data: barcode,
    }).afterClosed().subscribe((result) => {
      if (result) {
        this.getStockBarcode();
      }
    });
  }

  public openDialog(stockBarcode: GetStockBarcode) {
    this._dialog.open(FormBarcodeComponent, {
      disableClose: true,
      data: stockBarcode,
      width: '420px',
      maxWidth: '420px',
    }).afterClosed().subscribe((result) => {
      if (result) {
        this.getStockBarcode();
      }
    });
  }
}
