<script lang="ts">
interface Props {
    log_lvl?: number;
    lq__event_obj: any;
    lq__event_location_obj: any;
}

let { log_lvl = 0, lq__event_obj, lq__event_location_obj }: Props = $props();

import { goto } from '$app/navigation';
import { Modal } from 'flowbite-svelte';
import {
    Info,
    List,
    MapPin,
    Pencil,
    Plane,
    Settings,
    ToggleLeft,
    ToggleRight,
    Wand2,
    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 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);

async function on_toggle(field: string, new_val: boolean) {
    await api.update_ae_obj({
        api_cfg: $ae_api,
        obj_type: 'event_location',
        obj_id: $lq__event_location_obj?.event_location_id,
        fields: { [field]: new_val }
    });
    events_func.load_ae_obj_id__event_location({
        api_cfg: $ae_api,
        event_location_id: $lq__event_location_obj?.event_location_id,
        log_lvl
    });
}

async function on_delete(method: 'delete' | 'disable') {
    await (events_func as any).delete_ae_obj_id__event_location({
        api_cfg: $ae_api,
        event_location_id: $lq__event_location_obj.event_location_id,
        method
    });
    $events_slct.event_location_id = null;
    goto(`/events/${$lq__event_location_obj.event_id}/locations`);
}
</script>

