import {Component} from '@angular/core';
import {SearchCustomFilter} from "../../../openaireLibrary/searchPages/searchUtils/searchUtils.class";
import {ActivatedRoute, Router} from "@angular/router";
import {StakeholderService} from "../../../openaireLibrary/monitor/services/stakeholder.service";

@Component({
  selector: 'monitor-advanced-search-projects',
  template: `
    <div id="page_content">
      <search-projects *ngIf="initialized" [simpleView]="false"
                       [customFilter]=customFilter [hasPrefix]="false"
                       [includeOnlyResultsAndFilter]="false" [showSwitchSearchLink]="showSwitchSearchLink">
      </search-projects>
    </div>
  `
  
})
export class MonitorAdvancedSearchProjectsComponent {
  customFilter: SearchCustomFilter = null;
  initialized: boolean = false;
  showSwitchSearchLink:boolean = false;
  constructor(private route: ActivatedRoute,
              private router: Router,
              private stakeholderService: StakeholderService) {
  }
  
  ngOnInit() {
    this.route.params.subscribe(params => {
      if (params['stakeholder']) {
        this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
          if (stakeholder) {
            if (stakeholder.type === "funder") {
              let value = stakeholder.index_id+"||"+stakeholder.index_name+"||"+stakeholder.index_shortName;
              this.customFilter = new SearchCustomFilter("Funder", "funder", value, "");
              this.customFilter.isHiddenFilter = false;
              this.customFilter.promptToAddFilter = true;
              this.showSwitchSearchLink = true;
            } else if (stakeholder.type === "organization") {
              let value = stakeholder.index_id;
              this.customFilter = new SearchCustomFilter("Organization", "relorganizationid", value, "");
              this.customFilter.isHiddenFilter = false;
              this.customFilter.promptToAddFilter = true;
              this.showSwitchSearchLink = true;
            } else if (stakeholder.type === "ri") {
              let value =  stakeholder.index_id+"||"+stakeholder.index_name;
              this.customFilter = new SearchCustomFilter("Community", "community", value,  stakeholder.name);
              this.customFilter.isHiddenFilter = false;
            }
            this.initialized = true;
          }
        });
      }
    });
  }
  
  navigateToError() {
    this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
  }
}
