import {
  ChartHelper, FilterType,
  Indicator, IndicatorFilterUtils,
  IndicatorPath, IndicatorPathType, IndicatorType,
  SourceType,
  Stakeholder,
  SubCategory,
  Topic, Visibility
} from "../openaireLibrary/monitor/entities/stakeholder";
import {AbstractControl, ValidatorFn, Validators} from "@angular/forms";
import {Option} from "../openaireLibrary/sharedComponents/input/input.component";
import {Session} from "../openaireLibrary/login/utils/helper.class";
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
import {properties} from "../../environments/environment";

export class StakeholderUtils {
  statuses: Option[] = [
    {value: 'PUBLIC', label: 'Public'},
    {value: 'RESTRICTED', label: 'Restricted'},
    {value: 'PRIVATE', label: 'Private'}
  ];
  
  types: Option[] = [
    {value: 'funder', label: 'Funder'},
    {value: 'ri', label: 'Reasearch Initiative'},
    {value: 'project', label: 'Project'},
    {value: 'organization', label: 'Organization'}
  ];
  
  visibility: Option[] = [
    {icon: 'world', value: "PUBLIC", label: 'Public'},
    {icon: 'lock', value: "RESTRICTED", label: 'Restricted'},
    {icon: 'users', value: "PRIVATE", label: 'Private'},
  ];
  

  visibilityIcon: Map<Visibility, string> = new Map<Visibility, string> ([
    ["PUBLIC", 'earth'],
    ["PRIVATE", 'lock'],
    ["RESTRICTED", 'group']
  ]);
  
  getTypesByUserRoles(user, id:string = null):Option[]{
    let types = [];
    for(let type of this.types){
      if(Session.isCurator(type.value, user)|| Session.isPortalAdministrator(user)|| (id && Session.isManager(type.value, id, user))){
        types.push(type);
      }
    }
    return types;
  }
  
  public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[]): Stakeholder {
    funder.topics = HelperFunctions.copy(defaultTopics);
    for (let topic of funder.topics) {
      // console.log('id:' + topic._id);
      topic.defaultId = topic._id;
      topic._id = null;
      // console.log('defaultId:' + topic.defaultId);
      for (let category of topic.categories) {
        category.defaultId = category._id;
        category._id = null;
        let subTokeep: SubCategory[] = [];
        for (let subCategory of category.subCategories) {
          subCategory.defaultId = subCategory._id;
          subCategory._id = null;
          subTokeep.push(subCategory);
          for (let section of subCategory.charts) {
            let chartsTokeep: Indicator[] = [];
            section.defaultId = section._id;
            section.stakeholderAlias = funder.alias;
            section._id = null;
            for (let indicator of section.indicators) {
              indicator.defaultId = indicator._id;
              indicator._id = null;
              chartsTokeep.push(indicator);
              for (let indicatorPath of indicator.indicatorPaths) {
                if (indicatorPath.parameters) {
                  Object.keys(indicatorPath.parameters).forEach(key => {
                    //TODO check before delete
                    /*if (indicatorPath.parameters[key].indexOf("_funder_name_") != -1) {
                      indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_name_", funder.index_name);
                    } else if (indicatorPath.parameters[key].indexOf("_funder_id_") != -1) {
                      indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_id_", funder.index_id);
                    } else if (indicatorPath.parameters[key].indexOf("_fsn_") != -1) {
                      indicatorPath.parameters[key] = indicatorPath.parameters[key].toString().replace("_fsn_", funder.index_shortName.toLowerCase());
                    }*/
                    if (key == "index_name") {
                      indicatorPath.parameters[key] = funder.index_name;
                    } else if (key == "index_id" ) {
                      indicatorPath.parameters[key] = funder.index_id;
                    }  else if (key == "index_shortName" ) {
                      indicatorPath.parameters[key] =  funder.index_shortName.toLowerCase();
                    }
                  });
                }
              }
            }
            section.indicators = chartsTokeep;
          }
          for (let section of subCategory.numbers) {
            section.defaultId = section._id;
            section.stakeholderAlias = funder.alias;
            section._id = null;
            for(let indicator of section.indicators) {
              indicator.defaultId = indicator._id;
              indicator._id = null;
              for (let indicatorPath of indicator.indicatorPaths) {
               /* indicatorPath.url = indicatorPath.url.replace("index_id", encodeURIComponent(funder.index_id));
                indicatorPath.url = indicatorPath.url.replace("index_name",  encodeURIComponent(funder.index_name));
                indicatorPath.url = indicatorPath.url.replace("index_shortName", encodeURIComponent(funder.index_shortName));*/
                // if(indicatorPath.parameters) {
                //   indicatorPath.parameters.forEach((value: string, key: string) => {
                //     if (value.indexOf("_funder_name_")!=-1) {
                //       indicatorPath.parameters.set(key,value.toString().replace("_funder_name_", funder.index_name));
                //     }else if (value.indexOf("_fsn_")!=-1) {
                //       indicatorPath.parameters.set(key,value.toString().replace("_fsn_", funder.index_shortName.toLowerCase()));
                //     }
                //   });
                // }
              }
            }
          }

        }
        category.subCategories = subTokeep;
      }
    }
    //console.log (funder);
    return funder;
  }
  
  aliasValidatorString(elements: string[]): ValidatorFn {
    return (control: AbstractControl): { [key: string]: string } | null => {
      if (control.value && elements.find(element =>
        element === control.value
      )) {
        return {'error': 'Alias already in use'};
      }
      return null;
    }
  }

  aliasValidator(elements: any[]): ValidatorFn {
    return (control: AbstractControl): { [key: string]: string } | null => {
      if (control.value && elements.find(element =>
        element.alias === control.value
      )) {
        return {'error': 'Alias already in use'};
      }
      return null;
    }
  }
  
  generateAlias(name: string): string {
    let alias = name.toLowerCase();
    while (alias.includes('/') || alias.includes(' ')) {
      alias = alias.replace(' / ', '-');
      alias = alias.replace('/', '-');
      alias = alias.replace(' ', '-');
    }
    return alias;
  }
}

