import {Component, Input} from '@angular/core';
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {ActivatedRoute, Router} from "@angular/router";
import {CommunityService} from "../openaireLibrary/connect/community/community.service";
import {ConnectHelper} from "../openaireLibrary/connect/connectHelper";
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
import {Meta, Title} from "@angular/platform-browser";
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 {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
import {properties} from "../../environments/environment";
import {Subscriber, Subscription} from "rxjs";

@Component({
  selector: 'subjects',
  template: `
    <schema2jsonld *ngIf="url" [URL]="url" [name]="pageTitle" type="other"></schema2jsonld>
    <div style=" min-height: 650px;" class="">
      <div class="uk-section uk-padding-remove-top uk-padding-remove-bottom">
        <breadcrumbs addClass="uk-margin-large-left uk-margin-remove-bottom uk-margin-small-top"
                     [breadcrumbs]="breadcrumbs"></breadcrumbs>
        <div class="uk-container uk-container-large">
          <div *ngIf="showLoading">
            <div class="uk-animation-fade uk-width-1-1" role="alert"><span
                class="loading-gif  uk-align-center"></span></div>
          </div>
          <div *ngIf="!showLoading">
            <h4 class="uk-margin-bottom uk-margin-medium-top">
              <span>Subjects</span>
            </h4>

            <div style=" min-height: 250px;" class="white-box-with-border uk-padding uk-list">
              <helper *ngIf="pageContents && pageContents['top'] && pageContents['top'].length > 0"
                      [texts]="pageContents['top']"></helper>

              <span *ngFor="let subject of subjects let i=index">
                      <span *ngIf="subject != ''">
                        <a class="portal-link"
                           [queryParams]="{f0:'resultsubject',fv0:createParams(subject)}"
                           routerLinkActive="router-link-active" [routerLink]="properties.searchLinkToAdvancedResults">
                          <span>{{subject}}</span>
                        </a>
                        <span *ngIf="i < subjects.length-1">, </span>
                      </span>
                  </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  `
})
export class SubjectsComponent {
  public subjects: string[];
  public communityId = null;
  public showLoading = true;
  
  public properties: EnvProperties = properties;
  public pageContents = null;
  public divContents = null;
  
  public url: string = null;
  public pageTitle: string = "Subjects";
  
  public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'about - subjects'}];
  
  subs: Subscription[] = [];
  
  constructor(private route: ActivatedRoute,
              private communityService: CommunityService,
              private _router: Router,
              private helper: HelperService,
              private _meta: Meta,
              private _title: Title,
              private seoService: SEOService,
              private _piwikService: PiwikService) {
  }
  
  ngOnInit() {
    this.showLoading = true;
    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");
    this.subs.push(this.communityService.getCommunityAsObservable().subscribe(community => {
      if (community) {
        this.communityId = community.communityId;
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
          this.subs.push(this._piwikService.trackView(this.properties, this.pageTitle, PiwikHelper.getSiteId(this.communityId, this.properties.environment)).subscribe());
        }
        //this.getDivContents();
        this.getPageContents();
        this.subjects = community.subjects;
        this.showLoading = false;
      }
    }));
  }
  
  createParams(param) {
    return StringUtils.quote(StringUtils.URIEncode(param));
  }
  
  ngOnDestroy() {
    for (let sub of this.subs) {
      if (sub instanceof Subscriber) {
        sub.unsubscribe();
      }
    }
  }
  
  private getPageContents() {
    this.subs.push(this.helper.getPageHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => {
      this.pageContents = contents;
    }));
  }
  
  private getDivContents() {
    this.subs.push(this.helper.getDivHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => {
      this.divContents = contents;
    }));
  }
  
  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'");
  }
}
