<script>
    import { onMount, createEventDispatcher } from 'svelte';
    import { invoke } from '@tauri-apps/api/tauri';

    import { navigate } from '../lib/routing.js';
    import Nav from '../ui/Nav.svelte';
    import Icon from '../ui/Icon.svelte';

    export let appState;

    const dispatch = createEventDispatcher();
    onMount(async () => {
        // will block until a request comes in
        let req = await appState.pendingRequests.get();
        appState.currentRequest = req;
        navigate('Approve');
    });

    let status = 'unknown';
    onMount(async() => {
        status = await invoke('get_session_status');
    })
</script>


<Nav />

{#if status === 'locked'}
    <div class="flex flex-col h-screen justify-center items-center space-y-4">
        <img src="/static/padlock-closed.svg" alt="An unlocked padlock" class="w-32" />
        <h2 class="text-2xl font-bold">Creddy is locked</h2>
        <Link target="Unlock">
            <button class="btn btn-primary">Unlock</button>
        </Link>
    </div>

{:else if status === 'unlocked'}
    <div class="flex flex-col h-screen justify-center items-center space-y-4">
        <img src="/static/padlock-open.svg" alt="An unlocked padlock" class="w-24" />
        <h2 class="text-2xl font-bold">Waiting for requests</h2>
    </div>

{:else if status === 'empty'}
    <Link target="EnterCredentials">
        <button class="btn btn-primary">Enter Credentials</button>
    </Link>
{/if}