import {Component} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {StakeholderService} from "../../../openaireLibrary/monitor/services/stakeholder.service";
import {Subscriber} from "rxjs";
import {ConfigurationService} from "../../../openaireLibrary/utils/configuration/configuration.service";

@Component({
  selector: 'monitor-publication',
  template: `
    <div id="page_content">
      <result-landing *ngIf="initialized" type="publication" [communityId]="communityId" [piwikSiteId]="piwikSiteId"></result-landing>
    </div>
  `,
})
export class MonitorPublicationComponent {
  initialized: boolean = false;
  communityId;
  piwikSiteId;
  constructor(private route: ActivatedRoute,
              private router: Router,
              private stakeholderService: StakeholderService,  private configurationService: ConfigurationService) {
  }
  subscriptions = [];
  ngOnDestroy() {
    this.subscriptions.forEach(subscription => {
      if (subscription instanceof Subscriber) {
        subscription.unsubscribe();
      }
    });
  }
  ngOnInit() {
    this.subscriptions.push(this.route.params.subscribe(params => {
      if (params['stakeholder']) {
        this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
          if (stakeholder) {
            this.initialized = true;
            this.communityId = stakeholder.alias;
            this.subscriptions.push(this.configurationService.communityInformationState.subscribe(portal => {
              if (portal) {
                console.debug(portal)
                this.piwikSiteId = portal.piwik;
              }
            }));
          }
        }));
      }
    }));
  }
}
