// 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) {}

	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));
	}

	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));
	}
}
