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 { DocumentModel } from '../model/document.model';
import { DocumentContentModel } from '../model/document-content.model';
import { WidgetModel } from '../model/widgets.model';

@Injectable({
  providedIn: 'root'
})
export class WidgetService {
  url = environment.url
  createStatus: boolean = true
  constructor(private http: HttpClient) { }
  getListWidgets(status?: string): Observable<WidgetModel[]> {
    if (status) {
      return this.http.get<WidgetModel[]>(this.url + "widget/lists?status=" + status)
    } else {
      return this.http.get<WidgetModel[]>(this.url + "widget/lists")
    }
  }
  getListExcelContent(status?: string): Observable<DocumentContentModel[]> {
    return this.http.get<DocumentContentModel[]>(this.url + "document-center/content/lists")
  }
  getWidgetById(widgetId: string): Observable<WidgetModel> {
    return this.http.get<WidgetModel>(this.url + "widget/" + widgetId)
  }
  downloadFile(logId:string):Observable<any>{
    return this.http.get(this.url + "widget/files/download/"+logId,  { responseType: 'blob'  })
  }
  createWidget(model: WidgetModel): Observable<any> {
    let body : any = model
    return this.http.post(this.url + 'widget', body)
  }
  deleteWidget(model: WidgetModel): Observable<any> {
    let body = {
      widgetId: model.widgetId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'widget', option)
  }
  deleteExcelContent(model: DocumentContentModel): Observable<any> {
    let body = {
      docId: model.docId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'document-center/content', option)
  }


  getCount(status?: string): Observable<number> {
    return this.http.get<number>(this.url + "widget/count")
  }
  getCountContent(status?: string): Observable<number> {
    return this.http.get<number>(this.url + "document-center/content/count")
  }
}
