import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {HttpClient, HttpHeaders, HttpResponse} from "@angular/common/http";

@Injectable({
    providedIn: 'root'
})
export class MastodonApiService {
    constructor(private httpClient: HttpClient) {
    }

    get<T>(url: string) {
        return this.httpClient.get<T>(url);
    }

    getAuthenticated<T>(url: string, accessToken: string): Observable<T> {
        const reqHeader = new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + accessToken
        });
        return this.httpClient.get<T>(url, {headers: reqHeader});
    }

    getAuthenticatedWithResponseHeaders<T>(url: string, accessToken: string): Observable<HttpResponse<T>> {
        const reqHeader = new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + accessToken
        });
        return this.httpClient.get<T>(url, {headers: reqHeader, observe: 'response'});
    }

    post<T>(url: string, parameters: object) {
        return this.httpClient.post<T>(url, parameters);
    }

    postAuthenticated(url: string, body: {}, accessToken: string) {
        const reqHeader = new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + accessToken
        });
        return this.httpClient.post(url, body, {headers: reqHeader});
    }

    putAuthenticated(url: string, body: {}, accessToken: string) {
        const reqHeader = new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + accessToken
        });
        return this.httpClient.put(url, body, {headers: reqHeader});
    }

    deleteAuthenticated(url: string, body: { account_ids: string[] }, accessToken: string) {
        const reqHeader = new HttpHeaders({
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + accessToken
        });
        return this.httpClient.delete(url, {headers: reqHeader, body});
    }
}
