<script setup>
import { onMounted, computed, nextTick, watch } from 'vue';
import { useFeeds } from '@/composables/useFeeds';

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

const unreadCount = computed(() => feeds.value.filter(f => !f.read).length)

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

function scrollToNextArticle() {
  const articles = document.querySelectorAll('#article .observe')
  const threshold = window.scrollY + 1
  for (const el of articles) {
    if (el.offsetTop > threshold) {
      el.scrollIntoView({ behavior: 'smooth', block: 'start' })
      return
    }
  }
}

// Small images (icons, logos, ...) look bad stretched to the full-bleed
// width used for readable article images — leave them at their natural size
// instead. Intrinsic size is only known once the image has loaded, so check
// on load (or immediately if it's already cached/complete).
const SMALL_IMAGE_THRESHOLD = 200

function markSmallImages() {
  document.querySelectorAll('.article-feature__content--readable img').forEach(img => {
    const checkSize = () => {
      if (img.naturalWidth && img.naturalWidth <= SMALL_IMAGE_THRESHOLD) {
        img.classList.add('article-feature__image--small')
      }
    }
    if (img.complete) {
      checkSize()
    } else {
      img.addEventListener('load', checkSize, { once: true })
    }
  })
}

watch(() => feeds.value[currentIndex.value]?.content, async () => {
  await nextTick()
  markSmallImages()
})

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>
  <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" class="list-topbar">
        <span v-if="!navTitleVisible" class="list-topbar__title">RSS Reader<span v-if="unreadCount" class="list-topbar__unread"> ({{ unreadCount }})</span></span>
        <button type="button" class="list-topbar__next" @click="scrollToNextArticle">Skip to next article &darr;</button>
      </div>
      <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">
      <div class="article-single__topbar">
        <div class="article-single__topbar-inner">
          <button type="button" class="article-single__back" @click="leaveArticleView">&larr; Back to list</button>
          <span v-if="feeds.length" class="article-single__progress">{{ currentIndex + 1 }} / {{ feeds.length }}</span>
        </div>
      </div>
      <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>
        <article class="article-feature">
          <p class="article-feature__source">{{ feeds[currentIndex].feedTitle }}</p>
          <h2 @click="getReadable(feeds[currentIndex], currentIndex)" class="article-feature__title">{{ feeds[currentIndex].title }}</h2>
          <h3 class="article-feature__meta">{{ 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="article-feature__content" :class="{ 'article-feature__content--readable': feeds[currentIndex].readable }" v-html="feeds[currentIndex].content"></p>
        </article>
      </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>
.list-topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0;
  margin-bottom: 0.5rem;
  background: var(--color-background);
}

.list-topbar__title {
  font-weight: bold;
  font-size: clamp(1.1rem, 4vw, 1.4rem);
}

.list-topbar__unread {
  font-weight: normal;
  opacity: 0.6;
}

.list-topbar__next {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  margin-left: auto;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: var(--color-background-soft);
  color: var(--color-text);
  cursor: pointer;
}

.list-topbar__next:hover {
  border-color: var(--color-border-hover);
}

.observe {
  scroll-margin-top: 3.5rem;
}

/* 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;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-bottom: 5rem;
}

.article-single__topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  align-self: flex-start;
  width: 100vw;
  margin-left: 50%;
  margin-bottom: 1rem;
  transform: translateX(-50%);
  background: var(--color-background);
}

.article-single__topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 720px;
  margin: 0 auto;
  padding: 0.5rem 1rem;
}

.article-single__back {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  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-single__progress {
  font-size: 0.85rem;
  color: var(--color-text);
  opacity: 0.6;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.article-feature {
  width: 100%;
  max-width: 720px;
}

.article-feature__source {
  margin: 0 0 0.5em;
  padding: 0 1rem;
  font-size: clamp(0.75rem, 2vw, 0.85rem);
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-accent);
}

.article-feature__title {
  cursor: pointer;
  margin: 0;
  padding: 0 1rem;
  font-family: 'Courier New';
  font-size: clamp(1.75rem, 6vw, 2.75rem);
  font-weight: bold;
  line-height: 1.15;
  color: var(--color-accent-2);
  transition: color 0.2s;
}

.article-feature__title:hover {
  color: var(--color-accent-2-hover);
}

.article-feature__meta {
  margin: 0.75em 0 1.5em;
  padding: 0 1rem 1.5em;
  font-size: clamp(0.85rem, 2.5vw, 1rem);
  font-weight: normal;
  opacity: 0.55;
  border-bottom: 1px solid var(--color-border);
}

.article-feature .feed-original-link {
  margin-bottom: 1.5em;
  padding: 0 1rem;
}

.article-feature__content {
  padding: 0 1rem;
  font-family: Georgia, 'Times New Roman', Times, serif;
  font-size: clamp(1rem, 3.5vw, 1.25rem);
  line-height: 1.75;
  overflow-wrap: break-word;
}

.article-feature__content :deep(p) {
  padding: 0.5em 0;
}

.article-feature__content :deep(h3) {
  padding: 0.5em 0;
  font-size: clamp(1rem, 3vw, 1.3rem);
  font-weight: bold;
}

.article-feature__content :deep(img),
.article-feature__content :deep(video) {
  max-width: 100%;
  height: auto;
}

.article-feature__content--readable :deep(img),
.article-feature__content--readable :deep(video) {
  display: block;
  width: 100vw;
  max-width: 100vw;
  height: auto;
  margin-top: 1.5em;
  margin-bottom: 1.5em;
  margin-left: 50%;
  transform: translateX(-50%);
}

/* Small images (icons, logos, ...) keep their natural size instead of being
   stretched to the full-bleed width. */
.article-feature__content--readable :deep(img.article-feature__image--small) {
  display: block;
  width: auto;
  max-width: 100%;
  margin: 1.5em auto;
  transform: none;
}

/* On desktop the viewport is much wider than the article column, so the
   full-bleed 100vw treatment above would blow images up far beyond their
   natural resolution. Keep them at natural size, centered in the text. */
@media (min-width: 720px) {
  .article-feature__content--readable :deep(img),
  .article-feature__content--readable :deep(video) {
    display: block;
    width: auto;
    max-width: 100%;
    height: auto;
    margin: 1.5em auto;
    transform: none;
  }
}

.article-feature__content :deep(a) {
  color: var(--color-accent);
  text-decoration-color: var(--color-accent-hover);
}

.article-feature__content :deep(blockquote) {
  margin: 1.5em 0;
  padding: 1em 0;
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  font-family: Georgia, 'Times New Roman', Times, serif;
  font-size: 1.25em;
  font-style: italic;
  text-align: center;
}

.article-feature__content :deep(pre) {
  overflow-x: auto;
  padding: 1em;
  background: var(--color-background-soft);
}

.article-feature__content :deep(code) {
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
}

.article-feature__content :deep(figcaption) {
  margin-top: 0.5em;
  font-size: 0.85em;
  text-align: center;
  opacity: 0.65;
}

.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>
