<script lang="ts">
interface Props {
    data: any;
    log_lvl?: number;
    lq__event_session_obj: any;
    lq__auth__event_presenter_obj: any;
}

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

import { goto } from '$app/navigation';
import { Modal } from 'flowbite-svelte';
import { Clock, Info, QrCode, Send, Settings, ToggleLeft, ToggleRight, X } from '@lucide/svelte';
import { ae_loc, ae_api } from '$lib/stores/ae_stores';
import { events_slct } from '$lib/stores/ae_events_stores';
import { pres_mgmt_loc } from '$lib/stores/ae_events_stores__pres_mgmt.svelte';
import { events_func } from '$lib/ae_events/ae_events_functions';

import { api } from '$lib/ae_api/api';
import Element_data_store from '$lib/elements/element_data_store.svelte';
import Sign_in_out from '../../../sign_in_out.svelte';
import Comp__events_menu_nav from '../../../../ae_comp__events_menu_nav.svelte';
import AE_Record_Controls from '$lib/elements/ae_record_controls.svelte';

let show_modal = $state(false);
let show_help = $state(false);

// Detect poster sessions so the Launcher link auto-applies kiosk URL params
let is_poster = $derived($lq__event_session_obj?.type_code === 'poster');

async function on_toggle(field: string, new_val: boolean) {
    await api.update_ae_obj({
        api_cfg: $ae_api,
        obj_type: 'event_session',
        obj_id: $lq__event_session_obj?.event_session_id,
        fields: { [field]: new_val }
    });
    events_func.load_ae_obj_id__event_session({
        api_cfg: $ae_api,
        event_session_id: $lq__event_session_obj?.event_session_id,
        log_lvl
    });
}

async function on_delete(method: 'delete' | 'disable') {
    await events_func.delete_ae_obj_id__event_session({
        api_cfg: $ae_api,
        event_session_id: $lq__event_session_obj.event_session_id,
        method
    });
    $events_slct.event_session_id = null;
    $events_slct.event_session_obj = {};
    goto(`/events/${$lq__event_session_obj.event_id}`);
}

async function toggle_hide_launcher() {
    await api.update_ae_obj({
        api_cfg: $ae_api,
        obj_type: 'event_session',
        obj_id: $lq__event_session_obj?.event_session_id,
        fields: {
            hide_event_launcher: !$lq__event_session_obj?.hide_event_launcher
        }
    });
    events_func.load_ae_obj_id__event_session({
        api_cfg: $ae_api,
        event_session_id: $lq__event_session_obj?.event_session_id,
        log_lvl
    });
}
</script>

