import {Component, OnDestroy, OnInit} from "@angular/core";
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
import {Stakeholder} from "../openaireLibrary/monitor/entities/stakeholder";
import {Subscription} from "rxjs";
import {Meta, Title} from "@angular/platform-browser";
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
import {properties} from "../../environments/environment";
import {ActivatedRoute, Router} from "@angular/router";

@Component({
  selector: 'methodology',
  template: `
    <div page-content>
      <div inner>
        <div class="uk-container uk-container-large uk-section-small">
          <h2 class="uk-text-center">
            Terminology and construction
          </h2>
          <div class="uk-margin-large-top">
            <ul class="uk-tab admin customTabs uk-margin-medium-bottom" uk-tab>
              <li [class.uk-active]="tab === 'entities'"><a (click)="tab = 'entities'"><span
                  class="title">Entities</span></a></li>
              <li [class.uk-active]="tab === 'attributes'"><a (click)="tab = 'attributes'"><span class="title">Attributes of Entities</span></a>
              </li>
            </ul>
            <table *ngIf="tab === 'entities'">
              <tr>
                <th class="important">Research Outcomes</th>
                <td>
                  <div>There are currently four different types of research outcomes in the OpenAIRE Research
                    Graph:
                  </div>
                  <ul class="portal-circle">
                    <li>Publication</li>
                    <li>Dataset</li>
                    <li>Software</li>
                    <li>Other Research Product</li>
                  </ul>
                  <div class="uk-margin-small-top">
                    OpenAIRE deduplicates (merges) different records of research outcomes and keeps the metadata of all
                    instances.
                  </div>
                </td>
              </tr>
              <tr>
                <th>Publication</th>
                <td>Research outcomes intended for human reading (published articles, pre-prints, conference papers,
                  presentations, technical reports, etc.)
                </td>
              </tr>
              <tr>
                <th>Dataset</th>
                <td>
                  <p>Research data</p>
                  <p>Granularity is not defined by OpenAIRE, it reflects the granularity supported by
                    the sources from which the description of the dataset has been collected.</p>
                </td>
              </tr>
              <tr>
                <th>Software</th>
                <td>Source code or software package developed and/or used in a research context</td>
              </tr>
              <tr>
                <th>Other Research Product</th>
                <td>Anything that does not fall in the previous categories (e.g. workflow, methods, protocols)</td>
              </tr>
            </table>
            <table *ngIf="tab === 'attributes'">
              <tr>
                <th>Organization & Country</th>
                <td>
                  <p><span class="uk-text-bold">For research outcomes:</span> the affiliated organizations of its
                    authors (and their country)</p>
                  <p><span class="uk-text-bold">For projects:</span> the organizations participating in the project
                    (i.e. beneficiaries of the grant) and
                    their countries
                  </p>
                  <p>
                    <span class="uk-text-bold">Country code mapping: </span>
                    <a href="https://api.openaire.eu/vocabularies/dnet:countries" target="_blank">
                      https://api.openaire.eu/vocabularies/dnet:countries</a>
                  </p>
                </td>
              </tr>
              <tr>
                <th>Type</th>
                <td>
                  <p>The sub-type of a research outcome (e.g., a publication can be a pre-print, conference proceeding,
                    article,
                    etc.)</p>
                  <p><span class="uk-text-bold">Resource type mapping: </span>
                    <a href="https://api.openaire.eu/vocabularies/dnet:result_typologies" target="_blank">https://api.openaire.eu/vocabularies/dnet:result_typologies</a>
                    (click on the code to see the specific types for each result type)
                  </p>
                </td>
              </tr>
              <tr>
                <th>Access mode</th>
                <td>
                  <p>The best available (across all instances) access rights of a research outcome</p>
                  <p>Types: open, restricted, closed, embargo (= closed for a specific period of time, then open)</p>
                  <p><span class="uk-text-bold">Note:</span> definition of <span class="uk-text-bold">restricted</span>
                    may vary by data source.</p>
                </td>
              </tr>
              <tr>
                <th>PID (persistent identifier)</th>
                <td>
                  <p>A long-lasting reference to a resource</p>
                  <p><span class="uk-text-bold">Types: </span> <a
                      href="http://api.openaire.eu/vocabularies/dnet:pid_types" target="_blank">http://api.openaire.eu/vocabularies/dnet:pid_types</a>
                  </p>
                </td>
              </tr>
              <tr>
                <th>Context</th>
                <td>Related research community, initiative or infrastructure</td>
              </tr>
              <tr>
                <th>Journal</th>
                <td>The scientific journal an article is published in.</td>
              </tr>
              <tr>
                <th>Publisher</th>
                <td>The publisher of the venue (journal, book, etc.) of a research outcome</td>
              </tr>
              <tr>
                <th class="important">Content Providers (Datasources)</th>
                <td>
                  <p>The different data sources ingested in the OpenAIRE Research Graph.</p>
                  <div class="uk-text-bold">Content Provider Types:</div>
                  <ul class="portal-circle">
                    <li>Repositories</li>
                    <li>Open Access Publishers & Journals</li>
                    <li>Aggregators</li>
                    <li>Entity Registries</li>
                    <li>Journal Aggregators</li>
                    <li>CRIS (Current Research Information System)</li>
                  </ul>
                </td>
              </tr>
              <tr>
                <th>Repositories</th>
                <td>Information systems where scientists upload the bibliographic metadata and payloads of their
                  research outcomes (e.g. PDFs of their scientific articles, CSVs of their data, archive with their
                  software), due to obligations from their organizations, their funders, or due to community practices
                  (e.g. ArXiv, Europe PMC, Zenodo).
                </td>
              </tr>
              <tr>
                <th>Open Access Publishers & Journals</th>
                <td>Information systems of open access publishers or relative journals, which offer bibliographic
                  metadata and PDFs of their published articles.
                </td>
              </tr>
              <tr>
                <th>Aggregators</th>
                <td>Information systems that collect descriptive metadata about research products from multiple sources
                  in order to enable cross-data source discovery of given research products (e,g, DataCite,
                  BASE, DOAJ).
                </td>
              </tr>
              <tr>
                <th>Entity Registries</th>
                <td>Information systems created with the intent of maintaining authoritative registries of given
                  entities in the scholarly communication, such as OpenDOAR for the institutional repositories, re3data
                  for the data repositories, CORDA and other funder databases for projects and funding information.
                </td>
              </tr>
              <tr>
                <th>CRIS (Current Research Information System)</th>
                <td>Information systems adopted by research and academic organizations to keep track of their research
                  administration records and relative results; examples of CRIS content are articles or datasets funded
                  by projects, their principal investigators, facilities acquired thanks to funding, etc.
                </td>
              </tr>
            </table>
            <div class="uk-text-small uk-margin-small-top">
              <img src="assets/common-assets/graph.svg" style="opacity: 0.4">
              <span class="uk-margin-small-left uk-text-baseline uk-text-muted">More information for <a
                  href="https://graph.openaire.eu" class="graph-color">OpenAIRE Research Graph</a>.</span>
            </div>
          </div>
        </div>
        <div id="how" class="uk-container uk-container-large uk-section-small">
          <h2 class="uk-text-center">
            See how it works
          </h2>
          <div class="uk-margin-large-top">
            <how></how>
          </div>
        </div>
      </div>
    </div>
  `,
  styleUrls: ['methodology.component.css']
})
export class MethodologyComponent implements OnInit, OnDestroy {
  public stakeholder: Stakeholder;
  public tab: 'entities' | 'attributes' = 'entities';
  private subscriptions: any[] = [];
  
  constructor(private stakeholderService: StakeholderService,
              private seoService: SEOService,
              private _meta: Meta,
              private _router: Router,
              private route: ActivatedRoute,
              private _title: Title) {
  }
  
  ngOnInit() {
    this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
      this.stakeholder = stakeholder;
      if (this.stakeholder) {
        /* Metadata */
        const url = properties.domain + properties.baseLink + this._router.url;
        this.seoService.createLinkForCanonicalURL(url, false);
        this._meta.updateTag({content: url}, "property='og:url'");
        const description = "Methodology | " + this.stakeholder.name;
        const title = "Methodology | " + this.stakeholder.name;
        this._meta.updateTag({content: description}, "name='description'");
        this._meta.updateTag({content: description}, "property='og:description'");
        this._meta.updateTag({content: title}, "property='og:title'");
        this._title.setTitle(title);
      }
    }));
  }
  
  ngOnDestroy() {
    this.subscriptions.forEach(subscription => {
      if (subscription instanceof Subscription) {
        subscription.unsubscribe();
      }
    });
  }
}
