import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { getBasePath } from 'app/app.routing';

@Injectable({
    providedIn: 'root'
})
export class DashboardService
{
    // Observables
    private _data: BehaviorSubject<any>;

    /**
     * Constructor
     *
     * @param {HttpClient} _httpClient
     */
    constructor(
        private _httpClient: HttpClient
    )
    {
        // Set the private defaults
        this._data = new BehaviorSubject(null);
    }

    // -----------------------------------------------------------------------------------------------------
    // @ Accessors
    // -----------------------------------------------------------------------------------------------------

    /**
     * Getter for data
     */
    get data$(): Observable<any>
    {
        return this._data.asObservable();
    }

    // -----------------------------------------------------------------------------------------------------
    // @ Public methods
    // -----------------------------------------------------------------------------------------------------

    /**
     * Get data
     */
    getSummaryData(): Observable<any>
    {
        return this._httpClient.get(getBasePath() + '/api/summary').pipe(
            tap((response: any) => {
                this._data.next(response);
            })
        );
    }

    getSummaryTempData(durationKey: string): Observable<any>
    {
        const params = {}
        if(durationKey){
            params['duration_key'] = durationKey
        }

        return this._httpClient.get(getBasePath() + '/api/summary/temp', {params: params});
    }
}
