import { Injectable } from '@angular/core';
import { Subject, BehaviorSubject } from 'rxjs';
import { FilterOption, RemissionProduct } from '@isa/remission';
import { UpdateFilter } from '../models/update-filter.model';
import { RemissionResourceType } from '../models/remission-resource-type.model';

@Injectable({
  providedIn: 'root',
})
export class RemissionHelperService {
  refetchProducts$ = new Subject<void>();
  filteOptionsDeleteEvents$ = new Subject<FilterOption>();
  filtersClearedEvent$ = new Subject();
  closeFilters$ = new Subject();
  loadRemissionListProducts$ = new BehaviorSubject<number>(null);
  filtersUpdated$ = new Subject<UpdateFilter>();
  reApplyFilters$ = new BehaviorSubject<RemissionResourceType>(null);
  remissionListHits$ = new BehaviorSubject<number>(null);
  deleteRemissionListItem$ = new Subject<RemissionProduct>();
  addRemissionListItem$ = new Subject<RemissionProduct>();
  updateRemissionListItem$ = new Subject<{ quantity: number; product: RemissionProduct }>();
  filterHeight$ = new BehaviorSubject<number>(null);
  constructor() {}

  public requestRefetchProducts = () => this.refetchProducts$.next();
  public deleteFilterOption = (option: FilterOption) => this.filteOptionsDeleteEvents$.next(option);
  public clearFilters = () => this.filtersClearedEvent$.next();
  public closeFilters = () => this.closeFilters$.next();
  public updateFilters = (changes: UpdateFilter) => this.filtersUpdated$.next(changes);
  public reApplyFilters = (source: RemissionResourceType) => this.reApplyFilters$.next(source);
  public loadRemissionListProducts = (remissionProcessId: number) => this.loadRemissionListProducts$.next(remissionProcessId);
  public setRemissionListHits = (hits: number) => this.remissionListHits$.next(hits);
  public deleteRemissionListItem = (remissionProduct: RemissionProduct) => this.deleteRemissionListItem$.next(remissionProduct);
  public addRemissionListItem = (remissionProduct: RemissionProduct) => this.addRemissionListItem$.next(remissionProduct);
  public updateRemissionListItem = (quantity: number, product: RemissionProduct) =>
    this.updateRemissionListItem$.next({ quantity: quantity, product: product });
  public setFilterHeight = (height: number) => this.filterHeight$.next(height);
}