export class IndicatorUtils {

  allChartTypes: Option[] = [
    {value: 'pie', label: 'Pie'},
    {value: 'table', label: 'Table'},
    {value: 'line', label: 'Line'},
    {value: 'column', label: 'Column'},
    {value: 'bar', label: 'Bar'},
    {value: 'other', label: 'Other'}
  ];
  basicChartTypes:IndicatorPathType[] =["pie", "line", "column", "bar"];
  defaultChartType:IndicatorPathType = "other";
  indicatorSizes: Option[] = [
    {value: 'small', label: 'Small(Enabled only for large screens)'},
    {value: 'medium', label: 'Medium'},
    {value: 'large', label: 'Large'}
  ];

  allSourceTypes: Option[]  = [
    {value: 'search', label: 'Search'},
    {value: 'statistics', label: 'Statistics'},
    {value: 'metrics', label: 'Metrics'},
    {value: 'stats-tool', label: 'Statistics tool'}
  ];
  sourceTypes: Option[]  = [
    {value: 'stats-tool', label: 'Statistics tool'}
  ];
  isPublic: Option[] = [
    {icon: 'public', value: true, label: 'Public'},
    {icon: 'lock', value: false, label: 'Private'},
  ];

  isActive: Option[] = [
    {icon: 'brightness_1', iconClass: '', value: true, label: 'Active'},
    {icon: 'brightness_1', value: false, label: 'Inactive'},
  ];

  chartTypesIcons: Map<string, string> = new Map([
    ['pie', 'pie_chart'],
    ['table', 'table_chart'],
    ['line', 'show_chart'],
    ['column', 'bar_chart'],
    ['bar', 'notes'],
    ['other', 'perm_media']
  ]);
  getChartTypes(initialType){
    let types: Option[]= [];
    if(this.basicChartTypes.indexOf(initialType) != -1){
      (this.allChartTypes).forEach(option => {
        if(this.basicChartTypes.indexOf(option.value)!=-1){
          types.push(option);
        }
      });
      return types;
    }else if(initialType == "table") {
      (this.allChartTypes).forEach(option => {
        if (initialType == option.value) {
          types.push(option);
        }
      });
      return types;
    }else {
      return this.allChartTypes;
    }
  }
  isPublicIcon: Map<boolean, string> = new Map([
    [true, 'public'],
    [false, 'lock']
  ]);

  isActiveIcon: string = 'brightness_1';

  ignoredParameters = ['index_name','index_id','index_shortName'];

  parametersValidators: Map<string, any> = new Map<string, any>([
    ['start_year', [Validators.required, Validators.pattern('^\\d+$')]],
    ['end_year', [Validators.required, Validators.pattern('^\\d+$')]]
  ]);

