import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { environment } from '../../environments/environment';
import { AbstractService } from './abstract.service';

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

    constructor(private http: HttpClient, private abstractService: AbstractService) {
    }

    fetch(limit: number, offset: number, sort: string, descending: boolean, filter: string = "") {
        return this.abstractService.fetch("/users/manage", limit, offset, sort, descending, filter);
    }

    pick(search: string) {
        return this.abstractService.fetch("/users/manage/pick", undefined, undefined, undefined, undefined, { filter: search });
    }

    get(username: string) {
        return this.http.get(environment.apiUrl + "/users/manage/" + username);
    }

    create(user: any) {
        return this.http.post(environment.apiUrl + "/users/manage", user);
    }

    update(user: any) {
        return this.http.patch(environment.apiUrl + "/users/manage", user);
    }

    setPassword(username: string, password: string) {
        return this.http.post(environment.apiUrl + "/users/manage/" + username + "/password", { password: password, password2: password });
    }

    deleteUser(username: string) {
        return this.http.delete(environment.apiUrl + "/users/manage/" + username);
    }
}