<script setup>
import { ref} from "vue";
import FilterComponent from "@/Apps/ScientificPublications/FilterComponent.vue";
import ArticleComponent from "@/Apps/ScientificPublications/ResearchComponent.vue";
import {getFile, fetchResearches, addView} from "@/rest/ScientificPublications/ManageResearch.js";
const input = ref("")
const isFilterOpened = ref(false);
const isResearchOpened = ref(false);
const articleToDisplay = ref(Object)

const researchList = ref(await fetchResearches(1));

const props = defineProps({
  filters: ref([""]),
});

const openFilter = () => {
  isFilterOpened.value = true;
};
const closeFilter = () => {
  isFilterOpened.value = false;
};
const submitFilters = ()=>{
}
const openResearch = (article) => {
  isResearchOpened.value = true;
  articleToDisplay.value = article;
}

const closeResearch = () => {
  isResearchOpened.value =false;
  articleToDisplay.value = null;
}

const downloadBibTex = (research) => {
  getFile(research.bibTexLocation)
}

const downloadArticle = (research) => {
  addView(research.pdfLocation)
  getFile(research.pdfLocation)
}

function downloadCoAuthors(){
  //todo
  const data = JSON.stringify(researcher.value);
  const blob = new Blob([data], {type:"application/json"});
  return URL.createObjectURL(blob);
}


function searchInList(list, searchInput) {
  let retList = []
  for (let i = 0; i < list.length; i++) {
    if (lDistance(list[i].title, searchInput) < 10 || list[i].title.toUpperCase().indexOf(searchInput.toUpperCase()) > -1){
      retList.push(list[i])
    }
  }
  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];
}

</script>

<template>
<div id="main">
  <FilterComponent :isOpen="isFilterOpened" :allArticles="researchList" @modal-close="closeFilter" @submit="submitFilters()"></FilterComponent>
  <ArticleComponent :article="articleToDisplay" :isOpen="isResearchOpened" :manage="false" @modal-close="closeResearch" @downloadPdf="downloadArticle(articleToDisplay)" @downloadBibTex="downloadBibTex(articleToDisplay)"></ArticleComponent>
  <div id="researches">
    <div id="search">
      <input type="text" id="search-input" placeholder="search for researches" v-model="input"/>
      <button id="filterButton" @click="openFilter">  Filters </button>
    </div>
    <ul id="researchUL">
      <li id="researchLi" v-for="n in searchInList(researchList,input)">
        <div class="vl"> {{n.title}}</div>
        <div class="vl"> {{ n.researcher.user.firstName +" "+ n.researcher.user.lastName }}</div>
        <a @click="openResearch(n)"> MoreInfo  </a></li>
    </ul>
  </div>
</div>
</template>

<style scoped>

</style>