<script>
  import { link } from 'svelte-spa-router';
  import { onMount } from 'svelte';
  import { defer } from "$/client/helpers.js";
  import Layout from "$/client/Layout.svelte";
  import IconImage from "$/client/components/IconImage.svelte";
  import SnapImage from "$/client/components/SnapImage.svelte";
  import Spinner from "$/client/components/Spinner.svelte";
  import Markdown from "$/client/components/Markdown.svelte";
  import formatDistanceToNow from "date-fns/formatDistanceToNow";
  import parseISO from "date-fns/parseISO";
  import { load_past_streams } from "$/client/livestreams.js";

  let load_defer = defer("load_defer");
  let streams;

  const streaming_info = {
    title: "Livestreams",
    description: "Past, present, and future live education."
  }

  onMount(async () => {
    streams = await load_past_streams();
    load_defer.resolve();
  });
</script>

<style>
  streams {
    display: flex;
    flex-direction: column;
    width: 100%;
    padding-top: 0.5em;
  }

  streams card {
    width: 100%;
    border: 0px;
    border-bottom: 1px solid var(--value5);
    border-radius: unset;
    box-shadow: unset;
  }

  streams card middle {
    height: 100%;
    padding-left: 0px;
    padding-right: 0px;
  }

  streams card bottom {
    font-size: 0.9em;
    color: var(--color-border-tabs);
    padding-bottom: 0.5rem;
  }

  streams card top {
    display: flex;
    flex-direction: row;
    justify-content: start;
    padding-top: 0.5rem;
  }

  title-time {
    display: flex;
    flex-direction: row;
    flex-grow: 10;
  }

  title-time span.start-time {
    font-size: 0.9em;
  }

  title-time h4 {
    margin-top: 0px;
    flex-grow: 10;
  }

  @media only screen and (max-width: 700px) {
    content {
      width: 650px;
    }
  }

  @media only screen and (max-width: 800px) {
    content {
      width: 100%;
      flex-direction: column-reverse;
    }

    right {
      width: 100%;
    }

    left {
      width: 100%;
      max-width: unset;
      min-width: unset;
      box-shadow: unset;
    }
  }
</style>

<Layout auth_optional={ true } testid="streams-page" footer={ false } header={ true } fixed={ true }>
  <content>
  {#await load_defer}
    <Spinner color="var(--value4)" aspect_ratio="16/9" />
  {:then}
    <left>
      <h1>Sidebar</h1>
    </left>

    <right>
      <streams>
      {#each streams as stream}
        <card>
        <top>
          <title-time>
            <h4>
              <a data-testid="stream-link-{ stream.id }" href="/live/{ stream.id }/" use:link>
                { stream.title }
              </a>
            </h4>
            {#if stream.state === "pending"}
              <span class="start-time">{ formatDistanceToNow(parseISO(stream.starts_on), {addSuffix: true}) }</span>
            {:else if stream.state === "live"}
              <span class="start-time">Live Now!</span>
            {:else}
              <span class="start-time">{ formatDistanceToNow(parseISO(stream.updated_at), {addSuffix: true}) }</span>
            {/if}
          </title-time>

        </top>

        <middle>
        <a href="/live/{ stream.id }/" use:link>
              {#if stream.poster }
                <figure>
                  <SnapImage src={ stream.poster } />
                </figure>
              {:else}
                <IconImage name="video" />
              {/if}
            </a>
        </middle>

        <bottom>
          <Markdown content={ stream.description } />
        </bottom>
        </card>
      {/each}
      </streams>
    </right>
  {/await}
  </content>
</Layout>

