<script>
    import { page } from '$app/stores';
    import { goto } from '$app/navigation';
	import { onMount, onDestroy } from 'svelte';

	import { boot, subscribeToEvents } from '$lib/apiServer';
	import { showMenu, currentUser, logins, channelsList, messages } from '$lib/store';

    import ChannelList from '$lib/components/ChannelList.svelte';
    import CreateChannelForm from '$lib/components/CreateChannelForm.svelte';
    import MessageInput from '$lib/components/MessageInput.svelte';

    let loading = true;
    let events = null;
    let showMenuValue;
    showMenu.subscribe((value) => {
        showMenuValue = value;
    });


    function toggleMenu() {
        showMenu.update((value) => !value);
    }

    $: channel = $page?.params?.channel;

    function onBooted(boot) {
        currentUser.update(() => ({
            id: boot.login.id,
            username: boot.login.name,
        }));
        logins.update((value) => value.setLogins(boot.logins));
        channelsList.update((value) => value.setChannels(boot.channels));
        messages.update((value) => value.setMessages(boot.messages));
    }

	onMount(async () => {
        let response = await boot();
        switch (response.status) {
            case 200:
                onBooted(response.data);
                events = subscribeToEvents(response.data.resume_point);
                break;
            case 401:
                currentUser.update(() => null);
                goto('/login');
                break;
            case 503:
                currentUser.update(() => null);
                goto('/setup');
                break;
            default:
                // TODO: display error.
                break;
        }
        loading = false;
	});

    onDestroy(async () => {
        if (events !== null) {
            events.close();
        }
    });
</script>

{#if loading}
    <h2>Loading&hellip;</h2>
{:else}
    <div id="interface" class="p-2">
        <main>
            <div class="active-channel border border-solid border-gray-400 rounded-[1.25rem]">
                <slot />
            </div>
            <div class="create-message overflow-scroll max-h-full">
                <MessageInput {channel} />
            </div>
        </main>

        <nav id="sidebar" data-expanded={showMenuValue}>
            <button class="h-4 w-4" aria-controls="sidebar" aria-expanded={showMenuValue} on:click={toggleMenu}>&#10006;</button>
            <div class="channel-list">
                <ChannelList active={channel} />
            </div>
            <div class="create-channel">
                <CreateChannelForm />
            </div>
        </nav>
    </div>
{/if}

<style>
:root {
    --app-bar-height: 5rem;
    --input-row-height: 2rem;
}

#interface {
  margin: unset;
  /* min-block-size: 100%;*/
  display: grid;
  grid-template: 
    'side main' 1fr
    / auto 1fr
  ;
  
  @media (width > 50em) {
    --toggle-display: none;
    --overlay: static;
    --translate: 0;
  }
}
nav {
    grid-area: side;
    background-color: rgb(var(--color-surface-600));
    inset: auto auto 0 0;
    padding: 0.25rem;
    position: var(--overlay, absolute);
    transition: translate 300ms ease-out;
    height: calc(100vh - var(--app-bar-height));
    width: 21rem;
}
nav button {
    position: absolute;
    top: 0;
    right: 0;
}
main {
    grid-area: main;
    height: calc(100vh - var(--app-bar-height));
}
.active-channel {
    height: calc(100vh - var(--app-bar-height) - var(--input-row-height));
    overflow: scroll;
}
.channel-list {
    height: calc(100vh - var(--app-bar-height) - var(--input-row-height));
    overflow: scroll;
}
nav[data-expanded=false] {
  translate: var(--translate, -100% 0);
}
[aria-controls="sidebar"] {
  display: var(--toggle-display, revert);
}

</style>
