// Questo file fa parte di MyTown.
// MyTown è software libero: puoi ridistribuirlo e/o modificarlo secondo
// i termini della GNU Affero General Public License come pubblicata dalla
// Free Software Foundation, versione 3.
// Distribuito senza ALCUNA GARANZIA; vedi la GNU AGPL v3 per i dettagli.
// Copyright (C) Kondor Consulting & Services S.r.l.
// SPDX-License-Identifier: AGPL-3.0-or-later

import { Injectable } from '@angular/core';
import { MonoTypeOperatorFunction, Observable, of, OperatorFunction} from 'rxjs'; // necessario per leggere e contenere 
import { catchError, map, tap, switchMap } from 'rxjs/operators'; // permette di rintracciare errori 
import { HttpClient } from '@angular/common/http';

import { ConfigService } from './config.service';

@Injectable({
  providedIn: 'root'
})

export class ProdService {

  constructor(private config: ConfigService,
              private http: HttpClient,) {}
  
  //Tipologia del prodotto
get_typewine(filter: any): Observable<any> {
        return this.http.post(`${this.config.onlineshopurl}search/filtri/get_typewine`, filter, { observe: 'response' }).pipe(
          map((response) => response.body),
        ); }

  // lista prodotti filtrati
lstProducts(data: any): Observable<any> {

  return this.http.post(`${this.config.onlineshopurl}search/filtri/getProducts`, data, {  observe: "response" }).pipe(
    map(response => response.body)
  ); }

  // max min price
MinMaxPrd(data: any): Observable<any>{
  return this.http.post(`${this.config.onlineshopurl}search/filtri/getProducts_min_max`, data, {  observe: "response" }).pipe(
    map(response => response.body)
  ); }

  // insert prodotto
addPrd(data: any): Observable<any> {
    return this.http.post(`${this.config.onlineshopurl}product/insertProduct`, data, {  observe: "response" }).pipe(
    map(response => response.body)

  ); }

  // Detail product
  getProductDetails(cmp_id: number, lang: string, prd_id: number): Observable<any> {
         return this.http.get(`${this.config.onlineshopurl}product/getProductById/${cmp_id}/${lang}/${prd_id}`, {  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }

  // delete product and product photo and product ratings
  deleteProduct(prd_id: Number): Observable<any> {
        return this.http.delete(`${this.config.onlineshopurl}product/deleteProduct/${prd_id}`, {  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }

  // update product
  updateProduct(lang: any, prd: any): Observable<any> {
       return this.http.post(`${this.config.onlineshopurl}product/updateProduct/${lang}`, prd,{  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }

  // delete tipologia
  deleteTypePrd(tpw_id: Number): Observable<any> {
        return this.http.delete(`${this.config.onlineshopurl}search/filtri/deleteTypePrd/${tpw_id}`, {  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }

    // update tipologia
  updateTypePrd(tpw: any): Observable<any> {
       return this.http.post(`${this.config.onlineshopurl}search/filtri/updateTypePrd`, tpw,{  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }

    // insert tipologia
  addTypePrd(tpw: any): Observable<any> {
    return this.http.post(`${this.config.onlineshopurl}search/filtri/addTypePrd`, tpw, {  observe: "response" }).pipe(
    map(response => response.body)

  ); }

    // add product alla wishlist
  addPrdWishlist(cmp_id: number, prd_id: number): Observable<any> {
       return this.http.post(`${this.config.onlineshopurl}product/addPrdWishlist/${cmp_id}/${prd_id}` ,{  observe: "response" }).pipe(
         // map(response => response.body)
                   
  ); }

    // remove prd dalla wishlist
  removePrdWishlist(prd_id: Number): Observable<any> {
        return this.http.delete(`${this.config.onlineshopurl}product/removePrdWishlist/${prd_id}`, {  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }



   addProductImages(ImageData: FormData, prd_id: Number): Observable<any> {
        let typephoto: number = 2;
        return this.http.post(`${this.config.onlineshopurl}product/images/add/${typephoto}/${prd_id}`, ImageData, {  observe: "response" }).pipe(
          map(response => response.body)
                   
  ); }


    removeProductPhoto(prph_id: Number): Observable<any> {
        return this.http.delete(`${this.config.onlineshopurl}product/images/remove/${prph_id}`, {  observe: "response" }).pipe(
          map(response => response.body)
  ); }



}

        


