import {Component, Input} from '@angular/core';
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {CuratorService} from "../openaireLibrary/connect/curators/curator.service";
import {Curator} from "../openaireLibrary/utils/entities/CuratorInfo";
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 {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
import {StringUtils} from "../openaireLibrary/utils/string-utils.class";
import {Subscription} from "rxjs";
import {properties} from "../../environments/environment";
import {UserRegistryService} from "../openaireLibrary/services/user-registry.service";

@Component({
  selector: 'curators',
  templateUrl: './curators.component.html'
  
})
export class CuratorsComponent {
  @Input() longView = true;
  communityId = null;
  public downloadUrl = null;
  public showLoading = true;
  
  public curators: Curator[] = [];
  
  public curatorsLimit: number = 5;
  public numberOfCurators: number = 5;
  
  public showMore = [];
  public maxCharacters = 500;
  
  public properties: EnvProperties;
  public pageContents = null;
  public divContents = null;
  
  public url: string = null;
  public pageTitle: string = "Curators";
  
  public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'About - curators'}];
  
  subs: Subscription[] = [];
  
  constructor(private route: ActivatedRoute,
              private curatorsService: CuratorService,
              private communityService: CommunityService,
              private userRegistryService: UserRegistryService,
              private _router: Router,
              private helper: HelperService,
              private _meta: Meta,
              private _title: Title,
              private seoService: SEOService,
              private _piwikService: PiwikService) {
  }
  
  ngOnInit() {
    this.showLoading = true;
    this.properties = properties;
    this.downloadUrl = this.properties.utilsService + '/download/';
    //if (properties.environment !== 'development') {
    if (!this.longView) {
      this.subs.push(this.communityService.getCommunityAsObservable().subscribe(community => {
        if (community) {
          this.communityId = community.communityId;
          this.getCurators();
        }
      }));
    } else {
      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.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.getDivContents();
          this.getPageContents();
          this.getCurators();
        }
      }));
    }
  }
  
  ngOnDestroy() {
    for (let sub of this.subs) {
      sub.unsubscribe();
    }
  }
  
  private getCurators() {
    this.subs.push(this.curatorsService.getCurators(this.properties, this.communityId).subscribe(curators => {
      this.curators = curators;
      for (let i = 0; i < this.curators.length; i++) {
        this.showMore[i] = false;
      }
      this.showLoading = false;
    }));
  }
  
  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;
    }));
  }
  
  public toggle(index: number) {
    this.showMore[index] = !this.showMore[index];
  }
  
  _format(name: string) {
    if (name) {
      return (((name).length > this.maxCharacters) ? (name.substring(0, (this.maxCharacters - ('...').length)) + '...') : name);
    } else {
      return null;
    }
  }
  
  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'");
  }
}
