// 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 { HttpClient } from '@angular/common/http';
import { finalize, map, switchMap } from 'rxjs/operators';
import { Observable, Subject, from } from 'rxjs';
import { ConfigService } from './config.service';

@Injectable()
export class LoginService {
	reload = new Subject<boolean>();

	constructor(private configService: ConfigService, private http: HttpClient) {}

	set_reload(r: any) {
		this.reload.next(r);
	}

	getMenuApp(id: number, menu_app: boolean): Observable<any> {
		return this.http.get(this.configService.serviceUrl_Login + 'getmenuApp/' + id + '/' + menu_app, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	login(data: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Login + 'login/' + 0, data, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	login_check_app(data: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Login + 'login/check_app/' + 0, data, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	confirm_app(id: number, action: string): Observable<any> {
		return this.http.delete(this.configService.serviceUrl_Login + 'confirm_app/' + id + '/' + action, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	send_sms(data: any, checkUserRegistered: boolean, registrationStep: any): Observable<any> {
		return this.http.post(`${this.configService.serviceUrl_Login}send_sms/${checkUserRegistered}${registrationStep ? '?rs=' + registrationStep : ''}`, data, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	check_token_sms(data: any, number: string, token: string, remember_me: boolean): Observable<any> {
		return this.http.post(`${this.configService.serviceUrl_Login}check_token_sms?number=${number}&token=${token}&remember_me=${remember_me}`, data, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	sendMailresetPasswordUser(user: any): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Login + 'send_mail_reset_user_password', user, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	send_token_registration(data: any, registrationStep: number, guid: string): Observable<any> {
		return this.http.post(`${this.configService.serviceUrl_Login}send_token_registration/${registrationStep ? '?rs=' + registrationStep : ''}`, { user: data, guid: guid }, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}
	check_token_registration(number: string, token: string, guid: string): Observable<any> {
		return this.http.post(this.configService.serviceUrl_Login + 'check_token_registration/' + number + '/' + token, guid, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	save_nominativo(firstname: string, lastname: string, displayname: string, phone: string, email: string, password: string, codicefiscale: string, userData: any): Observable<any> {
		let user = Object.assign({}, userData);
		user.usr_firstname = firstname;
		user.usr_lastname = lastname;
		user.usr_displayname = displayname;
		user.usr_phone = phone;
		user.usr_mail = email;
		user.usr_password = password;
		user.usr_codicefiscale = codicefiscale;

		//localStorage.setItem('currentUser', JSON.stringify(user));

		return this.http
			.put(this.configService.serviceUrl_Login + 'save_nominativo', user, {
				observe: 'response',
			})
			.pipe(
				map((response) => {
					return response.body;
				}),
			);
	}

	register_user(usr_id: number, username: string, firstname: string, lastname: string, displayname: string, phone: string, email: string, password: string, codicefiscale: string, sessionRegisterUUID: string): Observable<any> {
		let user = Object.assign({});
		user.usr_id = usr_id;
		user.usr_username = username;
		user.usr_firstname = firstname;
		user.usr_lastname = lastname;
		user.usr_displayname = displayname;
		user.usr_phone = phone;
		user.usr_mail = email;
		user.usr_password = password;
		user.usr_codicefiscale = codicefiscale;

		return this.http.put(this.configService.serviceUrl_Login + 'register_user', { user: user, guid: sessionRegisterUUID }, { observe: 'response' }).pipe(
			map((response) => {
				return response.body;
			}),
		);
	}

	comuni(): Observable<any> {
		return this.http
			.get(this.configService.serviceUrl_Login + 'comuni', {
				observe: 'response',
			})
			.pipe(
				map((response) => {
					return response.body;
				}),
			);
	}

	setUserLastAccess() {
		return this.http
			.get(this.configService.serviceUrl_Login + 'set_user_last_access', {
				observe: 'response',
			})
			.pipe(
				map((response) => {
					return response.body;
				}),
			);
	}

	getUser(usr_id: number): Observable<any> {
		return this.http
			.get(`${this.configService.serviceUrl_PortalManager}user/get/${usr_id}`, {
				observe: 'response',
			})
			.pipe(map((response) => response.body));
	}

	getUsersByRole(cerca: string, orderby: string, limitda: number, limitper: number, role_id: number): Observable<any> {
		return this.http.get(`${this.configService.serviceUrl_PortalManager}user/list?usr_username=${cerca}&orderby=${orderby}&limitda=${limitda}&limitper=${limitper}&role_id=${role_id}`, { observe: 'response' }).pipe(map((response) => response.body));
	}

	saveNotificationPreferences(usr_id: number, emailNotifications: boolean, smsNotifications: boolean): Observable<any> {
		return this.http.put(`${this.configService.serviceUrl_PortalManager}user/preferences/${usr_id}`, { emailNotifications, smsNotifications }, { observe: 'response' }).pipe(map((response) => response.body));
	}

	// Aggiorna l'immagine profilo dell'utente autenticato (data-URL base64, o '' per rimuoverla).
	updateAvatar(avatar: string): Observable<any> {
		return this.http.put(`${this.configService.serviceUrl_PortalManager}user/avatar`, { avatar }, { observe: 'response' }).pipe(map((response) => response.body));
	}

	getRuoliUser(usr_id: number): Observable<any> {
		return this.http
			.get(`${this.configService.serviceUrl_PortalManager}ruoli/listaruoliuser/${usr_id}`, {
				observe: 'response',
			})
			.pipe(map((response) => response.body));
	}
}
