// 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, finalize, Observable, of, OperatorFunction, Subject} 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 { SpinnerService } from 'src/app/_services/SpinnerService';

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

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

   num_product = new Subject<number>();

  constructor(private config: ConfigService,
              private http: HttpClient,
             /* private spinnerService: SpinnerService,*/) {}


  set_num_product(num: any) {
    this.num_product.next(num);
  }

    // aggiungi prd al carrello
  addPrdCart(p: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.put(`${this.config.onlineshopurl}cart/product_add`, p, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

    // get carrello
  getCart(cmp_id: number, lang: string, upp: boolean): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}cart/cart_get/${cmp_id}/${lang}/${upp}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

      // get commissioni carrello
  cart_commission_get(cmp_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}cart/cart_commission_get/${cmp_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

      // upd commisioni cart
  cart_commission_action(d: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.put(`${this.config.onlineshopurl}cart/cart_commission_action`, d, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }


  operator_delivery_get(cmp_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}cart/operator_delivery_get/${cmp_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  
  check_products(cmp_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}cart/check_products/${cmp_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  first_purchase_check_product(cmp_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}cart/first_purchase_check_product/${cmp_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  type_payment_get(cmp_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}cart/type_payment_get/${cmp_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  getConfigPayment(cmp_id: number, payment: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.post(`${this.config.onlineshopurl}cart/getConfigPayment/${cmp_id}`, payment, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  savePayment(cmp_id: number, payment: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.post(`${this.config.onlineshopurl}cart/savePayment/${cmp_id}`, payment, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }


  delivery_address_get(cmp_id: number, data: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.post(`${this.config.onlineshopurl}deliveryAddress/delivery_address_get/${cmp_id}`, data, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  
  delivery_address_add(data: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.post(`${this.config.onlineshopurl}deliveryAddress/delivery_address_add`, data, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  delivery_address_upd(data: any): Observable<any> {
    //this.spinnerService.show();
    return this.http.post(`${this.config.onlineshopurl}deliveryAddress/delivery_address_upd`, data, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  delivery_address_del(uda_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.delete(`${this.config.onlineshopurl}deliveryAddress/delivery_address_del/${uda_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  delivery_address_id(uda_id: number): Observable<any> {
    //this.spinnerService.show();
    return this.http.get(`${this.config.onlineshopurl}deliveryAddress/delivery_address_id/${uda_id}`, {  observe: "response" }).pipe(
    map(response => response.body),
    //finalize(() => this.spinnerService.hide()),

  ); }

  getComuniSelect(filterValue: string): Observable<any> {
	//this.spinnerService.show();
	return this.http.get(this.config.serviceUrl_Companies + 'locations/get_comuniselect?filterValue=' + filterValue, { observe: 'response' }).pipe(
	map((response) => response.body),
	//finalize(() => this.spinnerService.hide()),
  ); }

}