<!----------------------------------------------------
	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 FilterComponent from "@/Apps/ScientificPublications/FilterComponent.vue";
import ArticleComponent from "@/Apps/ScientificPublications/ArticleComponent.vue";
const input = ref("");
const statsOf = ref("");
const statsBy = ref("");
const isFilterOpened = ref(false);
const isArticleOpened = ref(false);
let chart;

const openModal = () => {
  isFilterOpened.value = true;
};
const closeModal = () => {
  isFilterOpened.value = false;
};
const submitFilters = ()=>{
  // call only with those filters the get
}
const openArticle = (article) => {
  isArticleOpened.value = true;
}

const closeArticle = () => {
  isArticleOpened.value =false;
}

const downloadBibTex = (article) => {
//todo
}

const downloadArticle = (article) => {
 //todo
}


const articleList = [
  ["The Great Potato War Lorem Ipsum aeaDq Terelumni ","Author",["Co-author1", "co-Authors2"],
  " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas condimentum ex tempor leo pulvinar, vitae imperdiet leo pellentesque. Aenean aliquam, ante at tempus sagittis, lorem leo feugiat felis, eget vestibulum tortor est nec libero. Cras sit amet venenatis velit, at rhoncus est. Cras id sem placerat, cursus sem et, aliquam felis. Nullam mi nunc, laoreet eget rutrum ac, blandit nec lorem. Duis fermentum aliquet tortor ac tristique. Aenean ac sagittis nulla, at auctor dolor. Vivamus et neque sodales, vestibulum dolor et, posuere urna. Pellentesque ut elit metus. Cras velit lectus, luctus auctor interdum eu, aliquam nec est. Donec elementum nisl sit amet nibh aliquam ultricies. Nullam felis orci, suscipit eu tincidunt pretium, euismod vel sem. Duis eget vehicula neque, nec gravida leo. Cras pellentesque arcu quis justo lobortis, ut semper massa rhoncus. Quisque sagittis dignissim congue. Nullam tortor ligula, mattis vel cursus id, pretium non lacus.",
  "language",
    "access",
  ]
]


const jsonMockViewsByYears= [
  {label: "2004", y:4},
  {label: "2005", y:99},
  {label: "2007", y:555},
  {label: "2009", y:22},
  {label: "2011", y:1666},
]

function searchInList(list, searchInput){
  let retList= []
  if (searchInput === "")
    return [list[0].slice(0,2)]
  for (let i = 0; i < list.length; i++) {
    console.log(list[i])
    if (lDistance(list[i][0], searchInput) < 10 || list[i][0].toUpperCase().indexOf(searchInput.toUpperCase()) > -1){
      retList.push(list[i].slice(0,2))
    }
    console.log(list[i][0].toUpperCase().indexOf(searchInput.toUpperCase()))
  }
  return retList
}

function lDistance(s,t){
  if (!s.length) return t.length;
  if (!t.length) return s.length;
  const arr = [];
  for (let i = 0; i <= t.length; i++) {
    arr[i] = [i];
    for (let j = 1; j <= s.length; j++) {
      arr[i][j] =
          i === 0
              ? j
              : Math.min(
                  arr[i - 1][j] + 1,
                  arr[i][j - 1] + 1,
                  arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1)
              );
    }
  }
  return arr[t.length][s.length];
}

const options = reactive({
    backgroundColor:null,
    theme: "light2",
    animationEnabled: true,
    title: {
      fontColor: "white",
      text : "please select options",
    },
    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",
        text: statsOf.value + " By "+ statsBy.value,
      }
      if (statsOf.value === "views" && statsBy.value === "years") {
        options.data[0].dataPoints = jsonMockViewsByYears;
      }

      options.title.text = statsOf.value + " By "+ statsBy.value;
      chart.render()
    }
</script>

<template>
  <div id="main">
    <FilterComponent :isOpen="isFilterOpened" @modal-close="closeModal" @submit="submitFilters">
    </FilterComponent>
    <ArticleComponent :isOpen="isArticleOpened" @modal-close="closeArticle"></ArticleComponent>
    <div id="profilePicture">
      <img src="/Clyde.png" />
    </div>
    <div id="researcherInfos">
      <div class="surrounded">John Doe</div>
      <div class="surrounded">Orcid : 12144-2144-12336-B</div>
      <div class="surrounded">Email : John.Doe@umons.ac.be</div>

      <div class="surrounded">
        site :
        <a href="http://localhost:5173" style="color: #007aff">here</a>
      </div>
      <div class="surrounded">Domain : physics, IT</div>
      <div id="coAuthorList" class="surrounded">Co-authors list : D</div>
    </div>
    <div id="stats">
      <div class="surrounded">
        Stat type :
        <select @change="update()" id="stats-select" v-model="statsOf">
          <option value="views">Views</option>
          <option value="co-authors">Co-authors</option>
          <option value="articles">Articles</option>
          <option value="language">Languages</option>
        </select>
      </div>
      <div class="surrounded">
        Class by:
        <select @change="update()" id="classed-select" v-model="statsBy">
          <option selected="selected" value="years">Years</option>
          <option value="months">Months</option>
          <option value="topics">Topics</option>
        </select>
      </div>
      <div id="statsPie">
		  <CanvasJSChart :options="options" id=chart @chart-ref="c => chart = c "/>
      </div>
    </div>
    <div id="articles">
      <div id="search">
        <input type="text" id="search-input" placeholder="search articles" v-model="input"/>
        <button id="filterButton" @click="openModal">  Filters </button>
      </div>
      <ul id="articlesUL">
        <li id="articleLi" v-for="n in searchInList(articleList,input)">
          <div class="vl"> {{n[0]}}</div>
          <div class="vl"> {{n[1]}}</div>
         <a @click="openArticle"> MoreInfo  </a></li>
      </ul>
    </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;
}

#statsPie {

}

#search{
  width: 100%;
  height: 10%;
  display: inline-flex;
}
#search-input {
  margin-left: 25px;
  width: 75%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  height: 20px;
  align-self: center;
}

#filterButton {
  align-self: center;
  margin-left: 2px;
  font-size: xx-large;
  color: white;
  background: rgba(191, 64, 191,0.5);
  border:2px solid black;
}
#filterButton:hover{
  background: rgba(191, 64, 191);
}


#articlesUL {
  list-style-type: none;
  color: white;
  padding: 12px;
  margin: 5px;
  height: 400px;
  overflow: scroll;
}
#articleLi{
  display: grid;
  grid-template-columns: auto auto auto;
  border: 2px solid black;
  color: white;
  font-size: x-large;
  text-align: center;
  text-indent: 7px;
  background-color: rgba(255, 255, 255, 0.09);
  border-radius: 18px;
  margin-bottom: 15px;
}
a{
  color:#007aff;
  text-decoration: underline;
  cursor: pointer;
}

.vl {
  border-right: 2px solid black;
}

</style>
