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

    import { appState } from '../lib/state.js';
    import Nav from '../ui/Nav.svelte';
    import Link from '../ui/Link.svelte';
    import ErrorAlert from '../ui/ErrorAlert.svelte';
    import SettingsGroup from '../ui/settings/SettingsGroup.svelte';
    import Keybind from '../ui/settings/Keybind.svelte';
    import { Setting, ToggleSetting, NumericSetting, FileSetting, TextSetting } from '../ui/settings';

    import { fly } from 'svelte/transition';
    import { backInOut } from 'svelte/easing';


    let error = null;
    async function save() {
        console.log('updating config');
        try {
            await invoke('save_config', {config: $appState.config});
        }
        catch (e) {
            error = e;
            $appState.config = await invoke('get_config');
        }
    }

    let osType = null;
    type().then(t => osType = t);
</script>


<Nav>
    <h1 slot="title" class="text-2xl font-bold">Settings</h1>
</Nav>

{#await invoke('get_config') then config}
    <div class="max-w-lg mx-auto mt-1.5 p-4 space-y-16">
        <SettingsGroup name="General">            
            <ToggleSetting title="Start on login" bind:value={$appState.config.start_on_login} on:update={save}>
                <svelte:fragment slot="description">
                    Start Creddy when you log in to your computer.
                </svelte:fragment>
            </ToggleSetting>

            <ToggleSetting title="Start minimized" bind:value={$appState.config.start_minimized} on:update={save}>
                <svelte:fragment slot="description">
                    Minimize to the system tray at startup.
                </svelte:fragment>
            </ToggleSetting>

            <NumericSetting title="Re-hide delay" bind:value={$appState.config.rehide_ms} min={0} unit="Milliseconds" on:update={save}>
                <svelte:fragment slot="description">
                    How long to wait after a request is approved/denied before minimizing
                    the window to tray. Only applicable if the window was minimized
                    to tray before the request was received.
                </svelte:fragment>
            </NumericSetting>

            <NumericSetting
                title="Listen port"
                bind:value={$appState.config.listen_port}
                min={osType === 'Windows_NT' ? 1 : 0}
                on:update={save}
            >
                <svelte:fragment slot="description">
                    Listen for credentials requests on this port. 
                    (Should be used with <code>$AWS_CONTAINER_CREDENTIALS_FULL_URI</code>)
                </svelte:fragment>
            </NumericSetting>

            <Setting title="Update credentials">
                <Link slot="input" target="EnterCredentials">
                    <button class="btn btn-sm btn-primary">Update</button>
                </Link>
                <svelte:fragment slot="description">
                    Update or re-enter your encrypted credentials.
                </svelte:fragment>
            </Setting>

            <FileSetting
                title="Terminal emulator"
                bind:value={$appState.config.terminal.exec}
                on:update={save}
            >
                <svelte:fragment slot="description">
                    Choose your preferred terminal emulator (e.g. <code>gnome-terminal</code> or <code>wt.exe</code>.) May be an absolute path or an executable discoverable on <code>$PATH</code>.
                </svelte:fragment>
            </FileSetting>
        </SettingsGroup>

        <SettingsGroup name="Hotkeys">
            <div class="space-y-4">
                <p>Click on a keybinding to modify it. Use the checkbox to enable or disable a keybinding entirely.</p>

                <div class="grid grid-cols-[auto_1fr_auto] gap-y-3 items-center">
                    <Keybind description="Show Creddy" value={$appState.config.hotkeys.show_window} on:update={save} />
                    <Keybind description="Launch terminal" value={$appState.config.hotkeys.launch_terminal} on:update={save} />
                </div>
            </div>
        </SettingsGroup>

    </div>
{/await}

{#if error}
    <div transition:fly={{y: 100, easing: backInOut, duration: 400}} class="toast">
        <div class="alert alert-error no-animation">
            <div>
                <span>{error}</span>
            </div>

            <div>
                <button class="btn btn-sm btn-alert-error" on:click={() => error = null}>Ok</button>
            </div>
        </div>
    </div>
{/if}
