<script lang="ts">
    interface Props {
        // /** @type {import('./$types').PageData} */
        data: any;
        event_session_id: string;
        lq__event_session_obj: any;
        lq__auth__event_presenter_obj: any;
    }

    let {
        data,
        event_session_id,
        lq__event_session_obj,
        lq__auth__event_presenter_obj
    }: Props = $props();

    import { onMount } from 'svelte';
    import { goto } from '$app/navigation';

    import {
        ae_snip,
        ae_loc,
        ae_sess,
        ae_api,
        ae_trig,
        slct,
        slct_trigger
    } from '$lib/stores/ae_stores';
    import {
        events_loc,
        events_sess,
        events_slct,
        events_trigger,
        events_trig_kv
    } from '$lib/stores/ae_events_stores';

    onMount(() => {
        console.log('Browser environment detected.');

        console.log(
            `lq__auth__event_presenter_obj = `,
            $lq__auth__event_presenter_obj
        );

        let url_person_id = data.url.searchParams.get('person_id');
        let url_person_pass = data.url.searchParams.get('person_pass');
        let url_presentation_id = data.url.searchParams.get('presentation_id');
        let url_presenter_id = data.url.searchParams.get('presenter_id');
        let url_session_id = data.url.searchParams.get('session_id');

        // This should be turned into a function to correctly authenticate the person and allow them access to their presentations and presenter details.
        if (url_person_pass) {
            console.log(
                `ae_events_pres_mgmt session [slug] +page.svelte: event_session_id=${$events_slct.event_session_id}; person_id=${url_person_id}; person_pass=${url_person_pass}; presentation_id=${url_presentation_id}; presenter_id=${url_presenter_id}`
            );

            // Using temporary session values first.
            $events_sess.auth__entered_key = url_person_id;
            $events_sess.auth__entered_passcode = url_person_pass;

            $events_sess.auth__person.id = url_person_id;
            $events_sess.auth__person.presentation_id = url_presentation_id;
            $events_sess.auth__person.presenter_id = url_presenter_id;
            $events_sess.auth__person.session_id = url_session_id; // For POC or LCI Champions for sessions. Do not set for a presenter!

            $events_sess.auth__kv.person[url_person_id] = true;
            $events_sess.auth__kv.session[url_session_id] = true; // For POC or LCI Champions for sessions. Do not set for a presenter!
            $events_sess.auth__kv.presentation[url_presentation_id] = true;
            $events_sess.auth__kv.presenter[url_presenter_id] = true;

            // We need to set the selected presentation and presenter objects based on the respective IDs.
            if (url_session_id) {
                $events_slct.auth__event_session_id = url_session_id;
            }
            if (url_presentation_id && url_presenter_id) {
                $events_slct.auth__event_presentation_id = url_presentation_id;
                $events_slct.auth__event_presenter_id = url_presenter_id;
            }

            if (url_presenter_id) {
                presenter_sign_in();
            }

            if (url_session_id) {
                session_sign_in();
            }

            console.log('Remove the sign in fields from the URL.');
            data.url.searchParams.delete('person_id');
            data.url.searchParams.delete('person_pass');
            data.url.searchParams.delete('presentation_id');
            data.url.searchParams.delete('presenter_id');
            data.url.searchParams.delete('session_id');

            let new_url = data.url.toString();
            console.log(new_url);

            // We need to set browser history and force all load functions to rerun.
            goto(new_url, { replaceState: true, invalidateAll: true });
        }
    });

    // For session point of contact (moderator, chair, LCI Champions).
    function session_sign_in() {
        console.log('Session sign in with URL values');

        $ae_loc.access_type = 'public'; // Just below trusted access used by staff.
        // $ae_loc.administrator_access = false;
        // $ae_loc.trusted_access = false;
        $ae_loc.public_access = true;
        $ae_loc.authenticated_access = true;

        $events_loc.auth__person.id = $events_sess.auth__person.id; // person_id
        $events_loc.auth__person.person_id = $events_sess.auth__person.id;
        $events_loc.auth__person.entered_key = $events_sess.auth__entered_key; // need to verify entered_key...

        $events_loc.auth__kv.person[$events_sess.auth__person.person_id] = true;
        $events_loc.auth__kv.session[$events_sess.auth__person.session_id] =
            true;
        $events_loc.auth__kv.presentation[
            $events_sess.auth__person.presentation_id
        ] = false; // Set to false for session POC (LCI Champions).
        $events_loc.auth__kv.presenter[$events_sess.auth__person.presenter_id] =
            false; // Set to false for session POC (LCI Champions).

        // Setting again here... just because for now.
        // $events_slct.event_presentation_id = $events_sess.auth__person.presentation_id;
        // $events_slct.event_presenter_id = $events_sess.auth__person.presenter_id;
    }

    // For presenters within a session presentation.
    function presenter_sign_in() {
        console.log('Presenter sign in with URL values');

        console.log(event_session_id);

        $ae_loc.access_type = 'authenticated'; // Just below public access used for the session POC.
        // $ae_loc.administrator_access = false;
        // $ae_loc.trusted_access = false;
        $ae_loc.public_access = false;
        $ae_loc.authenticated_access = true;

        $ae_loc.person_id = $events_sess.auth__person.id;

        $events_loc.auth__person.id = $events_sess.auth__person.id; // person_id
        $events_loc.auth__person.person_id = $events_sess.auth__person.id;
        // $events_loc.auth__person.entered_key = $events_sess.auth__entered_key; // also person_id in this case
        // $lq__auth__event_presenter_obj.full_name
        // $events_loc.auth__person.email = 'test@example.com';
        // $events_loc.auth__person.full_name = 'Test Person';
        $events_loc.auth__person.presenter_id =
            $events_sess.auth__person.presenter_id;

        $events_loc.auth__kv.person[$events_sess.auth__person.person_id] = true;
        // $events_loc.auth__kv.session[$events_sess.auth__person.session_id] = 'read'; // Set to 'read' for presenters.
        $events_loc.auth__kv.session[event_session_id] = 'read'; // Set to 'read' for presenters.
        $events_loc.auth__kv.presentation[
            $events_sess.auth__person.presentation_id
        ] = true; // true implies read/write access
        $events_loc.auth__kv.presenter[$events_sess.auth__person.presenter_id] =
            true; // true implies read/write access

        // Setting again here... just because for now.
        // $events_slct.auth__event_presentation_id = $events_sess.auth__person.presentation_id;
        // $events_slct.auth__event_presenter_id = $events_sess.auth__person.presenter_id;

        $events_loc.auth__person.presentation_id =
            $events_sess.auth__person.presentation_id;
        $events_loc.auth__person.presenter_id =
            $events_sess.auth__person.presenter_id;
    }

    function sign_out() {
        $ae_loc.authenticated_access = false;
        $ae_loc.access_type = 'anonymous';

        $events_loc.auth__person = {
            id: null,
            email: null,
            full_name: null,
            entered_key: null,
            entered_passcode: null,
            person_id: null,
            presenter_id: null,
            presentation_id: null,
            session_id: null
        };
        $events_loc.auth__kv = {
            event: {},
            session: {},
            presentation: {},
            presenter: {},
            person: {}
        };

        $events_sess.auth__person.presenter_id = null;
        // $events_sess.auth__person.presentation_id = null;
        $events_sess.auth__person.session_id = null;
    }
