<script lang="ts" context="module">
  import type { Load } from "@sveltejs/kit/types/internal";
  import { sl3 } from "$lib/clients/sl3";

  function generateItemFilters(now: Date): {scheduledFilter: ItemFilter, acquiredFilter: ItemFilter, looseFilter: ItemFilter} {
    return {
      scheduledFilter: {
        scheduledDate: datesOf(parseInterval("next:7d", now)),
      },
      acquiredFilter: {
        acquiredTime: morningInterval(parseInterval("today", now)),
      },
      looseFilter: {
        loose: true,
        unAcquired: true,
        unScheduled: true,
      },
    }
  }

  export const load: Load = async({ fetch, stuff }) => {
    const client = sl3(fetch, stuff.idToken);

    const scopes = await client.listScopes();

    const {acquiredFilter, looseFilter, scheduledFilter} = generateItemFilters(new Date());

    const [scheduledItems, acquiredItems, looseItems, sprints] = await Promise.all([
      client.listItems("ALL", scheduledFilter),
      client.listItems("ALL", acquiredFilter),
      client.listItems("ALL", looseFilter),
      client.listSprints("ALL"),
    ]);

    return {
      props: { scopes, scheduledItems, acquiredItems, looseItems, sprints }
    };
	}
</script>

<script lang="ts">
  import Column from "$lib/components/layout/Column.svelte";
  import Columns from "$lib/components/layout/Columns.svelte";
  import Header from "$lib/components/layout/Header.svelte";
  import Row from "$lib/components/layout/Row.svelte";
  import type Scope from "$lib/models/scope";
  import ScopeLinkList from "$lib/components/frontpage/ScopeLinkList.svelte";
  import ScopeCreateUpdateModal from "$lib/modals/ScopeCreateUpdateModal.svelte";
  import OptionsRow from "$lib/components/layout/OptionsRow.svelte";
  import Option from "$lib/components/layout/Option.svelte";
  import ScopeListContext from "$lib/components/contexts/ScopeListContext.svelte";
  import parseInterval, { datesOf, morningInterval } from "$lib/utils/timeinterval";
  import ItemMultiListContext from "$lib/components/contexts/ItemMultiListContext.svelte";
  import type Item from "$lib/models/item";
  import DeletionModal from "$lib/modals/DeletionModal.svelte";
  import ItemAcquireModal from "$lib/modals/ItemAcquireModal.svelte";
  import ItemListRow from "$lib/components/scope/ItemListRow.svelte";
  import { getTimeContext } from "$lib/components/contexts/TimeContext.svelte";
  import type { ItemFilter } from "$lib/models/item";
  import ItemCreateModal from "$lib/modals/ItemCreateModal.svelte";
  import SprintList from "$lib/components/scope/SprintList.svelte";
  import SprintCreateUpdateModal from "$lib/modals/SprintCreateUpdateModal.svelte";
  import SprintListContext from "$lib/components/contexts/SprintListContext.svelte";
  import type Sprint from "$lib/models/sprint";
  import Icon from "$lib/components/layout/Icon.svelte";

  export let scopes: Scope[] = [];
  export let acquiredItems: Item[];
  export let scheduledItems: Item[];
  export let looseItems: Item[];
  export let sprints: Sprint[];

  const {now} = getTimeContext();
</script>

<Header fullwidth wide subtitle="Logging your stuff">Stufflog</Header>

<svelte:head>
    <title>Stufflog 3</title>
</svelte:head>

<ItemMultiListContext global lists={{acquiredItems, scheduledItems, looseItems}} filters={generateItemFilters($now)}>
  <ScopeListContext scopes={scopes}>
    <SprintListContext global sprints={sprints} intervalString="">
      <Columns wide>
        <Column>
          <Row title="Scopes">
            <OptionsRow slot="right">
              <Option open={{name: "scope.create"}}><Icon name="plus" /></Option>
            </OptionsRow>
            <ScopeLinkList />
          </Row>
          <ItemListRow title="Scheduled" key="scheduleItems" />
          <ItemListRow title="Today" key="acquiredItems" showAcquiredTime />
          <ItemListRow title="Loose" key="looseItems" />
        </Column>
        <Column>
          <Row title="Sprints">
            <SprintList sub />
          </Row>
        </Column>
      </Columns>
      
      <ItemCreateModal />
      <ItemAcquireModal />
      <DeletionModal />
      <ScopeCreateUpdateModal />
      <SprintCreateUpdateModal />
    </SprintListContext>
  </ScopeListContext>
</ItemMultiListContext>