import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {HttpErrorService} from 'src/app/ErrorComponents/http-error.service';
import {catchError} from 'rxjs/operators';
import {Observable} from 'rxjs';
import {StudentResponseDao} from '../dao/student-response.dao';

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'
  })
};

@Injectable()
export class StudentInfoService {

  constructor(private http$: HttpClient, private errorService: HttpErrorService) {
  }

  public getStudentClasses(username: String): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/getStudentClasses', {username: username}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }

  public getOpenPapers(username: String): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/getOpenPapers', {username: username}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }

  public getSubmittedPapers(username: String, classCode: String): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/getSubmittedClassPapers', {username: username, classCode: classCode}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }

  public getPaperQuestions(username: String, paperId: String): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/getPaperQuestions', {paperId: paperId}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }

  public getStudentAnswers(username: String, paperId: String): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/getStudentAnswers', {username: username, paperId: paperId}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }

  public saveDraft(answers: StudentResponseDao[]): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/saveDraft', {answers: answers}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }

  public submitResponse(answers: StudentResponseDao[]): Observable<String | Object> {
    return this.http$.post('http://localhost:8080/submitResponse', {answers: answers}, httpOptions).pipe(
      catchError(this.errorService.handleError)
    );
  }
}