<!----------------------------------------------------
	File:  ResearcherProfile.vue
	Author: Maxime Bartha
	Scope: Extension Publicatons scientifiquess
	Description: Researcher Profile Page containing his articles and his statistics
----------------------------------------------------->

<script setup>
import { ref, reactive } from "vue";
import {fetchResearcher, fetchResearches, fetchStats} from "@/rest/ScientificPublications/ResearcherProfile.js";
import ListResearches from "@/Apps/ScientificPublications/ListResearches.vue";
import i18n from "../../i18n.js";
const input = ref("");
const statsOf = ref();
const statsBy = ref();
let chart;

const researcherId = window.location.href.split("=")[1]


function getPP(){
  if(researcher.value.user.profilePictureUrl === null){
    return "/Clyde.png"
  }
  return researcher.value.user.profilePictureUrl
}
const researchList = ref(await fetchResearches(researcherId));
const researcher = ref(await fetchResearcher(researcherId));
const stats = ref(await fetchStats(researcherId))

function downloadCoAuthors(){
  let coAuthors = []
  for (let i in researchList.value) {
    for (const j in researchList.value[i].coAuthors){
      const coAuthor = researchList.value[i].coAuthors[j]
      coAuthors.push(coAuthor)
    }
  }
  const data = JSON.stringify(coAuthors);
  const blob = new Blob([data], {type:"application/json"});
  return URL.createObjectURL(blob);
}

const options = reactive({
    backgroundColor:null,
    theme: "light2",
    animationEnabled: true,
    title: {
      fontColor: "white",
      text : i18n("Please.Select.Option"),
    },
    data: [
      {
        type: "pie",
        indexLabel: "{label} (#percent%)",
        yValueFormatString: "#,##0",
        indexLabelFontColor: "white",
        toolTipContent:
          "<span style='\"'color: {color};'\"'>{label}</span> {y}(#percent%)",
      }]
	});

function update(){
  options.title = {
    fontColor: "white",
  }

  let index = (statsOf.value === "Views"?0:(statsOf.value === "Researches"?3:6)) + (statsBy.value ==="Years"?0:(statsBy.value==="Months"?1:2))
  if (typeof statsBy.value !== 'undefined' && typeof statsOf.value !== 'undefined'){
    options.data[0].dataPoints = stats.value[index]
    options.title.text = i18n(statsOf.value) +" "+ i18n("By") +" " + i18n(statsBy.value);
    chart.render();
  }

}
</script>

<template>
  <div id="main">
    <div id="profilePicture">
      <img :src=getPP() />
    </div>
    <div id="researcherInfos">
      <div class="surrounded">{{researcher.user.lastName}} {{researcher.user.firstName}}</div>
      <div class="surrounded">Orcid : {{researcher.orcidId}}</div>
      <div class="surrounded">Email : {{researcher.user.email}}</div>
      <div class="surrounded">
        Site : <a :href=researcher.site style="color: #007aff"> {{researcher.site}}</a>
      </div>
      <div class="surrounded">{{i18n("Domain")}} : {{researcher.domain}}</div>
      <div id="coAuthorList" class="surrounded">Co-authors list : <a :href=downloadCoAuthors() download="coAuthors.json">{{i18n("Here")}}</a></div>
    </div>
    <div id="stats">
      <div class="surrounded">
        {{i18n("Stat.Type")}} :
        <select @change="update()" id="stats-select" v-model="statsOf">
          <option value="Views">{{i18n("Views")}}</option>
          <option value="Researches">{{i18n("Researches")}}</option>
          <option value="DifferentLanguage">{{i18n("DifferentLanguage")}}</option>
        </select>
      </div>
      <div class="surrounded">
        {{i18n("Class.By")}} :
        <select @change="update()" id="classed-select" v-model="statsBy">
          <option value="Years">{{i18n("Years")}}</option>
          <option value="Months">{{i18n("Months")}}</option>
          <option value="Domain">{{i18n("Domain")}}</option>
        </select>
      </div>
      <div id="statsPie">
		  <CanvasJSChart :options="options" id=chart @chart-ref="c => chart = c "/>
      </div>
    </div>
    <div id="researches"><list-researches :researchList="researchList"></list-researches></div>
  </div>
</template>

<style scoped>
#main {
  display: grid;
  grid-template-columns: 22% auto;
  grid-template-rows: 26% auto;
  height: 100%;
  width: 100%;
}

#profilePicture {
  display: flex;
  justify-content: center;
}

#profilePicture img {
  align-self: center;
  justify-self: center;
  width: 60%;
}

#researcherInfos {
  display: grid;
  grid-template-columns: auto auto auto;
  column-gap: 5px;
  grid-template-rows: auto auto;
}

.surrounded {
  border: 2px solid black;
  color: white;
  font-size: x-large;
  align-self: center;
  text-align: center;
  background-color: rgba(255, 255, 255, 0.09);
  border-radius: 20px;
  margin-bottom: 10px;
}

.surrounded select {
  margin-top: 2px;
  margin-bottom: 2px;
  border: 1px solid black;
  color: white;
  background-color: rgb(255, 255, 255, 0.1);
  font-size: large;
}

a{
  color:#007aff;
  text-decoration: underline;
  cursor: pointer;
}
</style>