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

    let { data }: Props = $props();
    let log_lvl: number = $state(0);

    // Imports
    import { untrack } from 'svelte';
    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
    } from '$lib/stores/ae_events_stores';
    import { events_func } from '$lib/ae_events_functions';

    import {
        kill_processes,
        open_local_file_v2,
        run_cmd,
        run_cmd_sync,
        run_osascript,
        get_device_info
    } from '$lib/electron/electron_relay';

    // Variables
    // Quickly save the data passed from the parent(s) to the Svelte stores, localStorage, and other.
    // NOTE: Derived from data.account_id (prop) instead of $slct.account_id (store)
    // to prevent circular dependency loops during hydration.
    let ae_acct = $derived(data[data.account_id]);

    // $ae_loc.url_origin = data.url.origin;

    $effect(() => {
        log_lvl = 1;
        if (log_lvl) {
            console.log(`event_id: ${data.params.event_id}`);
            console.log(`event_location_id: ${data.params.event_location_id}`);
        }
        untrack(() => {
            $events_slct.event_id = data.params.event_id;
            $events_slct.event_location_id = data.params.event_location_id;
        });
    });

    $effect(() => {
        if (ae_acct) {
            untrack(() => {
                $events_slct.event_location_obj_li = ae_acct.slct.event_location_obj_li ?? [
                    ''
                ];
                $events_slct.id_li__event_location = ae_acct.slct.id_li__event_location ?? [
                    ''
                ];
                if (log_lvl) {
                    console.log(
                        `$events_slct.event_location_obj_li:`,
                        $events_slct.event_location_obj_li
                    );
                }
                $events_slct.event_session_obj_li = ae_acct.slct.event_session_obj_li ?? [
                    ''
                ];
            });
        }
    });

    // Set the local storage values
    if (!$events_loc.launcher) {
        $events_loc.launcher = {};
        $events_loc.launcher.slct = {
            event_id: null
        };
        $events_loc.launcher.show_content__session_code = true;
        $events_loc.launcher.show_content__presentation_code = true;
        $events_loc.launcher.show_content__presenter_code = true;
    }
    if (!$events_loc.launcher.slct) {
        $events_loc.launcher.slct = {
            event_id: null,
            event_location_id: null,
            event_session_id: null,
            event_presentation_id: null,
            event_presenter_id: null
        };
    }

    // Set the session storage values
    if (!$events_sess.launcher) {
        $events_sess.launcher = {};
    }

    $events_sess.launcher.show_content__session_code = true;
    $events_sess.launcher.show_content__presentation_code = true;
    $events_sess.launcher.show_content__presenter_code = true;

    // Variables that will be used with Svelte bind are prefixed with 'bi_'.

    let bi_trig__event_location: any = null;
    // let bi_id_li__event_location: string[] = [];

    let bi_trig__event_session: any = null;
    // let bi_id_li__event_session: string[] = [];

    // *** Functions and Logic

    // if (browser) {
    //     console.log(`Browser: ${browser}`);
    // }

    // onMount(() => {
    //     console.log('Pres Mgmt Launcher [slug]: +page@(launcher).svelte');
    // });

    async function handle_get_device_info(event_device_id: string) {
        console.log('*** handle_get_device_info() ***');
        // console.log(event_device_id);

        // console.log('GET DEVICE INFO!!! GET DEVICE INFO!!! GET DEVICE INFO!!!');

        // Using standardized relay method
        let get_device_info_promise = get_device_info();
        get_device_info_promise.then(function (result) {
            // console.log('GOT DEVICE INFO!!! GOT DEVICE INFO!!! GOT DEVICE INFO!!!');
            console.log(get_device_info_promise);
            let event_device_data: any = {};
            event_device_data['other_json'] = JSON.stringify(result);

            event_device_data['info_hostname'] = result.hostname;

            // console.log(event_device_data['other_json']);

            // console.log(result.networkInterfaces);

            let ni_data_li = [];
            let ip_address_li = [];

            for (let network_interface in result.networkInterfaces) {
                // console.log(network_interface);
                let ni_data: any = {};

                if (network_interface.startsWith('en')) {
                    console.log('Wired or wireless network interface found');

                    ni_data['name'] = network_interface;

                    let ni_details =
                        result.networkInterfaces[network_interface];
                    // console.log(ni_details);

                    for (let i = 0; i < ni_details.length; i++) {
                        ni_data['address'] = ni_details[i].address;
                        ni_data['mac'] = ni_details[i].mac;

                        ip_address_li.push(ni_details[i].address);
                    }

                    ni_data_li.push(ni_data);
                }
            }

            event_device_data['meta_json'] = JSON.stringify(ni_data_li);
            console.log(event_device_data['meta_json']);

            event_device_data['info_ip_list'] = ip_address_li.join('; ');
            console.log(event_device_data['info_ip_list']);

            let event_device_obj_up_promise = events_func
                .update_ae_obj__event_device({
                    api_cfg: $ae_api,
                    event_device_id: event_device_id,
                    data_kv: event_device_data,
                    log_lvl: 0
                })
                .then(function (up_event_device_result) {
                    // console.log('UPDATED DEVICE INFO!!! UPDATED DEVICE INFO!!! UPDATED DEVICE INFO!!!');
                    if (up_event_device_result) {
                        console.log(up_event_device_result);
                    }
                })
                .catch(function (error: any) {
                    console.log('No results returned or failed.');
                    console.log(error);
                });
        });

        return get_device_info_promise;
    }
</script>

<div class="hidden">This is for forcing data loading.</div>