  public getFullUrl(stakeholder:Stakeholder, indicatorPath: IndicatorPath, fundingL0: string = null, startYear: string = null, endYear: string = null): string {
    let replacedUrl =indicatorPath.chartObject?indicatorPath.chartObject:indicatorPath.url;
    if (indicatorPath.parameters) {
      Object.keys(indicatorPath.parameters).forEach(key => {
        let replacedValue = indicatorPath.parameters[key];
        if (startYear && key == "start_year" && indicatorPath.filters["start_year"]) {
          replacedValue = (replacedValue < startYear) ? startYear : replacedValue;
        }
        if (endYear && key == "end_year" && indicatorPath.filters["end_year"]) {
          replacedValue = (replacedValue > endYear) ? endYear : replacedValue;
        }
        if (key == "index_id") {
          replacedValue = stakeholder.index_id;
        }
        if (key == "index_name") {
          replacedValue = stakeholder.index_name;
        }
        if (key == "index_shortName") {
          replacedValue = stakeholder.index_shortName.toLowerCase();
        }

        replacedUrl = replacedUrl.split(ChartHelper.prefix + key + ChartHelper.suffix).join(replacedValue)
      });
    }
    if (indicatorPath.chartObject) {
      if (fundingL0 && indicatorPath.filters["fundingL0"]) {
        let newJsonObject = JSON.parse(replacedUrl);
        for (let queries of this.getQueryObjectName(newJsonObject)?newJsonObject[this.getDescriptionObjectName(newJsonObject)][this.getQueryObjectName(newJsonObject)]:newJsonObject[this.getDescriptionObjectName(newJsonObject)]) {
          if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
            queries["query"]["filters"] = [];
          }
          //TODO check how it works if the query already has a filter
          queries["query"]["filters"].push(JSON.parse(indicatorPath.filters["fundingL0"].replace(ChartHelper.prefix + "fundingL0" + ChartHelper.suffix, fundingL0)));
        }
        replacedUrl = JSON.stringify(newJsonObject);
      }
      if (startYear && indicatorPath.filters["start_year"]) {
        let newJsonObject = JSON.parse(replacedUrl);

        for (let queries of this.getQueryObjectName(newJsonObject)?newJsonObject[this.getDescriptionObjectName(newJsonObject)][this.getQueryObjectName(newJsonObject)]:newJsonObject[this.getDescriptionObjectName(newJsonObject)]) {
          if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
            queries["query"]["filters"] = [];
          }
          //TODO check how it works if the query already has a filter
          queries["query"]["filters"].push(JSON.parse(indicatorPath.filters["start_year"].replace(ChartHelper.prefix + "start_year" + ChartHelper.suffix, startYear)));
        }
        replacedUrl = JSON.stringify(newJsonObject);
      }
      if (endYear && indicatorPath.filters["end_year"]) {
        let newJsonObject = JSON.parse(replacedUrl);
        for (let queries of this.getQueryObjectName(newJsonObject)?newJsonObject[this.getDescriptionObjectName(newJsonObject)][this.getQueryObjectName(newJsonObject)]:newJsonObject[this.getDescriptionObjectName(newJsonObject)]) {
          if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
            queries["query"]["filters"] = [];
          }
          //TODO check how it works if the query already has a filter
          queries["query"]["filters"].push(JSON.parse(indicatorPath.filters["end_year"].replace(ChartHelper.prefix + "end_year" + ChartHelper.suffix, endYear)));
        }
        replacedUrl = JSON.stringify(newJsonObject);
      }

    }
    //For numbers (e.g from stats-api , search service, etc)
    if(indicatorPath.url.indexOf(ChartHelper.prefix + 'index_id' + ChartHelper.suffix) !=- 1){
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_id' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_id))
    }
    if(indicatorPath.url.indexOf(ChartHelper.prefix + 'index_name' + ChartHelper.suffix) !=- 1){
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_name' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_name))
    }
    if(indicatorPath.url.indexOf(ChartHelper.prefix + 'index_shortName' + ChartHelper.suffix) !=- 1){
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_shortName' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_shortName))
    }
    return (indicatorPath.chartObject?indicatorPath.url + encodeURIComponent(replacedUrl):replacedUrl);
  }
  public getFullUrlWithFilters(stakeholder:Stakeholder, indicatorPath: IndicatorPath, fundingL0: string = null, startYear: string = null, endYear: string = null, coFunded:boolean=false): string {
    indicatorPath.filtersApplied = 0;
    let replacedUrl = indicatorPath.chartObject?indicatorPath.chartObject:indicatorPath.url;
    if (indicatorPath.parameters) {
      Object.keys(indicatorPath.parameters).forEach(key => {
        let replacedValue = indicatorPath.parameters[key];
        if (startYear && key == "start_year") {
          replacedValue = (replacedValue < startYear) ? startYear : replacedValue;
        }
        if (endYear && key == "end_year") {
          replacedValue = (replacedValue > endYear) ? endYear : replacedValue;
        }
        if (key == "index_id") {
          replacedValue = stakeholder.index_id;
        }
        if (key == "index_name") {
          replacedValue = stakeholder.index_name;
        }
        if (key == "index_shortName") {
          replacedValue = stakeholder.index_shortName.toLowerCase();
        }

        replacedUrl = replacedUrl.split(ChartHelper.prefix + key + ChartHelper.suffix).join(replacedValue)
      });
    }
    if (fundingL0) {
      if(indicatorPath.source == "stats-tool" && indicatorPath.chartObject) {
        let filterResults = this.addFilter(replacedUrl, 'fundingL0', fundingL0);
        replacedUrl = filterResults.url;
        indicatorPath.filtersApplied += filterResults.filtersApplied;
      }
    }
    if (startYear) {
      if(indicatorPath.source == "stats-tool" && indicatorPath.chartObject) {
        let filterResults =  this.addFilter(replacedUrl, 'start_year', startYear);
        replacedUrl = filterResults.url;
        indicatorPath.filtersApplied += filterResults.filtersApplied;
      }
    }
    if (endYear ) {
      if(indicatorPath.source == "stats-tool" && indicatorPath.chartObject) {
        let filterResults = this.addFilter(replacedUrl, 'end_year', endYear);
        replacedUrl = filterResults.url;
        indicatorPath.filtersApplied += filterResults.filtersApplied;
      }
    }
    if (coFunded ) {
      if(indicatorPath.source == "stats-tool" && indicatorPath.chartObject) {
        let filterResults = this.addFilter(replacedUrl, 'co-funded', endYear);
        replacedUrl = filterResults.url;
        indicatorPath.filtersApplied += filterResults.filtersApplied;
      }
    }

    //For numbers
    if (replacedUrl.indexOf(ChartHelper.prefix + 'index_id' + ChartHelper.suffix) != -1) {
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_id' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_id))
    }
    if (replacedUrl.indexOf(ChartHelper.prefix + 'index_name' + ChartHelper.suffix) != -1) {
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_name' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_name))
    }
    if (replacedUrl.indexOf(ChartHelper.prefix + 'index_shortName' + ChartHelper.suffix) != -1) {
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_shortName' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_shortName))
    }

    return (indicatorPath.chartObject?indicatorPath.url + encodeURIComponent(replacedUrl):replacedUrl);

  }
  private  addFilter(replacedUrl, filterType:FilterType, filterValue){
    let newJsonObject = JSON.parse(replacedUrl);
    let filterApplied:boolean = false;
    let queryIndex = 0;
    for (let queries of this.getQueryObjectName(newJsonObject)?newJsonObject[this.getDescriptionObjectName(newJsonObject)][this.getQueryObjectName(newJsonObject)]:newJsonObject[this.getDescriptionObjectName(newJsonObject)]) {

      if(queries["query"]["name"] && !queries["query"]["select"]){

        if(queries["query"]["name"].indexOf("monitor.")==-1 || !queries["query"]["parameters"]){
          continue;
        }
        if(filterType == 'fundingL0') {
          let paramFields = queries["query"]["name"].split(".").slice(3);
          let filterPosition = queries["query"]["name"].split(".").indexOf(filterType == "fundingL0" ? 'fl0' : filterType) ;
          if (filterPosition != -1) {
            //already filtered
            //TODO double check if we need to override if the fl0 is already filtered
            filterPosition -=3;
           /* //update the value
           if(paramFields.length == queries["query"]["parameters"].length ){
              //ok
              queries["query"]["parameters"][filterPosition] = filterValue;
            }else if((paramFields.length + 2) == queries["query"]["parameters"].length || (paramFields.length*2 + 4) == queries["query"]["parameters"].length){
              queries["query"]["parameters"][filterPosition + 2]=filterValue;
              filterApplied = true;
            }
            if((paramFields.length*2 + 4) == queries["query"]["parameters"].length){
                 queries["query"]["parameters"][(2* filterPosition) + 5]=filterValue;
            }*/
           //if applied with the same value mark as filtered
            if(paramFields.length == queries["query"]["parameters"].length &&  queries["query"]["parameters"][filterPosition] == filterValue){
              filterApplied = true;
            }else if((paramFields.length + 2) == queries["query"]["parameters"].length || (paramFields.length*2 + 4) == queries["query"]["parameters"].length && queries["query"]["parameters"][filterPosition + 2]==filterValue){
              filterApplied = true;
            }
          } else {
            // if((paramFields.length*2) == queries["query"]["parameters"].length){
            //   queries["query"]["parameters"].splice(paramFields.length, 0, filterValue);
            // }
            if((paramFields.length*2 + 4) == queries["query"]["parameters"].length){
              queries["query"]["parameters"].splice(paramFields.length + 1, 0, filterValue);
            }
            queries["query"]["name"] = queries["query"]["name"] + ".fl0";
            queries["query"]["parameters"].push(filterValue);
            filterApplied = true;
          }
        }else{
          let paramFields = queries["query"]["name"].split(".").slice(3);
          // console.debug("Field Params length:" + paramFields.length)
          // console.debug(paramFields)
          // console.debug("Parameters length:" + queries["query"]["parameters"].length)

          if((paramFields.length + 2) == queries["query"]["parameters"].length || (paramFields.length*2 + 4) == queries["query"]["parameters"].length){
            filterApplied = true;
           if(filterType == "start_year"){
             queries["query"]["parameters"][0] = parseInt(filterValue);
           }else if(filterType == "end_year"){
             queries["query"]["parameters"][1] = parseInt(filterValue);
           }
          }
          if((paramFields.length*2 + 4) == queries["query"]["parameters"].length){
            filterApplied = true;
            if(filterType == "start_year"){
              queries["query"]["parameters"][paramFields.length + 2] = parseInt(filterValue);
            }else if(filterType == "end_year"){
              queries["query"]["parameters"][paramFields.length + 3] = parseInt(filterValue);
            }
          }
        }
        // console.debug(queries["query"])
        // it is a name query
        continue;
      }
      if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
        queries["query"]["filters"] = [];
      }
      let field = queries["query"]["select"][0]["field"];
      let filterString =  IndicatorFilterUtils.getFilter(field&&field.length > 0? field.split(".")[0]:"",filterType);
      if(filterString){
        let filter = JSON.parse(filterString);
        let filterposition = IndicatorFilterUtils.filterIndexOf(filter,queries["query"]["filters"])
        if(filterposition){
          if(queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0] != filter['groupFilters'][0]["values"][0].replace(ChartHelper.prefix + filterType + ChartHelper.suffix, filterValue)){
            //change filter value
            // queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0] = filter['groupFilters'][0]["values"][0].replace(ChartHelper.prefix + filterType + ChartHelper.suffix, filterValue);
            //add user filter value
            // queries["query"]["filters"].push(JSON.parse(filterString.replace(ChartHelper.prefix + filterType + ChartHelper.suffix, filterValue)));
            // update colors
            //if noit a pie, map and chart has more than one query
            //
            if(!newJsonObject.hasOwnProperty("mapDescription") && queries["type"]!="pie" && this.isComparingChart(newJsonObject, filter)) {
              let activeColors = ["#7CB5EC", "#434348", "#8bbc21", "#910000", "#1aadce", "#492970", "#f28f43", "#77a1e5", "#c42525", "#a6c96a"];
              let inActiveColors = ["#E4EFFB", "#D8D8D9", "#8bbc21", "#910000", "#1aadce", "#492970", "#f28f43", "#77a1e5", "#c42525", "#a6c96a"];
              if (!newJsonObject[this.getDescriptionObjectName(newJsonObject)]["colors"]) {
                newJsonObject[this.getDescriptionObjectName(newJsonObject)]["colors"] = activeColors;
              }
              newJsonObject[this.getDescriptionObjectName(newJsonObject)]["colors"][queryIndex] = inActiveColors[queryIndex];
              filterApplied = true;
            }else if (filterType == "start_year" || filterType == "end_year") {
            //if has date filter already
               if(filterType == "start_year" && parseInt(filterValue) > parseInt(queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0])){
                 queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0] = filterValue;
               }else if(filterType == "end_year" && parseInt(filterValue) < parseInt(queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0])) {
                 queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0] = filterValue;
               }
              filterApplied = true;
            }
          }else{
            filterApplied = true;
          }
        }else {
          queries["query"]["filters"].push(JSON.parse(filterString.replace(ChartHelper.prefix + filterType + ChartHelper.suffix, filterValue)));
          filterApplied = true;
        }
      }
      queryIndex++;
    }
    return  { "url":JSON.stringify(newJsonObject), "filtersApplied":(filterApplied)?1:0};
  }

  isComparingChart(newJsonObject, filter,){
    let queriesCount = this.getQueryObjectName(newJsonObject)?newJsonObject[this.getDescriptionObjectName(newJsonObject)][this.getQueryObjectName(newJsonObject)].length:newJsonObject[this.getDescriptionObjectName(newJsonObject)].length;
    let values = [];
    if(queriesCount < 2){
      return false;
    }
    for (let queries of this.getQueryObjectName(newJsonObject)?newJsonObject[this.getDescriptionObjectName(newJsonObject)][this.getQueryObjectName(newJsonObject)]:newJsonObject[this.getDescriptionObjectName(newJsonObject)]) {
      let filterposition = IndicatorFilterUtils.filterIndexOf(filter, queries["query"]["filters"]);
      if (filterposition) {
        if (values.indexOf(queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0]) == -1) {
          values.push(queries["query"]["filters"][filterposition.filter]['groupFilters'][filterposition.groupFilter]["values"][0]);
        }
      }
    }
    // console.debug(values);
    return values.length > 1;
  }
  generateIndicatorByForm(form: any, indicatorPaths: IndicatorPath[], type:IndicatorType, addParameters:boolean = true ): Indicator {
    let indicator: Indicator = new Indicator(form.name, form.description, form.additionalDescription, type,
      form.width, form.height, form.visibility, indicatorPaths, form.defaultId);
    indicator._id = form._id;
    form.indicatorPaths.forEach((indicatorPath, index) => {
      indicator.indicatorPaths[index].type = indicatorPath.type;
      if(addParameters) {
        indicatorPath.parameters.forEach(parameter => {
          indicator.indicatorPaths[index].parameters[parameter.key] = parameter.value;
          if (parameter.key === 'type') {
            indicator.indicatorPaths[index].type = parameter.value;
          }
        });
      }
    });
    return indicator;
  }
  generateIndicatorByNumberUrl(source: SourceType, url: string, stakeholder:Stakeholder, jsonPath = [],  sourceServices:string[] =[] ): IndicatorPath {
    let indicatorPath = new IndicatorPath(null, source, url, null, jsonPath);
    if (source === 'stats-tool') {
      indicatorPath.url = url.split("json=")[0] + "json=";
      indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
      indicatorPath.chartObject = decodeURIComponent(url.indexOf("json=")!=-1?url.split("json=")[1]:"");
      let chart = JSON.parse(indicatorPath.chartObject);
      this.parameterizeDefaultQuery(chart, indicatorPath, stakeholder);
      this.extractStakeHolders(chart, indicatorPath, stakeholder);
      indicatorPath.chartObject = JSON.stringify(chart);
      indicatorPath.jsonPath = ["data","0","0","0"];
      // this.addResultFilters(chart, indicatorPath);
    }else {
      for( let service of sourceServices){
        if(url.indexOf(service)!=-1){
          url = url.split(service)[1] ;
        }
      }
      try {
        if (url.indexOf(encodeURIComponent(stakeholder.index_id)) !== -1) {
          url = url.split(encodeURIComponent(stakeholder.index_id)).join(ChartHelper.prefix + "index_id" + ChartHelper.suffix);
        }
        if (url.indexOf(encodeURIComponent(stakeholder.index_name)) !== -1) {
          url = url.split(encodeURIComponent(stakeholder.index_name)).join(ChartHelper.prefix + "index_name" + ChartHelper.suffix);
        }
        if (url.indexOf(encodeURIComponent(stakeholder.index_shortName)) !== -1) {
          url = url.split(encodeURIComponent(stakeholder.index_shortName)).join(ChartHelper.prefix + "index_shortName" + ChartHelper.suffix);
        }
        indicatorPath.url = url;
      } catch (e) {
        console.error(e);
      }
    }
    return indicatorPath;
  }
  generateIndicatorByChartUrl(source: SourceType, url: string, type: IndicatorPathType = null, stakeholder:Stakeholder): IndicatorPath {
    let indicatorPath = new IndicatorPath(type, source, null, null, []);
    try {
      if (source === 'stats-tool') {
        indicatorPath.url = url.split("json=")[0] + "json=";
        indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
        indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
        let chart = JSON.parse(indicatorPath.chartObject);
        // console.debug(indicatorPath);
        if (indicatorPath.url == "chart?json=") {

          if (chart["library"] && (chart["library"] == "HighCharts" || chart["library"] == "eCharts" || chart["library"] == "HighMaps" )) {
            indicatorPath.type = this.extractType(chart, indicatorPath);
          } else {
            indicatorPath.type = this.defaultChartType;
          }

          this.extractTitle(chart, indicatorPath);
          this.extractSubTitle(chart, indicatorPath);
          this.extractXTitle(chart, indicatorPath);
          this.extractYTitle(chart, indicatorPath);
        }else if(indicatorPath.url == "table?json="){
          indicatorPath.type = "table";
        }
        if (indicatorPath.url == "chart?json=" || indicatorPath.url == "table?json=") {
          // common for tables and other chart types
          this.extractDataTitle(chart, indicatorPath);
          this.parameterizeDefaultQuery(chart, indicatorPath, stakeholder);
          this.extractStakeHolders(chart, indicatorPath, stakeholder);
          this.extractStartYear(chart, indicatorPath);
          this.extractEndYear(chart, indicatorPath);
          indicatorPath.chartObject = JSON.stringify(chart);
        }
      } else if (source === 'old') {
        indicatorPath.url = url.split("data=")[0].split("/stats/")[1] + "data=";
        indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1].split("&")[0]);
        indicatorPath.type = type;
        let chart = JSON.parse(indicatorPath.chartObject);
        this.extractOldToolTitle(chart, indicatorPath);
        this.extractOldToolXTitle(chart, indicatorPath);
        this.extractOldToolYTitle(chart, indicatorPath);
        indicatorPath.chartObject = JSON.stringify(chart);
      } else {
        indicatorPath.url = url;
        indicatorPath.type = type;
      }
    }catch(e){
      console.error(e);
      indicatorPath.url = url;
      indicatorPath.type = type;
    }
    // console.debug(indicatorPath.parameters);
    // console.debug(indicatorPath.chartObject);
    if(indicatorPath.type == null){
      indicatorPath.type = this.defaultChartType;
    }
    return indicatorPath;
  }
  private getQueryObjectName(obj){
    if((obj[this.getDescriptionObjectName(obj)]).hasOwnProperty("queriesInfo")){
        return "queriesInfo";
    }else if((obj[this.getDescriptionObjectName(obj)]).hasOwnProperty("queries")) {
      return "queries";
    }
  }
  private getDescriptionObjectName(obj){
    if(obj.hasOwnProperty("mapDescription")){
      return "mapDescription";
    }else if(obj.hasOwnProperty("chartDescription")) {
      return "chartDescription";
    }else if(obj.hasOwnProperty("tableDescription") ){
      return "tableDescription";
    }else if(obj.hasOwnProperty("series") ){
      return "series";
    }
    }
  private extractType(obj, indicatorPath: IndicatorPath): IndicatorPathType {
    let type = (obj[this.getDescriptionObjectName(obj)] && obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)][0]["type"])?obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)][0]["type"]:"";
    if (this.basicChartTypes.indexOf(type) == -1) {
      type = this.defaultChartType;
    } else {
      obj[this.getDescriptionObjectName(obj)]["queries"][0]["type"] = ChartHelper.prefix + "type" + ChartHelper.suffix;
      indicatorPath.parameters['type'] = type;
    }
    return type;
  }
  private extractStakeHolders(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
    this.extractFunder(obj, indicatorPath, stakeholder);
    this.extractRI(obj, indicatorPath, stakeholder);
    this.extractOrganization(obj, indicatorPath, stakeholder);
  }
  private extractFunder(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
    if(stakeholder.type != "funder"){
      return;
    }
    for (let query of this.getQueryObjectName(obj)?obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]:obj[this.getDescriptionObjectName(obj)]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["query"]["filters"]) {
        if (filter["groupFilters"][0]["field"].indexOf(" funder") != -1) {//new statistcs schema
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
        }else if (filter["groupFilters"][0]["field"].indexOf(".funder") != -1) {
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
        }else  if (filter["groupFilters"][0]["field"].indexOf(".funder.id") != -1) {
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName;
        }
      }
    }
  }

  private extractRI(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
    if(stakeholder.type != "ri"){
      return;
    }
    for (let query of this.getQueryObjectName(obj)?obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]:obj[this.getDescriptionObjectName(obj)]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["query"]["filters"]) {
        if (filter["groupFilters"][0]["field"].indexOf(".context.name") != -1) {
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
        }else  if (filter["groupFilters"][0]["field"].indexOf(".context.id") != -1) {
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName;
        }
      }
    }
  }

  private extractOrganization(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
    // works for publication.project.organization.name
    // and publication.organization.name
    if(stakeholder.type != "organization"){
      return;
    }
    for (let query of this.getQueryObjectName(obj)?obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]:obj[this.getDescriptionObjectName(obj)]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["query"]["filters"]) {
        if (filter["groupFilters"][0]["field"].indexOf(".organization.name") != -1) {
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
        }else  if (filter["groupFilters"][0]["field"].indexOf(".organization.id") != -1) {
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName;
        }
      }
    }
  }
  private extractStartYear(obj, indicatorPath: IndicatorPath) {
    let start_year;
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["query"]["filters"]) {
        for (let gfilter of filter["groupFilters"]) {
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
            start_year = gfilter["values"][0];
            gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
            indicatorPath.parameters["start_year"] = start_year;
          }
        }
      }
    }
  }

  private extractEndYear(obj, indicatorPath: IndicatorPath) {
    let end_year;
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
      if (!query["query"]["filters"]) {
        return;
      }
      for (let filter of query["query"]["filters"]) {
        for (let gfilter of filter["groupFilters"]) {
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
            end_year = gfilter["values"][0];
            gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
            indicatorPath.parameters["end_year"] = end_year;
          }
        }
      }
    }
  }

  private parameterizeDefaultQuery(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
    let name = "";
    for (let query of this.getQueryObjectName(obj)?obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]:obj[this.getDescriptionObjectName(obj)]) {
      //monitor.{{stakeholderType}}.{{queryname}}
      //parameters: stakeholderId*, type
      if (query["query"]["name"]) {
        name = query["query"]["name"];
        let parameters = (query["query"]["parameters"])?query["query"]["parameters"]:[];
        if(name.split('.')[0] == "rcd" && parameters.length > 0  && stakeholder.type=="ri") {
          //rcd.{{queryname}}
          parameters[0] = ChartHelper.prefix + "index_id" + ChartHelper.suffix;
          indicatorPath.parameters["index_id"] = stakeholder.index_id;
        }else if(name.split('.')[0] == "monitor" && parameters.length == 0 && stakeholder.type=="funder"){
          // old saved queries without params
          //monitor.{{funder_shortName}}.{{type}}.{{queryname}}
          let stakeholderSN = name.split('.')[1];
          query["query"]["name"] = name.split('.' + stakeholderSN + ".")[0] + "." + ChartHelper.prefix + "index_shortName" + ChartHelper.suffix +"." + name.split('.' + stakeholderSN + ".")[1];
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName.toLowerCase();
        }else if(name.split('.')[0] == "monitor" && parameters.length > 0 && name.split('.')[1]  == stakeholder.type) {
          // new parameterized queries
          //monitor.{{type}}.{{queryname}}.{{param1 - id }}.{{param2 result-type}}.{{fl0}} --> params [start year, end year, id, result type, fl0]

          let index = (name.split('.').slice(3).length +2 == parameters.length)?[2]:((name.split('.').slice(3).length * 2 + 4 == parameters.length)?[2,name.split('.').slice(3).length+4]:[0]);
          for(let i of index) {
            if (name.split('.').length > 3 && name.split('.')[3] == "id") {
              parameters[i] = ChartHelper.prefix + "index_id" + ChartHelper.suffix;
              indicatorPath.parameters["index_id"] = stakeholder.index_id;
            } else if (name.split('.').length > 3 && name.split('.')[3] == "shortname") {
              parameters[i] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
              indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName.toLowerCase();
            } else if (name.split('.').length > 3 && name.split('.')[3] == "name") {
              parameters[i] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
              indicatorPath.parameters["index_name"] = stakeholder.index_name;
            }
          }
        }
      }
    }
  }
  private extractDataTitle(obj, indicatorPath: IndicatorPath) {
    let index = 0;
    if(!obj[this.getDescriptionObjectName(obj)] || !obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]){
      return;
    }
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
      if (query["name"]) {
        let name = query["name"];
        query["name"] =  ChartHelper.prefix + "data_title_"+index + ChartHelper.suffix;
        indicatorPath.parameters["data_title_"+index] = name;
      }
      index++;
    }
  }
  private extractTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj[this.getDescriptionObjectName(obj)]["title"]) {
      title = obj[this.getDescriptionObjectName(obj)]["title"]["text"];
      obj[this.getDescriptionObjectName(obj)]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
    }else if (obj[this.getDescriptionObjectName(obj)]["options"] && obj[this.getDescriptionObjectName(obj)]["options"]["title"]) {
      title = obj[this.getDescriptionObjectName(obj)]["options"]["title"];
      obj[this.getDescriptionObjectName(obj)]["options"]["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
    }
    indicatorPath.parameters["title"] = title ? title : "";
  }

  private extractSubTitle(obj, indicatorPath: IndicatorPath) {
    let subtitle = "";
    if (obj[this.getDescriptionObjectName(obj)]["subtitle"]) {
      subtitle = obj[this.getDescriptionObjectName(obj)]["subtitle"]["text"];
      obj[this.getDescriptionObjectName(obj)]["subtitle"]["text"] = ChartHelper.prefix + "subtitle" + ChartHelper.suffix;
      indicatorPath.parameters["subtitle"] = subtitle ? subtitle : "";
    }else if (obj[this.getDescriptionObjectName(obj)]["title"] && obj[this.getDescriptionObjectName(obj)]["title"] && obj[this.getDescriptionObjectName(obj)]["title"]["subtext"]) {
      subtitle = obj[this.getDescriptionObjectName(obj)]["title"]["subtext"];
      obj[this.getDescriptionObjectName(obj)]["title"]["subtext"] = ChartHelper.prefix + "subtitle" + ChartHelper.suffix;
      indicatorPath.parameters["subtitle"] = subtitle ? subtitle : "";
    }
  }

  private extractXTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj[this.getDescriptionObjectName(obj)]["xAxis"] && obj[this.getDescriptionObjectName(obj)]["xAxis"]["title"]) {
      title = obj[this.getDescriptionObjectName(obj)]["xAxis"]["title"]["text"];
      obj[this.getDescriptionObjectName(obj)]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
    }else if (obj[this.getDescriptionObjectName(obj)]["options"] && obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"] && obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"]["title"]) {
      title = obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"]["title"];
      obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"]["title"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
    }else if (obj[this.getDescriptionObjectName(obj)]["xAxis"] && obj[this.getDescriptionObjectName(obj)]["xAxis"]["name"]) {
      title = obj[this.getDescriptionObjectName(obj)]["xAxis"]["name"];
      obj[this.getDescriptionObjectName(obj)]["xAxis"]["name"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
    }
    indicatorPath.parameters["xAxisTitle"] = title ? title : "";
  }

  private extractYTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj[this.getDescriptionObjectName(obj)]["yAxis"] && obj[this.getDescriptionObjectName(obj)]["yAxis"]["title"] ) {
      title = obj[this.getDescriptionObjectName(obj)]["yAxis"]["title"]["text"];
      obj[this.getDescriptionObjectName(obj)]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
    }else if (obj[this.getDescriptionObjectName(obj)]["options"]&& obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"] && obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"]["title"]) {
      title = obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"]["title"];
      obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"]["title"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
    }else if (obj[this.getDescriptionObjectName(obj)]["yAxis"] && obj[this.getDescriptionObjectName(obj)]["yAxis"]["name"]) {
      title = obj[this.getDescriptionObjectName(obj)]["yAxis"]["name"];
      obj[this.getDescriptionObjectName(obj)]["yAxis"]["name"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
    }
    indicatorPath.parameters["yAxisTitle"] = title ? title : "";
  }

  private extractOldToolTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["title"]) {
      title = obj["title"];
      obj["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
      indicatorPath.parameters["title"] = title;

    }
  }

  private extractOldToolXTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["xaxistitle"]) {
      title = obj["xaxistitle"];
      obj["xaxistitle"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
      indicatorPath.parameters["xAxisTitle"] = title;
    }
  }

  private extractOldToolYTitle(obj, indicatorPath: IndicatorPath) {
    let title = "";
    if (obj["fieldsheaders"]) {
      title = Array.isArray(obj["fieldsheaders"]) ? obj["fieldsheaders"][0] : obj["fieldsheaders"];
      if (Array.isArray(obj["fieldsheaders"])) {
        obj["fieldsheaders"][0] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
      } else {
        obj["fieldsheaders"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
      }
      indicatorPath.parameters["yAxisTitle"] = title;
    }
  }

  public checkForSchemaEnhancements(url:string):boolean{
    return url !=this.applySchemaEnhancements(url);
  }
  public applySchemaEnhancements(url:string):string{
    let resultEnhancements = [
      [".project.acronym",".project acronym"],
      [".project.title",".project title"],
      [".project.funder",".project funder"],
      [".project.funding level 0",".project funding level 0"],
      [".datasource.name",".HostedBy datasource"],
      [".datasource.type",".HostedBy datasource type"]
    ];
    let changes = "";
  for (let field of resultEnhancements) {
    for (let type of ["publication", "software", "dataset", "other", "result"]) {
      if (url.indexOf(encodeURIComponent(type + field[0])) != -1) {
        changes += "Changed  " + type + field[0] + " to " + type + field[1] + "\n";
        url = url.split(encodeURIComponent(type + field[0])).join(encodeURIComponent(type + field[1]));
      }
    }
  }
    console.debug(changes);
    console.debug(url);
    return url;
  }
}
