import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from "@angular/common/http";
import {ConfigurationService} from "../env/configuration.service";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";
import {Product} from "../model/product";
import {FiltersService} from "./filters.service";
import {CustomHttpParamEncoder} from "../custom-http-param-encoder";

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

  endPoint: string = '/services/product/';

  constructor(
    private http: HttpClient,
    private configService: ConfigurationService,
    private filtersService: FiltersService) {
  }

  public getRecordCount(epoch: number, type: number, pattern: string): Observable<number> {
    return this.http.get<number>(this.configService.config.url + this.endPoint + 'byEpoch/' + epoch.toString() + '/byType/' + type.toString() +
      '/pages?pattern=' + encodeURIComponent(pattern), {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public getPage(epoch: number, type: number, pageId: number, pattern: string): Observable<Array<Product>> {
    let params = new HttpParams({encoder: new CustomHttpParamEncoder()});
    if (pattern) {
      params = params.append('pattern', pattern);
    }

    const sortName = this.filtersService.getCurrentSetting('PRODUCT_SORT');
    const sortDir = this.filtersService.getCurrentSetting('SORT_DIRECTION');

    params = params.append('columnNames', sortName);
    params = params.append('directions', sortDir);

    return this.http.get<Array<Product>>(this.configService.config.url + this.endPoint + 'byEpoch/' + epoch.toString() + '/byType/' + type.toString() +
      '/pages/' + pageId.toString() + '/', {params: params, observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public getPageInfo(type: number): Observable<Array<string>> {
    return this.http.get<Array<string>>(this.configService.config.url + this.endPoint + 'types/' + type + '/queues', {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public getProductById(id: string): Observable<Product> {
    return this.http.get<Product>(this.configService.config.url + this.endPoint + 'byId/' + id, {observe: 'response'}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public getProductConfig(id: number, type: string): Observable<object> {
    return this.http.get<object>(this.configService.config.url + this.endPoint + 'byId/' + id + '/' + type, {observe: "response"}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public getProductTypeConfig(type: number): Observable<object> {
    return this.http.get<object>(this.configService.config.url + this.endPoint + 'types/' + type + '/configurations', {observe: "response"}).pipe(
      map(response => {
        return response.body;
      }));
  }

  public updateProductStatus(id: number, status: string): Observable<string> {
    return this.http.put<string>(this.configService.config.url + this.endPoint + 'byId/' + id + '/changeStatus?status=' + status, {}, {observe: "response"}).pipe(
      map(reponse => {
        return status;
      }));
  }

  public deleteProduct(id: number): Observable<number> {
    return this.http.delete<string>(this.configService.config.url + this.endPoint + 'byId/' + id , {observe: "response"}).pipe(
      map(response => {
        return response.status;
      }));
  }
}