// Angular Core
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Subject } from 'rxjs';

// Services
import { ApiService } from '../api/api.service';

@Injectable({
  providedIn: 'root'
})
export class SupplierCatalogService {

  constructor(private http: HttpClient, public api: ApiService) { }

  private supplierCatalogOnSamePageSource = new Subject<{ name: string; supplierId: number }>();
  supplierCatalogOnSamePage$ = this.supplierCatalogOnSamePageSource.asObservable();

  emitSupplierCatalogOnSamePage(data: { name: string; supplierId: number }) {
    this.supplierCatalogOnSamePageSource.next(data);
  }
  supplierName: string = '';
  supplierId: number = 0;

  /**
   * sets supplier Name
   * @param {string} name
  */
  setSupplierName(name: string) {
    this.supplierName = name;
  };

  /**
   * gets current supplierName
   * @returns {String|supplierName}
  */
  getSupplierName() {
    return this.supplierName;
  };

  /**
   * sets supplier Id
   * @param  id
  */
  setSupplierId(id: number) {
    this.supplierId = id;
  };

  /**
   * gets current supplier id
   * @returns {String|supplierId}
  */
  getSupplierId() {
    return this.supplierId;
  };

  /**
   * gets active products for supplier
   * @param {type} data
   * @returns {unresolved}
   */
  getActiveProducts(data: any) {
    return this.api.create('/Supplier-Listings/activeProducts', data);
  };

  /**
   * Delete Auction
   * @param {type} auctionId
   * @returns {unresolved}
   */
  deleteProduct(auctionId: number) {
    var data = {
      'auctionId': auctionId
    };
    return this.api.create('/Supplier-Listings/deleteAuction', data);
  };

  /**
   * Edit Auction
   * @param {type} auctionId
   * @param {type} currentEndTime
   * @param {type} editData
   * @returns {unresolved}
   */
  editProduct(auctionId: number, currentEndTime: string, editData: any) {
    var data = {
      'data': editData,
      'auctionId': auctionId,
      'currentEndTime': currentEndTime
    };
    return this.api.create('/Supplier-Listings/editAuction', data);
  };

  /**
   * Update active products with selected date
   * @param object searchData
   * @param string endDate
   * @param array selectedDate
   * @returns {unresolved}
   */
  updateActiveProducts(searchData: any, endDate: string, selectedDate: any) {
    var data = {
      'searchData': searchData,
      'endDate': endDate,
      'selectedDate': selectedDate
    };
    return this.api.create('/Supplier-Listings/updateProducts', data);
  };

}