import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs';
import { Dashboard } from '../model/Dashboard';
import { API_BASE_URL } from 'src/modules/common/type/injection-token.type';

@Injectable({
  providedIn: 'root'
})
export class DashboardService {
  constructor(
    @Inject(API_BASE_URL) private apiBaseUrl: string,
    private httpClient: HttpClient
  ) {}

  public getDashboards(): Observable<Dashboard[]> {
    return this.httpClient.get<Dashboard[]>(
      `${this.apiBaseUrl}/dashboards`,
      {}
    );
  }

  public updateDashboardDisplay(
    id: number,
    display: boolean
  ): Observable<Dashboard> {
    return this.httpClient.put<Dashboard>(
      `${this.apiBaseUrl}/dashboards/${id}/display/${display}`,
      {}
    );
  }

  public updateDashboardSortOrder(
    id: number,
    targetSortOrder: number
  ): Observable<Dashboard> {
    return this.httpClient.put<Dashboard>(
      `${this.apiBaseUrl}/dashboards/${id}/sort_order/${targetSortOrder}`,
      {}
    );
  }

  public deleteDashboard(id: number): Observable<void> {
    return this.httpClient.delete<void>(
      `${this.apiBaseUrl}/dashboards/${id}`,
      {}
    );
  }
}
