import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetObjectsProperties, ResponseDataGetObjectsProperties } from '../../../interfaces/inventory-management/object-properties-artitle.interface';
import { ResourcesService } from '../../../services/resources.service';
import { EncService } from '../../../services/enc.service';
import { Router } from '@angular/router';
import { InventoryManagementService } from '../../../services/inventory-management/inventory-management.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
    selector: 'app-object-properties-artitle',
    templateUrl: './object-properties-artitle.component.html',
    styleUrl: './object-properties-artitle.component.css',
    standalone: false
})
export class ObjectPropertiesArtitleComponent implements OnInit {
  public isLoading: boolean;
  public isLoadingForm: boolean;
  public txtSearch: string;
  public dataSource: MatTableDataSource<GetObjectsProperties>;
  public arrObjectProperties: GetObjectsProperties[];

  constructor (
    public resourcesService: ResourcesService,
    private _encService: EncService,
    private _router: Router,
    private _inventoryManagementService: InventoryManagementService,
  ) {
    this.isLoading = false;
    this.isLoadingForm = false;
    this.txtSearch = '';
    this.dataSource = new MatTableDataSource<GetObjectsProperties>();
    this.arrObjectProperties = [];
  }
  
  async ngOnInit() {
    await this.getObjectProperties();
  }

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

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

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

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

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

  public async getDetailsObjectProperties(objectsProperties: GetObjectsProperties) {
    const encStock = await this._encService.encrypt(objectsProperties.ID_STOCK);
    this._router.navigate([`/sam/GIST/RAOP/details/${encStock}`]);
  }
}
