// Angular Core
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Router } from '@angular/router';

// Services
import { SupplierCatalogService } from 'src/app/services/supplierCatalog/supplier-catalog.service';
import { UtilitiesService } from 'src/app/services/utilities/utilities.service';
import { environment } from 'src/environments/environment';

@Component({
  standalone: false,
  selector: 'app-supplier-recommended-products',
  templateUrl: './supplier-recommended-products.component.html',
  styleUrls: ['./supplier-recommended-products.component.scss'],
})
export class SupplierRecommendedProductsComponent implements OnInit {
  @Input() supplier!: any;
  @Input() recomProds!: any;
  @Input() recomSuppliers!: any;
  @Input() accordText!: any;
  @Output() getSuppProductsEmitter = new EventEmitter();
  @Output() checkAccordionEmitter = new EventEmitter();
  @Output() checkQuantityEmitter = new EventEmitter();
  @Output() setProductQtyEmitter = new EventEmitter();
  @Output() keypressQtyEmitter = new EventEmitter();
  imgUrl: string = environment.imageUrl;

  constructor(
    private supplierCatalogService : SupplierCatalogService,
    private utilitiesService: UtilitiesService,
    private router : Router,
  ) { }

  ngOnInit(): void {
  }

  getSuppProducts(suppId: any, repull: any, notifDisplay: any): any {
    const dataToEmit = {
      suppId: suppId,
      repull: repull,
      notifDisplay: notifDisplay,
    }
    this.getSuppProductsEmitter.emit(dataToEmit);
  }

  checkAccordion(id: any) {
    this.checkAccordionEmitter.emit(id);
  }

  /**
  * redirect user to supplier search results page with passed values
  */
  supplierCatalogSearch(supplierName: string, supplierId: number) {
    // this.supplierCatalogService.setSupplierId(supplierId)
    this.router.navigate(['/market/supplierCatalog'], { queryParams: { supplierName: escape(supplierName), supplierId: supplierId, mdl: 'mp' } })
  };

  checkQuantity(event: any) {
    this.checkQuantityEmitter.emit(event);
  }

  setProductQty(auctionId: any, supplier: any, qty: any, oldValue: any, fromPage: any, minPurchaseQuantity: any) {
    this.recomProds[supplier.supplierData.id].map((data: any) => {
      if(data.id == auctionId){
        data.quantity = qty === 0 ? minPurchaseQuantity: qty
      }
    })
    const dataToEmit = {
      auctionId: auctionId,
      supplier: supplier,
      qty: qty,
      oldValue: oldValue,
      fromPage: fromPage,
      minPurchaseQuantity: minPurchaseQuantity
    }
    this.setProductQtyEmitter.emit(dataToEmit);
  }

  keypressQty(event: any) {
    this.keypressQtyEmitter.emit(event);
  }
  
  /**
   * Method to check if the Expiration Date is below 90 days or not
   * @param {string} expirationDate
   * @return {Boolean}
   */
  isExpireIn90Days(expirationDate: string) {
    return this.utilitiesService.getExpireDateBelow90Days(expirationDate);
  }

  trackByFn(index: number, item: any): any{
    return item.key
  }

}
