<template>
  <Dialog
    id="add-pdf"
    v-model:visible="isOpen"
    modal
    :draggable="false"
    header="Titre du PDF"
    class="w-full | text-grey-800 | rounded-2xl | overflow-hidden"
    :class="{ 'with-comments': isCommentsOpen }"
    :closeOnEscape="true"
  >
    <template #header>
      <a class="btn">Valider et envoyer le brief</a>
      <h2 class="font-serif text-lg">Titre du PDF</h2>
    </template>
    <div id="vpv-container">
      <VPdfViewer
        :darkMode="true"
        :initialThumbnailsVisible="true"
        :src="file.url"
        local="fr_FR"
        @loaded="onPdfLoaded"
      />
      <button
        id="toggle-comments"
        aria-pressed="false"
        class="btn btn--transparent"
        data-icon="comment"
        @click="isCommentsOpen = !isCommentsOpen"
      >
        <span class="sr-only">Afficher les commentaires</span>
      </button>
      <Comments
        v-if="isCommentsOpen"
        :current-page-index="currentPageIndex"
        :file="file"
        :comments="file.comments"
        @update:file="changeFile"
      />
    </div>
  </Dialog>
</template>

<script setup>
import Dialog from "primevue/dialog";
import Comments from "../../comments/Comments.vue";
import { VPdfViewer, useLicense } from "@vue-pdf-viewer/viewer";
import { ref, watch } from "vue";

const { file } = defineProps({
  file: Object,
});

const emits = defineEmits(["close", "update:file"]);

const licenseKey = import.meta.env.VITE_VPV_LICENSE;
useLicense({ licenseKey });

// Variables
const isOpen = ref(true);
watch(isOpen, (newValue) => {
  emits("close");
});
const isCommentsOpen = ref(false);
const currentPageIndex = ref(1);

// Functions
const onPdfLoaded = () => {
  const wrapper = document.querySelector(".vpv-pages-inner-container");

  const observer = new IntersectionObserver(
    (entries) => {
      entries.forEach((entry, index) => {
        if (entry.intersectionRatio > 0.5) {
          currentPageIndex.value = parseInt(
            entry.target.getAttribute("aria-label").split(" ")[1]
          );
        }
      });
    },
    {
      root: wrapper,
      threshold: [0.5],
    }
  );

  const observePages = () => {
    const pages = document.querySelectorAll(".vpv-page-inner-container");
    pages.forEach((page) => {
      if (!page.__observed__) {
        observer.observe(page);
        page.__observed__ = true;
      }
    });
  };

  const mutationObserver = new MutationObserver(() => {
    observePages();
  });

  if (wrapper) {
    mutationObserver.observe(wrapper, { childList: true, subtree: true });
  }

  observePages();
};

function changeFile(newFile) {
  emits("update:file", newFile);
}
</script>

<style>
#add-pdf {
  --max-w: min(100vw - var(--gutter) * 2, 77rem);
  --max-h: calc(100vh - var(--gutter) * 2 - 5.5rem);
  --header-h: 4.5rem;
  --comments-w: 22.5rem;
  max-width: var(--max-w);
  height: var(--max-h);
  background: black;
  margin: auto !important;
}
#add-pdf [data-pc-section="header"] {
  height: var(--header-h);
  padding: 1rem;
  color: var(--color-white);
  background: var(--color-white-20);
  display: flex;
  gap: var(--space-16);
}
#add-pdf [data-pc-section="header"] h2 {
  font-family: var(--font-serif);
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  padding: 0 15rem;
  line-height: 2.5rem;
}
#add-pdf [data-pc-name="pcclosebutton"] {
  color: var(--color-white);
}

#vpv-container {
  width: var(--max-w);
  height: calc(var(--max-h) - var(--header-h));
  background: black;
  position: relative;
}

/* Comments */
.with-comments .vpv-pages-container-wrapper {
  margin-right: var(--comments-w, 20rem);
}
#toggle-comments {
  position: absolute;
  right: var(--space-16);
  bottom: var(--space-16);
  padding: 0.625rem;
}
#comments-container {
  background-color: black;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 4.5rem;
  width: var(--comments-w);
  padding: var(--space-24) var(--space-32);
}
.comments {
  overflow-y: auto;
  height: calc(100% - 3.5rem);
  margin-bottom: 1rem;
  margin-right: -2rem;
  padding-right: 2rem;
}
.comment {
  --flow-space: var(--space-12);
  font-size: var(--text-sm);
  border: var(--border);
  border-radius: var(--rounded-lg);
  padding: var(--space-12);
  color: var(--color-grey-400);
}
.comment header p {
  display: flex;
  gap: var(--space-8);
}
.comment header strong,
.comment footer {
  font-weight: 500;
  color: var(--color-white);
}
.comment header time {
  color: var(--color-primary);
  font-weight: 500;
  margin-left: auto;
}
.comment[data-status="unread"] {
  background: var(--color-white);
  border-color: var(--color-white);
  color: var(--color-grey-700);
}
.comment[data-status="unread"] header p > :first-child::before {
  content: "";
  display: inline-block;
  width: 0.375rem;
  height: 0.375rem;
  border-radius: 50%;
  background: var(--color-primary);
  margin-right: var(--space-8);
  margin-bottom: 0.075em;
}
.comment[data-status="unread"] header strong,
.comment[data-status="unread"] footer {
  color: var(--color-black);
}
.comment[data-status="unread"] header time {
  color: var(--color-primary);
}
#comments-container form {
  --flow-space: 0.5rem;
  flex-direction: column;
  position: -webkit-sticky;
  position: sticky;
  bottom: 4.5rem;
  background: black;
  padding: var(--space-24) 0;
}
#comments-container textarea {
  position: sticky;
  bottom: 0;
  margin: 0;
  resize: none;
  color: var(--color-white);
}
::placeholder {
  color: var(--color-white-50);
}
#comments-container form footer {
  gap: var(--space-12);
}
#comments-container form footer > * {
  flex-grow: 1;
}
</style>

