// 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 { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ConfigService } from './config.service';
import { Translation } from '@ngx-translate/core';
import { finalize, map, Observable } from 'rxjs';
//import { SpinnerService } from './SpinnerService';
@Injectable()
export class TranslateService {
	private apiUrl = this.configService.serviceUrl_Login + 'translate';
	data: any = {};
	constructor(private http: HttpClient, private configService: ConfigService, /*private spinnerService: SpinnerService*/) {}

	use(): Promise<{}> {
		const currentUser = this.configService.getLocalStorageObjectJson('currentUser');
		let lang = 'it';
		if (currentUser != null) {
			lang = currentUser.usr_language;
		}
		if (lang == null || lang === undefined) {
			lang = 'it';
		}
		return new Promise<{}>((resolve, reject) => {
			const langPath = `src/assets/i18n/${lang || 'it'}.json`;
			this.http.get(langPath).subscribe(
				(translation) => {
					const tempdata: any = Object.assign({}, translation || {});
					this.data = Object.assign({}, JSON.parse(tempdata._body) || {});
					resolve(this.data);
				},
				(error) => {
					this.data = {};
					resolve(this.data);
				},
			);
		});
	}

	getTranslations(name: string, orderBy: string, limit: number, offset: number): Observable<any> {
		//this.spinnerService.show();
		const url = `${this.apiUrl}?name=${encodeURIComponent(name)}&order_by=${encodeURIComponent(orderBy)}&limit=${limit}&offset=${offset}`;
		return this.http.get<{ translations: Translation[]; totalRows: number }>(url, { observe: 'response', responseType: 'json' }).pipe(
			map((response) => response.body),
			/*finalize(() => this.spinnerService.hide()),*/
		);
	}

	addTranslation(translation: Translation): Observable<any> {
		//this.spinnerService.show();
		return this.http.post(this.apiUrl, translation, { observe: 'response' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}

	updateTranslation(id: number, translation: Translation): Observable<any> {
		//this.spinnerService.show();
		return this.http.put(`${this.apiUrl}/${id}`, translation, { observe: 'response' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}

	deleteTranslation(id: number): Observable<any> {
		//this.spinnerService.show();
		return this.http.delete(`${this.apiUrl}/${id}`, { observe: 'response' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}

	exportTranslations(): Observable<any> {
		//this.spinnerService.show();
		return this.http.get<any>(`${this.apiUrl}/manager/export`, { observe: 'response', responseType: 'json' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}

	importOverwrite(translations: any[]): Observable<any> {
		//this.spinnerService.show();
		return this.http.post(`${this.apiUrl}/manager/import-overwrite`, translations, { observe: 'response' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}

	previewMerge(translations: any[]): Observable<any> {
		//this.spinnerService.show();
		return this.http.post<any>(`${this.apiUrl}/manager/preview-merge`, translations, { observe: 'response', responseType: 'json' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}

	importMerge(translations: any[]): Observable<any> {
		//this.spinnerService.show();
		return this.http.post(`${this.apiUrl}/manager/import-merge`, translations, { observe: 'response' }).pipe(
			map((response) => response.body),
			//finalize(() => this.spinnerService.hide()),
		);
	}
}
