import { inject, Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { isPresent } from 'app/core/util/operators';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { ResilientService } from 'app/resilient/resilient-base/resilient-base.service';
import { createRequestOption } from 'app/core/request/request-util';
import { IDashboardComponent, NewDashboardComponent } from '../dashboard-component.model';
import { IDashboardComponentDetail } from '../dashboard-component-detail.model';
import { IDashboardComponentDetailValue } from '../dashboard-component-detail-value.model';
import { IOrganization } from 'app/entities/organization/organization.model';

export type PartialUpdateDashboardComponent = Partial<IDashboardComponent> & Pick<IDashboardComponent, 'id'>;

export type EntityResponseType = HttpResponse<IDashboardComponent>;
export type EntityArrayResponseType = HttpResponse<IDashboardComponent[]>;

@Injectable({ providedIn: 'root' })
export class DashboardComponentService extends ResilientService<IDashboardComponent, NewDashboardComponent> {
  protected http = inject(HttpClient);
  protected applicationConfigService = inject(ApplicationConfigService);

  private resourceUrl = this.applicationConfigService.getEndpointFor('api/dashboard-components');
  getResourceUrl(): string { return this.resourceUrl; }
  
  build(id: number): Observable<HttpResponse<Map<string, IDashboardComponentDetail[]>>> {
    // No convertion is needed
    return this.http.get<Map<string, IDashboardComponentDetail[]>>(`${this.getResourceUrl()}/build/${id}`, { observe: 'response' });
  }
  
  loadValuesForComponent(componentId: number, periodVersionId: number): Observable<HttpResponse<Map<string, IDashboardComponentDetailValue[]>>> {
    // No convertion is needed
    return this.http.get<Map<string, IDashboardComponentDetailValue[]>>(`${this.getResourceUrl()}/build/${componentId}/${periodVersionId}`, { observe: 'response' });
  }
  
  loadOrganizationValuesForComponent(organizationId: number, componentId: number, periodVersionId: number): Observable<HttpResponse<Map<string, IDashboardComponentDetailValue[]>>> {
    // No convertion is needed
    return this.http.get<Map<string, IDashboardComponentDetailValue[]>>(`${this.getResourceUrl()}/build/${organizationId}/${componentId}/${periodVersionId}`, { observe: 'response' });
  }
  
  getAllActive(): Observable<EntityArrayResponseType> {
    // No convertion is needed
    return this.http.get<IDashboardComponent[]>(`${this.getResourceUrl()}/active`, { observe: 'response' });  
  }
  
  getAllActiveForView(dashboardView: string): Observable<EntityArrayResponseType> {
    // No convertion is needed
    return this.http.get<IDashboardComponent[]>(`${this.getResourceUrl()}/active/${dashboardView}`, { observe: 'response' });  
  }

  export(organizationId: number, dashboardComponentId: number,  periodVersionId: number): Observable<Blob> {
    return this.http.get(`${this.resourceUrl}/export/${organizationId}/${dashboardComponentId}/${periodVersionId}`, { responseType: 'blob' });
  }

}
