<script>
  import { selectedIndex, attractions, popScreen } from '../lib/store.js';
  import { getImageUrl } from '../lib/api.js';

  function handleNavigation(direction) {
    const newIndex =
      direction === 'right'
        ? ($selectedIndex + 1) % $attractions.length
        : ($selectedIndex - 1 + $attractions.length) % $attractions.length;
    selectedIndex.set(newIndex);
  }
</script>

<div class="attractions-container">
  <div class="header">
    <button class="back-button" on:click={popScreen}>← Back</button>
    <h1>Things to Do</h1>
  </div>

  {#if $attractions.length > 0}
    <div class="attractions-showcase">
      {#each $attractions as attraction, index (attraction.id)}
        <div class="attraction-card" class:active={index === $selectedIndex}>
          {#if attraction.image_id}
            <img
              src={getImageUrl(attraction.image_id.id)}
              alt={attraction.name}
              class="attraction-image"
            />
          {/if}
          <div class="attraction-info">
            <h2>{attraction.name}</h2>
            {#if attraction.description}
              <p class="description">{attraction.description}</p>
            {/if}
            {#if attraction.category}
              <div class="badge">{attraction.category}</div>
            {/if}
            {#if attraction.distance_km}
              <div class="distance">📍 {attraction.distance_km} km away</div>
            {/if}
          </div>
        </div>
      {/each}
    </div>

    <div class="navigation-hints">
      <button on:click={() => handleNavigation('left')}>← Previous</button>
      <span class="counter">{$selectedIndex + 1} / {$attractions.length}</span>
      <button on:click={() => handleNavigation('right')}>Next →</button>
    </div>
  {:else}
    <div class="empty-state">
      <p>No attractions available</p>
    </div>
  {/if}
</div>

<style>
  .attractions-container {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    color: white;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    display: flex;
    flex-direction: column;
    padding: 2rem;
  }

  .header {
    display: flex;
    align-items: center;
    gap: 2rem;
    margin-bottom: 2rem;
  }

  .back-button {
    padding: 0.75rem 1.5rem;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid rgba(255, 255, 255, 0.2);
    color: white;
    border-radius: 0.5rem;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    transition: all 0.3s ease;
    font-family: inherit;
  }

  .back-button:hover {
    background: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.4);
  }

  .header h1 {
    margin: 0;
    font-size: 2.5rem;
    flex: 1;
  }

  .attractions-showcase {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 400px;
  }

  .attraction-card {
    padding: 2rem;
    background: linear-gradient(135deg, #0f3460 0%, #16213e 100%);
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-radius: 1.5rem;
    max-width: 600px;
    width: 100%;
    opacity: 0.3;
    transform: scale(0.9);
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
  }

  .attraction-card.active {
    opacity: 1;
    transform: scale(1);
    border-color: #00d4ff;
    box-shadow: 0 12px 48px rgba(0, 212, 255, 0.3);
  }

  .attraction-image {
    width: 100%;
    height: 300px;
    object-fit: cover;
    border-radius: 0.75rem;
    margin-bottom: 1.5rem;
  }

  .attraction-info h2 {
    margin: 0 0 1rem 0;
    font-size: 2rem;
    font-weight: 700;
  }

  .description {
    margin: 0 0 1rem 0;
    font-size: 1rem;
    opacity: 0.8;
    line-height: 1.6;
  }

  .badge {
    display: inline-block;
    padding: 0.5rem 1rem;
    background: #00d4ff;
    color: #1a1a2e;
    border-radius: 2rem;
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 1rem;
  }

  .distance {
    font-size: 0.95rem;
    opacity: 0.8;
  }

  .navigation-hints {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 3rem;
    margin-top: 2rem;
  }

  .navigation-hints button {
    padding: 0.75rem 1.5rem;
    background: rgba(0, 212, 255, 0.2);
    border: 2px solid #00d4ff;
    color: #00d4ff;
    border-radius: 0.5rem;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.3s ease;
    font-family: inherit;
  }

  .navigation-hints button:hover {
    background: #00d4ff;
    color: #1a1a2e;
  }

  .counter {
    font-size: 1rem;
    opacity: 0.7;
  }

  .empty-state {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    opacity: 0.5;
  }

  @media (max-width: 768px) {
    .attractions-container {
      padding: 1rem;
    }

    .header {
      flex-direction: column;
      align-items: flex-start;
      gap: 1rem;
    }

    .header h1 {
      font-size: 1.75rem;
    }

    .attraction-card {
      padding: 1rem;
    }

    .attraction-image {
      height: 200px;
    }
  }
</style>