<div class="pres_mgmt__session_menu ae_container_module_menu">
    <div class="flex w-full flex-col sm:flex-row gap-1 items-center justify-between">
        <!-- launcher_id is the event_location_id; launcher_session_id pre-loads this session in the launcher -->
        <Comp__events_menu_nav
            hide={false}
            event_id={$lq__event_session_obj?.event_id}
            events__reports={$lq__event_session_obj?.event_id &&
                $ae_loc.trusted_access}
            events__session_search={$lq__event_session_obj?.event_id &&
                (!pres_mgmt_loc.current.limit__navigation || $ae_loc.trusted_access)}
            events__launcher_id={$lq__event_session_obj?.event_location_id &&
            $ae_loc.administrator_access
                ? $lq__event_session_obj?.event_location_id
                : null}
            events__launcher_session_id={$lq__event_session_obj?.event_session_id &&
            $ae_loc.administrator_access
                ? $lq__event_session_obj?.event_session_id
                : null}
            events__launcher_extra_params={is_poster
                ? 'iframe=true&launcher_menu=hide'
                : ''}
            events__location_id={$lq__event_session_obj?.event_location_id &&
            $ae_loc.trusted_access
                ? $lq__event_session_obj?.event_location_id
                : null}
            events__locations={$ae_loc.administrator_access} />

        <div
            class="flex w-full flex-row flex-wrap items-center justify-end gap-1">
            <span
                class="ae_menu__object_options flex flex-row flex-wrap items-center gap-0.5">
                <!-- Options modal trigger -->
                {#if $ae_loc.trusted_access}
                    <button
                        type="button"
                        onclick={() => (show_modal = true)}
                        class="btn btn-sm ae_btn_info"
                        title="Session options">
                        <Settings size="1em" class="mr-1" />
                        Options
                    </button>
                {/if}

                <!-- Help toggle -->
                <button
                    type="button"
                    onclick={() => (show_help = !show_help)}
                    class="btn btn-sm"
                    class:ae_btn_info_filled={show_help}
                    class:ae_btn_info={!show_help}
                    title="Help and information about the session">
                    <Info size="1em" class="mr-1" />
                    {show_help ? 'Hide Help' : 'Help'}
                </button>
            </span>

            <span
                class="ae_menu__action_options">
                {#if $lq__event_session_obj?.event_id}
                    <Sign_in_out
                        {data}
                        event_session_id={$lq__event_session_obj?.event_session_id}
                        {lq__event_session_obj}
                        {lq__auth__event_presenter_obj} />
                {/if}
            </span>
        </div>
    </div>

    <!-- Options Modal -->
    <Modal
        bind:open={show_modal}
        autoclose={false}
        dismissable={true}
        placement="top-center"
        size="md"
        class="relative mx-auto flex w-full flex-col rounded-lg bg-white text-gray-800 shadow-xl dark:bg-gray-800 dark:text-gray-200"
        headerClass="flex flex-row gap-2 items-center justify-between w-full bg-surface-100 dark:bg-surface-800 p-4 rounded-t-lg border-b border-surface-200 dark:border-surface-700">
        {#snippet header()}
            <h3 class="flex flex-1 items-center gap-2 text-base font-bold">
                <Settings size="1.1em" class="text-primary-500" />
                Session Options
            </h3>
            <button
                type="button"
                class="btn-icon btn-icon-sm preset-tonal-surface ml-2"
                onclick={() => (show_modal = false)}>
                <X size="1.1em" />
            </button>
        {/snippet}

        <div class="flex flex-col gap-4 p-4">
            <!-- Display -->
            <section>
                <h4
                    class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                    Display
                </h4>
                <button
                    type="button"
                    onclick={() => {
                        pres_mgmt_loc.current.use_12h = !pres_mgmt_loc.current.use_12h;
                    }}
                    class="btn btn-sm w-full justify-between"
                    class:ae_btn_surface={pres_mgmt_loc.current.use_12h}
                    class:ae_btn_surface_outlined={!pres_mgmt_loc.current.use_12h}>
                    {#if pres_mgmt_loc.current.use_12h}<ToggleRight
                            size="1em"
                            class="mr-1" />{:else}<ToggleLeft
                            size="1em"
                            class="mr-1" />{/if}
                    <span class="grow">
                        <Clock size="1em" class="mr-1" />
                        {pres_mgmt_loc.current.use_12h ? '12-Hour Time' : '24-Hour Time'}
                    </span>
                </button>

                <!-- QR Code toggle — only shown to trusted staff and only when
                     admin has NOT already enabled QR globally (if they have,
                     it shows automatically without needing this toggle) -->
                {#if $ae_loc.trusted_access && !pres_mgmt_loc.current.show__session_qr}
                    <button
                        type="button"
                        onclick={() => {
                            pres_mgmt_loc.current.show_content__session_qr =
                                !pres_mgmt_loc.current.show_content__session_qr;
                        }}
                        class="btn btn-sm mt-1 w-full justify-between"
                        class:ae_btn_surface={pres_mgmt_loc.current.show_content__session_qr}
                        class:ae_btn_surface_outlined={!pres_mgmt_loc.current.show_content__session_qr}>
                        {#if pres_mgmt_loc.current.show_content__session_qr}<ToggleRight
                                size="1em"
                                class="mr-1" />{:else}<ToggleLeft
                                size="1em"
                                class="mr-1" />{/if}
                        <span class="grow">
                            <QrCode size="1em" class="mr-1" />
                            {pres_mgmt_loc.current.show_content__session_qr
                                ? 'Hide QR Code'
                                : 'Show QR Code'}
                        </span>
                    </button>
                {/if}
            </section>

            <!-- Launcher Settings -->
            {#if $ae_loc.trusted_access}
                <section>
                    <h4
                        class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                        Launcher
                    </h4>
                    <button
                        type="button"
                        onclick={toggle_hide_launcher}
                        class="btn btn-sm w-full justify-between"
                        class:ae_btn_surface_outlined={!$lq__event_session_obj?.hide_event_launcher}
                        class:ae_btn_success={$lq__event_session_obj?.hide_event_launcher}
                        title={$lq__event_session_obj?.hide_event_launcher
                            ? 'Not showing in Launcher — click to unhide'
                            : 'Showing in Launcher — click to hide'}>
                        <span>
                            {#if $lq__event_session_obj?.hide_event_launcher}
                                <ToggleRight size="1em" class="mr-1" />
                                Unhide from Launcher
                            {:else}
                                <Send size="1em" class="mr-1" />
                                Hide from Launcher?
                            {/if}
                        </span>
                    </button>
                </section>
            {/if}

            <!-- Record Controls -->
            {#if $ae_loc.trusted_access}
                <section>
                    <h4
                        class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                        Record
                    </h4>
                    <AE_Record_Controls
                        obj={$lq__event_session_obj}
                        obj_label="session"
                        show_alert={true}
                        show_priority={true}
                        show_enable={true}
                        show_hide={true}
                        allow_delete={$ae_loc.manager_access}
                        allow_disable={$ae_loc.administrator_access &&
                            !$ae_loc.manager_access}
                        {on_toggle}
                        {on_delete}
                        container_class="flex flex-row flex-wrap gap-2 items-center justify-start" />
                </section>
            {/if}
        </div>
    </Modal>

    <!-- Help panel -->
    <Element_data_store
        ds_code="events__pres_mgmt__session_help"
        ds_name="Default: Events - Pres Mgmt Session Help"
        ds_type="html"
        for_type="event"
        for_id={$lq__event_session_obj?.event_id}
        class_li="ae_container_module_help"
        show_edit={false}
        show_edit_btn={true}
        hide={!show_help} />
</div>
