import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { CourseModel } from '../model/course.model';
import { CourseContentModel } from '../model/course-content.model';

@Injectable({
  providedIn: 'root'
})
export class CourseService {
  url = environment.url
  createStatus: boolean = true
  constructor(private http: HttpClient) { }
  getListCourse(status?:string): Observable<CourseModel[]> {
    if(status){
      return this.http.get<CourseModel[]>(this.url + "course-center/upload/lists?status="+status)
      }else{
      return this.http.get<CourseModel[]>(this.url + "course-center/upload/lists")
    }
  }
  getListCourseContent(status?: string): Observable<CourseContentModel[]> {
    return this.http.get<CourseContentModel[]>(this.url + "course-center/content/lists")
  }
  getCourseById(logId:string): Observable<CourseModel> {
    return this.http.get<CourseModel>(this.url + "course-center/upload/"+logId)
  }

  downloadFile(logId:string):Observable<any>{
    return this.http.get(this.url + "course-center/files/download/"+logId,  { responseType: 'blob'  })
  }
  downloadFileContent(logId:string):Observable<any>{
    return this.http.get(this.url + "course-center/files/download/content/"+logId, { responseType: 'blob'  });
  }
  createCourse(model: CourseModel): Observable<any> {
    let body : any = model
    if (body.group.groupId=='') {
      body.group = undefined
    }
    return this.http.post(this.url + 'course-center', body)
  }
  deleteCourse(model: CourseModel): Observable<any> {
    let body = {
      logId: model.logId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'course-center/upload', option)
  }
  deleteCourseContent(model: CourseContentModel): Observable<any> {
    let body = {
      courseId: model.courseId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'course-center/content', option)
  }
  approve(model: CourseModel): Observable<any> {
    let body = {
      logId: model.logId,
      status: model.status
    }
    return this.http.put<any>(this.url + "course-center/approve",body)
  }
  getCount(status?: string): Observable<number> {
    return this.http.get<number>(this.url + "course-center/upload/count")
  }
  getCountContent(status?: string): Observable<number> {
    return this.http.get<number>(this.url + "course-center/content/count")
  }
}
