import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService } from '../api.service';
import { Qcm, QcmCategory, QcmQuestion } from '@models';

@Injectable({ providedIn: 'root' })
export class QcmService {
    private _apiVersion = '/v1';
    constructor(
        private apiService: ApiService
    ) { }

    get(type: string): Observable<Qcm> {
        return this.apiService.get(`${this._apiVersion}/qcm/type/${type}`).pipe(map(data => data.qcm));
    }

    saveChoices(question: object): Observable<QcmQuestion> {
        return this.apiService.post(`${this._apiVersion}/qcm/choices`, { question: question })
            .pipe(map(data => data.question));
    }

    saveQuestions(category: object): Observable<QcmCategory> {
        return this.apiService.post(`${this._apiVersion}/qcm/questions`, { category: category })
            .pipe(map(data => data.category));
    }
}