<div class="pres_mgmt__location_menu ae_container_module_menu">
    <div
        class="flex w-full flex-row flex-wrap items-center justify-around gap-1">
        <Comp__events_menu_nav
            hide={!$ae_loc.authenticated_access}
            event_id={$lq__event_obj?.event_id}
            ae_core={$ae_loc.edit_mode && $ae_loc.manager_access}
            events__locations={$ae_loc.trusted_access}
            events__session_search={$events_slct.event_id &&
                (!pres_mgmt_loc.current.limit__navigation || $ae_loc.trusted_access)} />

        <span
            class="ae_menu__object_options flex flex-row flex-wrap items-center justify-around 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="Location 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 location">
                <Info size="1em" class="mr-1" />
                {show_help ? 'Hide Help' : 'Help'}
            </button>
        </span>
    </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" />
                Location 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">
            <!-- Launcher & Location Link Toggles -->
            {#if $ae_loc.administrator_access}
                <section>
                    <h4
                        class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                        Display Links
                    </h4>
                    <div class="flex flex-col gap-1">
                        {#if pres_mgmt_loc.current.show__launcher_link || $ae_loc.trusted_access}
                            <button
                                type="button"
                                onclick={() => {
                                    pres_mgmt_loc.current.hide__launcher_link =
                                        !pres_mgmt_loc.current.hide__launcher_link;
                                }}
                                class="btn btn-sm ae_btn_surface w-full justify-between text-center">
                                {#if pres_mgmt_loc.current.hide__launcher_link}<ToggleLeft
                                        size="1em"
                                        class="m-1" />{:else}<ToggleRight
                                    size="1em"
                                    class="m-1" />{/if}
                            <span class="grow">
                                <Plane size="1em" class="m-1" />
                                {pres_mgmt_loc.current.hide__launcher_link
                                    ? 'Show Launcher Links'
                                    : 'Hide Launcher Links?'}
                            </span>
                            </button>
                        {/if}

                        <button
                            type="button"
                            onclick={() => {
                                pres_mgmt_loc.current.hide__location_link =
                                    !pres_mgmt_loc.current.hide__location_link;
                            }}
                            class="btn btn-sm ae_btn_surface w-full justify-between text-center">
                            {#if pres_mgmt_loc.current.hide__location_link}<ToggleLeft
                                    size="1em"
                                    class="m-1" />{:else}<ToggleRight
                                    size="1em"
                                    class="m-1" />{/if}
                            <span class="grow">
                                <MapPin size="1em" class="m-1" />
                                {pres_mgmt_loc.current.hide__location_link
                                    ? 'Show Location Links'
                                    : 'Hide Location Links?'}
                            </span>
                        </button>

                        <!-- Linked Files / Presentations (testing) -->
                        <button
                            type="button"
                            disabled={!$ae_loc.manager_access}
                            onclick={() => {
                                pres_mgmt_loc.current.show_content__session_files =
                                    !pres_mgmt_loc.current.show_content__session_files;
                            }}
                            class="btn btn-sm w-full justify-between text-center"
                            class:ae_btn_surface={pres_mgmt_loc.current.show_content__session_files}
                            class:ae_btn_surface_outlined={!pres_mgmt_loc.current.show_content__session_files}>
                            {#if pres_mgmt_loc.current.show_content__session_files}<ToggleRight
                                    size="1em"
                                    class="m-1" />{:else}<ToggleLeft
                                    size="1em"
                                    class="m-1" />{/if}
                            <span class="grow">
                                <List size="1em" class="m-1" />
                                {pres_mgmt_loc.current.show_content__session_files
                                    ? 'Hide Linked Files (testing)'
                                    : 'Show Linked Files? (testing)'}
                            </span>
                        </button>

                        <button
                            type="button"
                            disabled={!$ae_loc.manager_access}
                            onclick={() => {
                                pres_mgmt_loc.current.show_content__session_presentations =
                                    !pres_mgmt_loc.current.show_content__session_presentations;
                            }}
                            class="btn btn-sm w-full justify-between text-center"
                            class:ae_btn_surface={pres_mgmt_loc.current.show_content__session_presentations}
                            class:ae_btn_surface_outlined={!pres_mgmt_loc.current.show_content__session_presentations}>
                            {#if pres_mgmt_loc.current.show_content__session_presentations}<ToggleRight
                                    size="1em"
                                    class="m-1" />{:else}<ToggleLeft
                                    size="1em"
                                    class="m-1" />{/if}
                            <span class="grow">
                                <List size="1em" class="m-1" />
                                {pres_mgmt_loc.current.show_content__session_presentations
                                    ? 'Hide Linked Presentations (testing)'
                                    : 'Show Linked Presentations? (testing)'}
                            </span>
                        </button>
                    </div>
                </section>
            {/if}

            <!-- Mode toggles -->
            {#if $ae_loc.trusted_access}
                <section>
                    <h4
                        class="text-surface-500 mb-2 text-xs font-semibold tracking-wider uppercase">
                        Mode
                    </h4>
                    <div class="flex flex-row flex-wrap gap-2">
                        <button
                            type="button"
                            onclick={() =>
                                ($ae_loc.edit_mode = !$ae_loc.edit_mode)}
                            class="btn btn-sm"
                            class:ae_btn_warning={$ae_loc.edit_mode}
                            class:ae_btn_warning_outlined={!$ae_loc.edit_mode}>
                            <Pencil size="1em" class="mr-1" />
                            {$ae_loc.edit_mode ? 'Edit Mode On' : 'Edit Mode?'}
                        </button>

                        <button
                            type="button"
                            onclick={() =>
                                ($ae_loc.adv_mode = !$ae_loc.adv_mode)}
                            class="btn btn-sm"
                            class:ae_btn_warning={$ae_loc.adv_mode}
                            class:ae_btn_warning_outlined={!$ae_loc.adv_mode}>
                            <Wand2 size="1em" class="mr-1" />
                            {$ae_loc.adv_mode
                                ? 'Advanced Mode On'
                                : 'Advanced Mode?'}
                        </button>
                    </div>
                </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_location_obj}
                        obj_label="location"
                        show_alert={false}
                        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__location_help"
        ds_name="Default: Events - Pres Mgmt Location Help"
        ds_type="html"
        for_type="event"
        for_id={$lq__event_location_obj?.event_id}
        class_li="ae_container_module_help"
        show_edit={false}
        show_edit_btn={true}
        hide={!show_help} />
</div>
