import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
import {SourceType} from "../../openaireLibrary/monitor/entities/stakeholder";
import {properties} from "../../../environments/environment";


@Injectable({
    providedIn: 'root'
})
export class StatisticsService {

  numberSources: Map<SourceType, string[]> = new Map<SourceType, string[]>();
  chartSources: Map<SourceType, string[]> = new Map<SourceType, string[]>();

  constructor(private http:HttpClient) {
      this.numberSources.set('statistics', [properties.statisticsAPIURL]);
      this.numberSources.set('search', [properties.searchAPIURLLAst]);
      this.numberSources.set('metrics', [properties.metricsAPIURL]);
      this.numberSources.set('stats-tool', [properties.monitorStatsFrameUrl, "http://marilyn.athenarc.gr:8080/stats-api/", "http://88.197.53.71:8080/stats-api/", "https://stats.madgik.di.uoa.gr/stats-api/","https://beta.services.openaire.eu/stats-tool/","https://services.openaire.eu/stats-tool/"]);
      this.chartSources.set('stats-tool', [properties.monitorStatsFrameUrl, "http://marilyn.athenarc.gr:8080/stats-api/", "http://88.197.53.71:8080/stats-api/", "https://stats.madgik.di.uoa.gr/stats-api/","https://beta.services.openaire.eu/stats-tool/","https://services.openaire.eu/stats-tool/"]);
      this.chartSources.set('old', [properties.statisticsFrameAPIURL]);
      this.chartSources.set('metrics', [properties.metricsAPIURL]);
      this.chartSources.set('image', [""]);
  }
    getSourceType(source:string):SourceType{
      let sourceType: SourceType = 'search';
      this.numberSources.forEach((values, key) => {
        if(key == source) {
          sourceType = key;

        }
      });
      return sourceType;
    }
    
    getNumbers(source: SourceType, url: string): Observable<any> {
        if(source !== null) {
          return this.http.get<any>(this.numberSources.get(source)[0] + url);
        } else {
          return this.http.get<any>(url);
        }
    }

    getChartUrl(source: SourceType, url: string): string {
        return this.chartSources.get(source)[0] + url;
    }
    getNumberUrl(source: string, url: string): string {
      return this.numberSources.get(this.getSourceType(source))[0] + url;
    }
    getNumberSource(url: string): SourceType {
      let source: SourceType = 'search';
      this.numberSources.forEach((values, key) => {
        values.forEach((value) => {
          if(value !== '' && url.indexOf(value) !== -1) {
            source = key;
          }
        });
      });
      return source;
    }
    getChartSource(url: string): SourceType {
        let source: SourceType = 'image';
        this.chartSources.forEach((values, key) => {
          values.forEach((value) => {
            if(value !== '' && url.indexOf(value) !== -1) {
                source = key;
            }
          });
        });
        return source;
    }
}
