import {Component, Input}       from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Title, Meta}            from '@angular/platform-browser';
import {EnvProperties}          from '../openaireLibrary/utils/properties/env-properties';
import {AffiliationService}     from "../openaireLibrary/connect/affiliations/affiliation.service";
import {Affiliation}            from "../openaireLibrary/utils/entities/CuratorInfo";
import {ConnectHelper}          from "../openaireLibrary/connect/connectHelper";
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
import {PiwikService} from "../openaireLibrary/utils/piwik/piwik.service";
import {PiwikHelper} from "../utils/piwikHelper";
import {StringUtils} from '../openaireLibrary/utils/string-utils.class';
import {Subscriber} from "rxjs";
import {properties} from "../../environments/environment";

@Component({
  selector: 'affiliations',
  templateUrl: './affiliations.component.html'
})
export class AffiliationsComponent {
  @Input() getAffiliationsFromAPI: boolean = false;
  @Input() longView: boolean = false;
  @Input() communityFirstPage: boolean = false;
  @Input() affiliationsInSlider: number = 5;
  @Input() affiliations: Affiliation[] = [];
  @Input() sliderOptions = '';
  @Input() arrows = true;

  public showLoading: boolean = false;

  communityId: string;
  properties:EnvProperties;

  subscriptions = [];
  public url: string = null;
  public pageTitle: string = "Related Organizations";

  constructor ( private  route: ActivatedRoute, private _router: Router,
                private _meta: Meta,
                private _title: Title,
                private seoService: SEOService,
                private _piwikService: PiwikService,
                private affiliationService: AffiliationService) {}

  public ngOnInit() {

    this.subscriptions.push(this.route.queryParams.subscribe(
      communityId => {
        this.communityId  = ConnectHelper.getCommunityFromDomain(properties.domain);
        if(!this.communityId) {
          this.communityId = communityId['communityId'];
        }

        this.properties = properties;
        if(this.longView) {
          if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
            this.subscriptions.push(this._piwikService.trackView(this.properties, this.pageTitle, PiwikHelper.getSiteId(this.communityId, this.properties.environment)).subscribe());
          }
          this.url = this.properties.domain + this._router.url;
          this.seoService.createLinkForCanonicalURL(this.url);
          this.updateUrl(this.url);
          this.updateTitle(this.pageTitle);
          this.updateDescription("OpenAIRE - Connect, Community Gateway, research community, organizations");
        }
        if(this.getAffiliationsFromAPI) {
          this.showLoading = true;
          this.affiliationService.initAffiliations(this.communityId);
          this.subscriptions.push(this.affiliationService.affiliations.subscribe(
            affiliations => {
              this.affiliations = affiliations;
              this.showLoading = false;
            },
            error => {
              console.error("Affiliations Component: Error getting affiliations for community with id: " + this.communityId, error);
              this.showLoading = false;
            }
          ));
        }
      }));

  }

  ngOnDestroy() {
    this.subscriptions.forEach(subscription => {
      if (subscription instanceof Subscriber) {
        subscription.unsubscribe();
      }
    });
    this.affiliationService.clearSubscriptions();
  }
  
  private updateDescription(description: string) {
    this._meta.updateTag({content: description}, "name='description'");
    this._meta.updateTag({content: description}, "property='og:description'");
  }

  private updateTitle(title: string) {
    var _title = ((title.length > 50) ? title.substring(0, 50) : title);
    this._title.setTitle(_title);
    this._meta.updateTag({content: _title}, "property='og:title'");
  }

  private updateUrl(url: string) {
    this._meta.updateTag({content: url}, "property='og:url'");
  }
}
