<script>
  import { onMount } from 'svelte'
  import { localeStore } from '@stores/locale'
  import { navigateTo } from '@lib/router'

  let { data } = $props()

  const currentLang = $derived($localeStore.current)

  const translations = {
    homeText: {
      fr: "World Game crée des expériences gamifiées qui transforment l'engagement en résultats mesurables",
      en: "World Game creates gamified experiences that transform engagement into measurable results"
    },
    explore: {
      fr: "EXPLORER",
      en: "EXPLORE"
    }
  }

  function t(key) {
    return translations[key]?.[currentLang] || translations[key]?.fr || ''
  }

  onMount(() => {
    const playVideo = async (videoId) => {
      const video = document.getElementById(videoId)
      if (video) {
        try {
          video.muted = true
          video.playsInline = true
          const playPromise = video.play()
          if (playPromise !== undefined) {
            await playPromise
          }
        } catch (error) {
          console.log(`Autoplay failed for ${videoId}:`, error)
          const playOnInteraction = () => {
            video.play().catch(e => console.log('Fallback play failed:', e))
            document.removeEventListener('click', playOnInteraction)
            document.removeEventListener('touchstart', playOnInteraction)
          }
          document.addEventListener('click', playOnInteraction)
          document.addEventListener('touchstart', playOnInteraction)
        }
      }
    }

    const timer = setTimeout(() => {
      playVideo('home-video')
      playVideo('home-video-mobile')
    }, 100)

    return () => clearTimeout(timer)
  })

  function handleExplore() {
    navigateTo('/portfolio')
  }
</script>

<div class="home golden-grid slide" data-anchor="HOME">
  <div style="grid-area: 1/1/span 20/span 20; filter: saturate(140%);">
    <div class="olly">
      <figure
        style="
          height: -webkit-fill-available;
          width: -webkit-fill-available;
        "
      >
        <video
          muted
          autoplay
          playsinline
          loop
          controls={false}
          preload="metadata"
          id="home-video"
          class="home-video home-video-desktop"
          style="
            object-fit: cover;
            min-height: 100%;
            min-width: 100%;
            height: -webkit-fill-available;
            width: -webkit-fill-available;
          "
        >
          <source
            src="/assets/video/Website_version.mp4"
            type="video/mp4"
          />
        </video>
        <video
          muted
          autoplay
          playsinline
          loop
          controls={false}
          preload="metadata"
          id="home-video-mobile"
          class="home-video-mobile"
          style="
            object-fit: cover;
            min-height: 100%;
            min-width: 100%;
            height: -webkit-fill-available;
            width: -webkit-fill-available;
          "
        >
          <source
            src="/assets/video/mobile_version_texte_fixe.mp4"
            type="video/mp4"
          />
        </video>
      </figure>
    </div>
  </div>

  <div class="vertical-line-start"></div>
  <div class="vertical-line-center"></div>
  <div class="vertical-line-end"></div>

  <div
    class="home-text"
    style="z-index: 5; justify-self: center; margin-top: 6vmax;"
  >
    <h2 class="font-face-danzza-light home-subtitle">
      {t('homeText')}
    </h2>
    <div
      class="clickable button"
      style="margin: auto; margin-top: 40px;"
      onclick={handleExplore}
      onkeypress={(e) => e.key === 'Enter' && handleExplore()}
      role="button"
      tabindex="0"
    >
      <div class="earth-icon clickable-filter-black"></div>
      <p class="clickable" style="font-family: Terminal; font-size: 1.2em;">
        {t('explore')}
      </p>
    </div>
  </div>
</div>

<style>
  .home {
    background-color: rgba(0, 0, 0, 0);
  }

  .home-text {
    z-index: 9;
    grid-area: 9/1 / span 6 / span 20;
    width: 100%;
  }

  .home-subtitle {
    font-size: var(--font-size-subtitle);
    color: white;
    width: 30%;
    margin: auto;
  }

  .olly {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  .olly figure {
    margin: 0;
    padding: 0;
    position: relative;
    width: 100%;
    height: 100%;
  }

  .home-video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .home-video-desktop {
    display: block !important;
  }

  .home-video-mobile {
    display: none !important;
  }

  @media screen and (max-width: 1000px) {
    .home-video-desktop {
      display: none !important;
    }
    .home-video-mobile {
      display: block !important;
    }

    .home-subtitle {
      font-size: var(--font-size-subtitle-mobile);
      width: 80%;
      line-height: 1.4;
    }
  }

  @media screen and (min-width: 701px) and (max-width: 912px) {
    .home-subtitle {
      font-size: var(--font-size-subtitle-tablet);
      width: 70%;
    }
  }
</style>