</script>

<span>
    {#if $events_loc.auth__person?.id}
        <button
            type="button"
            class="btn btn-sm preset-tonal-warning"
            onclick={() => {
                sign_out();
                alert('You have been signed out.');
            }}
        >
            <span class="fas fa-times mx-1"></span>
            Sign Out
            {$lq__auth__event_presenter_obj?.given_name
                ? `- ${$lq__auth__event_presenter_obj?.given_name}`
                : ''}
        </button>
    {:else if $events_sess.auth__person.presenter_id || $events_sess.auth__person.session_id}
        <div
            class="border border-gray-200 p-2 rounded-md m-2 text-center space-y-2"
        >
            <div class="text-sm text-gray-500">
                Use the link that was sent to your email address to sign in or
                enter your key and passcode to sign in.
            </div>
            <div>
                <input
                    type="text"
                    class="input max-w-36"
                    placeholder="Key"
                    value={$events_sess.auth__entered_key ?? ''}
                    oninput={(e) =>
                        ($events_sess.auth__entered_key = (e.target as HTMLInputElement).value)}
                />
                <input
                    type="text"
                    class="input max-w-36"
                    placeholder="Passcode"
                    value={$events_sess.auth__entered_passcode ?? ''}
                    oninput={(e) =>
                        ($events_sess.auth__entered_passcode = (e.target as HTMLInputElement).value)}
                />
                <button
                    type="button"
                    class="btn btn-md preset-tonal-primary border border-primary-500"
                    onclick={() => {
                        console.log('Sign in with URL values');

                        if ($events_sess.auth__person.presenter_id) {
                            presenter_sign_in();
                        } else if ($events_sess.auth__person.session_id) {
                            session_sign_in();
                        }

                        // This needs to be moved to a function!

                        // $events_loc.auth__person.id = $events_sess.auth__entered_key; // person_id
                        // $events_loc.auth__person.entered_key = $events_sess.auth__entered_key; // also person_id in this case
                        // $events_loc.auth__person.email = 'test@example.com';
                        // $events_loc.auth__person.full_name = 'Test Person';

                        // $events_loc.auth__kv.person[$events_sess.entered_key] = true;
                        // $events_loc.auth__kv.session[$events_slct.event_session_id] = true;
                        // $events_loc.auth__kv.presentation[$events_sess.auth__person.presentation_id] = true;
                        // $events_loc.auth__kv.presenter[$events_sess.auth__person.presenter_id] = true;

                        // Setting again here... just because for now.
                        // $events_slct.event_presentation_id = $events_sess.auth__person.presentation_id;
                        // $events_slct.event_presenter_id = $events_sess.auth__person.presenter_id;
                    }}
                >
                    <span class="fas fa-sign-in-alt mx-1"></span>
                    Sign In
                </button>
            </div>

            <div class="text-sm text-gray-500">
                For: session: {$events_sess.auth__person.session_id}
                presentation: {$events_sess.auth__person.presentation_id}
                presenter: {$events_sess.auth__person.presenter_id}
                <br />

                <!-- {$lq__event_presentation_obj?.name ? `Name: "${$lq__event_presentation_obj.name}"` : '-- not set --'} -->
            </div>

            <!-- {#if $events_loc.auth__person?.id}
        <button type="button"
            class="btn btn-sm variant-soft-warning"
            on:click={() => {
                sign_out();
                alert('You have been signed out.');
            }}
            >
            <span class="fas fa-times mx-1"></span>
            Sign Out
        </button>
    {/if} -->
        </div>
    {/if}
</span>
