// 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 { ConfigService } from './config.service';

@Injectable({
	providedIn: 'root',
})
export class BakekaService {
	constructor(private config: ConfigService, private http: HttpClient) {}

	getListEvents(bk: any): Observable<any> {
		// la lista prende anche byId se metto bk.bk_id = num
		return this.http.post(`${this.config.bakekaUrl}bakeka/getListPost`, bk, { observe: 'response' }).pipe(map((response) => response.body));
	}

	getPost(cmp_id: number, bk_id: number): Observable<any> {
		return this.http.get(`${this.config.bakekaUrl}bakeka/getPost/${cmp_id}/${bk_id}`, { observe: 'response' }).pipe(map((response) => response.body));
	}

	addPost(bk: any): Observable<any> {
		return this.http.post(`${this.config.bakekaUrl}bakeka/addPost`, bk, { observe: 'response' }).pipe(map((response) => response.body));
	}

	updatePost(bk: any): Observable<any> {
		return this.http.post(`${this.config.bakekaUrl}bakeka/updatePost`, bk, { observe: 'response' }).pipe(map((response) => response.body));
	}

	deletePost(bk_id: number, deletelist: number[]): Observable<any> {
		return this.http.post(`${this.config.bakekaUrl}bakeka/deletePost/${bk_id}`, deletelist, { observe: 'response' }).pipe(map((response) => response.body));
	}

	addLike(bk_id: any): Observable<any> {
		return this.http
			.post(`${this.config.bakekaUrl}bakeka/insert_like_post/${bk_id}`, { observe: 'response' })
			.pipe
			//map(response => response.body),
			();
	}

	removeLike(bk_id: any): Observable<any> {
		return this.http
			.delete(`${this.config.bakekaUrl}bakeka/remove_like_post/${bk_id}`, { observe: 'response' })
			.pipe
			//map(response => response.body),
			();
	}

	// Segnala un post agli amministratori di portale (email via scheduler).
	// note e' facoltativa; bk_title/cmp_name servono a comporre l'email.
	reportPost(bk_id: number, payload: { note?: string; bk_title?: string; cmp_name?: string }): Observable<any> {
		return this.http.post(`${this.config.bakekaUrl}bakeka/report/${bk_id}`, payload, { observe: 'response' }).pipe(map((response) => response.body));
	}
}
