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

    export let appState;

    const dispatch = createEventDispatcher();

    async function approve() {
        let status = await invoke('get_session_status');
        if (status === 'unlocked') {
            dispatch('navigate', {target: 'ShowApproved'});
        }
        else if (status === 'locked') {
            dispatch('navigate', {target: 'Unlock'})
        }
        else {
            dispatch('navigate', {target: 'EnterCredentials'});
        }
    }

    function deny() {
        dispatch('navigate', {target: 'ShowDenied'});
    }

    function handleHotkey(event) {
        if (event.shiftKey && (event.code === 'Enter' || event.code === 'NumpadEnter')) {
            approve();
        }
        else if (event.code === 'Escape') {
            deny();
        }
    }
</script>

<svelte:window on:keydown={handleHotkey} />

<h2 class="text-3xl text-gray-200">An application would like to access your AWS credentials.</h2>

<div class="text-center">
    <ul class="text-gray-200">
        {#each appState.currentRequest.clients as client}
            <li>PID: {client.pid}</li>
            <li>Path: {client.exe}</li>
        {/each}
    </ul>

    <button on:click={approve}>
        <svg class="w-32 stroke-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1">
          <path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
        </svg>
    </button>

    <button on:click={deny}>
        <svg class="w-32 stroke-red-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1">
            <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
        </svg>
    </button>
</div>