<style scoped>
:deep(.vpv-variables) {
  /* Base styles */
  --vpv-base-radius: var(--rounded-xl);
  /* Viewer container */
  --vpv-container-width-sm: 48rem;
  /* Toolbar */
  --vpv-toolbar-size: 4.5rem;
}
:deep(.vpv-variables.vpv-variables__dark) {
  /* Base styles */
  --vpv-base-border-color: var(--color-grey-700);
  /* Viewer container */
  --vpv-container-border-color: var(--vpv-base-border-color);
  /* Loader */
  --vpv-loader-backdrop-color: rgba(0, 0, 0, 0.8);
  /* Toolbar */
  --vpv-toolbar-background-color: var(--color-white-10);
  --vpv-toolbar-color: white;
  --vpv-toolbar-border-color: var(--vpv-base-border-color);
  /* Sidebar */
  --vpv-sidebar-content-background-color: var(--color-white-10);
  --vpv-sidebar-content_thumbnail-focused-border-color: var(--color-white-20);
  /* Drop file mask */
  --vpv-drop-zone-background: #1c2024;
  --vpv-drop-zone-border: #434c56;
  /* PDF Pages Content*/
  --vpv-pages-container-background: black;
  /* Popover */
  --vpv-popover-background-color: black;
  --vpv-popover-color: var(--color-white);
  --vpv-popover-border-color: transparent;
  /* Menu Dropdown */
  --vpv-dropdown-menu-content-background: #181818;
  --vpv-dropdown-menu-content-menu-hover-background: var(--color-white-10);
  --vpv-dropdown-menu-content-separator-background: var(--color-white-10);
  --vpv-dropdown-menu-content-border-color: var(--color-white-10);
  --vpv-dropdown-separator: var(--color-white-10);
  /* Search Input */
  --vpv-input-background-color: transparent;
  --vpv-input-border-color: var(--color-white);
  --vpv-input-placeholder-color: #596673;
  /* Modal of PDF Properties */
  --vpv-properties-content-background: var(
    --vpv-dropdown-menu-content-background
  );
  --vpv-properties-content-secondary-color: #c6ccd2;
  --vpv-properties-content-color: var(--vpv-toolbar-color);
  --vpv-properties-separator: var(--vpv-dropdown-separator);
  --vpv-properties-header: white;
  /* Modal of Print Progress */
  --vpv-print-progress-background: var(--vpv-dropdown-menu-content-background);
  /* Tooltip */
  --vpv-tooltip-background: #181818;
  --vpv-tooltip-color: white;
  --vpv-tooltip-border-color: var(--color-black);
  /* Outline */
  --vpv-outline-color: var(--color-white-10);
}

:deep(.vpv-container) {
  border-top-left-radius: 0 !important;
  border-top-right-radius: 0 !important;
}
:deep(.vpv-body-wrapper),
:deep(.vpv-container .vpv-sidebar-wrapper) {
  margin-top: 0 !important;
  margin-bottom: var(--vpv-toolbar-size) !important;
}
:deep(.vpv-splitter) {
  max-width: 0;
}

/* Sidebar */
:deep(.vpv-container .vpv-sidebar-wrapper__opened) {
  max-width: 9rem;
}
:deep(.vpv-sidebar-features) {
  display: none;
}
:deep(.pdf-thumbnail-wrapper .pdf-thumbnail img),
:deep(.pdf-thumbnail-wrapper .pdf-thumbnail .placeholder) {
  border-radius: var(--rounded-md);
}

/* Toolbar */
:deep(.vpv-container .vpv-toolbar-wrapper) {
  border-bottom-width: 0;
  border-top-width: 1px;
  padding-left: 1rem !important;
}
:deep(.vpv-toolbar-wrapper) {
  top: unset !important;
  bottom: 0 !important;
}
:deep(.vpv-toolbar-start .vpv-toolbar-btn) {
  display: none;
}
:deep(.vpv-toolbar-start::before) {
  content: "Page";
}
:deep(.vpv-input) {
  border-radius: var(--rounded-sm) !important;
  max-width: 1.5rem !important;
}
:deep(.vpv-input ~ span) {
  color: var(--color-grey-400);
}
:deep(.vpv-toolbar-end) {
  padding-right: 4rem;
}
:deep(.vpv-toolbar-end > button:nth-child(1)),
:deep(.vpv-toolbar-end > button:nth-child(3)),
:deep(.vpv-toolbar-end > button:nth-child(4)) {
  display: none;
}
</style>
