import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SearchResultDTO } from 'app/shared/model/search/search-result-dto.model';

import { SERVER_API_URL } from 'app/app.constants';

@Injectable({ providedIn: 'root' })
export class ExerciseService {
    public resourceUrl = SERVER_API_URL + 'api/exerciseFile/';
    public exerciseUrl = SERVER_API_URL + 'api/exercise/';

    constructor(private http: HttpClient) {}

    public loadExerciseFile(exerciseId: string, filePath: string): Observable<string> {
        const headers = new HttpHeaders(); // .set('Content-Type', 'text/plain; charset=utf-8');
        const options: HttpParams = new HttpParams();
        options.append('filePath', filePath);

        return this.http.get<string>(this.resourceUrl + exerciseId, {
            headers,
             params: new HttpParams().set('filePath', filePath),
             responseType: 'text' as 'json'
             });
    }
    
    public loadExercise(exerciseId: string): Observable<SearchResultDTO> {

        return this.http.get<SearchResultDTO>(this.exerciseUrl + exerciseId);
    }
}