<script setup>
import { onMounted } from 'vue';
import Modal from './modal/AddUrl.vue';
import { useFeeds } from '@/composables/useFeeds';

const {
  feeds,
  showMessage,
  message,
  showModal,
  viewMode,
  currentIndex,
  leaveArticleView,
  layout,
  nextArticle,
  prevArticle,
  fetchData,
  getReadable,
  setInitialLoad,
  showMessageForXSeconds,
} = useFeeds()

const shareLabel = navigator.share ? 'Share' : 'Copy link'

async function shareUrl(url) {
  if (navigator.share) {
    await navigator.share({ url })
  } else {
    await navigator.clipboard.writeText(url)
    showMessageForXSeconds('Link copied.', 2)
  }
}

onMounted(async () => {
  setInitialLoad(false)
  await fetchData()
  setTimeout(function () {
    setInitialLoad(true)
    console.log('set to true')
  }, 2000);
});
</script>

<template>
  <Teleport to="body">
    <modal :show="showModal" @close="showModal = false">
      <template #header>
        <h3>Add RSS Feed</h3>
      </template>
    </modal>
  </Teleport>
  <div>
    <div v-if="showMessage" class="message">{{ message }}</div>

    <div v-if="viewMode === 'list'" id='article' class='article' :class="{ 'article--cards': layout === 'cards' }">
      <div v-if="feeds.length == 0" class="empty-state">
        <svg class="empty-state__icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
          <circle cx="12" cy="12" r="10"/>
          <polyline points="7 12.5 10.5 16 17 9"/>
        </svg>
        <p class="empty-state__label">All caught up</p>
      </div>
      <template v-for="(   feed, index   ) in    feeds   ">
        <div v-bind:id="index" class="observe">
          <p class="feed-source">{{ feed.feedTitle }}</p>
          <h2 @click="getReadable(feed, index)" class="feed-title">{{ feed.title }}</h2>
          <h3>{{ feed.timestamp }}</h3>
          <p v-if="!feed.readable" class="feed-original-link">
            <a :href="feed.url" target="_blank" rel="noopener noreferrer">Read original article &#8599;</a>
            <button type="button" class="feed-share-btn" :title="shareLabel" @click="shareUrl(feed.url)" :aria-label="shareLabel">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"/><polyline points="16 6 12 2 8 6"/><line x1="12" y1="2" x2="12" y2="15"/></svg>
            </button>
          </p>
          <p class="feed-content" v-html='feed.content'></p>
        </div>
      </template>
    </div>

    <div v-else class="article-single">
      <button type="button" class="article-single__back" @click="leaveArticleView">&larr; Back to list</button>
      <div v-if="feeds.length == 0" class="empty-state">
        <svg class="empty-state__icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
          <circle cx="12" cy="12" r="10"/>
          <polyline points="7 12.5 10.5 16 17 9"/>
        </svg>
        <p class="empty-state__label">All caught up</p>
      </div>
      <template v-else>
        <p class="feed-source">{{ feeds[currentIndex].feedTitle }}</p>
        <h2 @click="getReadable(feeds[currentIndex], currentIndex)" class="feed-title">{{ feeds[currentIndex].title }}</h2>
        <h3>{{ feeds[currentIndex].timestamp }}</h3>
        <p v-if="!feeds[currentIndex].readable" class="feed-original-link">
          <a :href="feeds[currentIndex].url" target="_blank" rel="noopener noreferrer">Read original article &#8599;</a>
          <button type="button" class="feed-share-btn" :title="shareLabel" @click="shareUrl(feeds[currentIndex].url)">{{ shareLabel }}</button>
        </p>
        <p class="feed-content" v-html="feeds[currentIndex].content"></p>
      </template>

      <div class="article-nav">
        <button
          type="button"
          class="article-nav__btn"
          :disabled="currentIndex === 0"
          aria-label="Previous article"
          @click="prevArticle"
        >&uarr;</button>
        <button
          type="button"
          class="article-nav__btn"
          :disabled="feeds.length === 0 || currentIndex === feeds.length - 1"
          aria-label="Next article"
          @click="nextArticle"
        >&darr;</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* Plain vertical stack of bordered "cards" — deliberately not flex/grid, and
   with no truncation/max-height: normal block flow lets each card grow to fit
   its own full content (images included), with no cross-element interaction. */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 4rem 1rem;
  gap: 1rem;
  opacity: 0.4;
}

.empty-state__icon {
  width: 64px;
  height: 64px;
}

.empty-state__label {
  font-size: 1.1rem;
  margin: 0;
}

.article--cards .observe {
  border: 1px solid var(--color-border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--color-background-soft);
}

.article--cards .observe + .observe {
  margin-top: 1rem;
}

.article--cards .feed-title {
  border-bottom: none;
}

.article--cards h3 {
  margin: 0;
  padding: 0 1em 0.5em;
}

/* `v-html` content isn't part of the component's render output, so it never
   gets the scoped `data-v-*` attribute — `:deep()` is required for this rule
   to actually reach the injected <img> tags (without it, the selector silently
   never matches). Cap the height so a large article photo reads as a tidy
   preview thumbnail; the card itself is left to grow to whatever height its
   content (image included) naturally needs — no clamping, no max-height. */
.article--cards .feed-content :deep(img) {
  max-height: 220px;
  width: auto;
}

.feed-original-link {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.feed-original-link a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.25em 1em;
  color: var(--color-accent);
  text-decoration: none;
}

.feed-original-link a:hover {
  text-decoration: underline;
}

.feed-share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--color-text);
  opacity: 0.45;
  cursor: pointer;
  transition: opacity 0.15s;
}

.feed-share-btn:hover {
  opacity: 1;
}

.article-single {
  position: relative;
  padding-bottom: 5rem;
}

.article-single__back {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  margin-bottom: 1rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
}

.article-single__back:hover {
  border-color: var(--color-border-hover);
}

.article-nav {
  position: fixed;
  right: 1rem;
  bottom: 1.5rem;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.article-nav__btn {
  min-width: 56px;
  min-height: 56px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: var(--color-background-soft);
  color: var(--color-text);
  font-size: 1.5rem;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  cursor: pointer;
  opacity: 0.55;
  transition: opacity 0.15s, border-color 0.15s;
}

.article-nav__btn:hover:not(:disabled),
.article-nav__btn:focus-visible {
  border-color: var(--color-border-hover);
  opacity: 1;
}

.article-nav__btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
</style>
