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

	let { data }: Props = $props();

	// Imports
	// import { browser } from '$app/environment';
	// import { onMount } from 'svelte';

	// import type { key_val } from '$lib/ae_stores';
	// import { ae_util } from '$lib/ae_utils/ae_utils';
	// import { api } from '$lib/api';
	// import Element_ae_crud from '$lib/element_ae_crud.svelte';
	// import Element_data_store from '$lib/element_data_store_v2.svelte';

	// import { liveQuery } from "dexie";
	// import { core_func } from '$lib/ae_core_functions';
	// import { db_core } from "$lib/db_core";
	// import { db_events } from "$lib/ae_events/db_events";
	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.js';

	// import Event_launcher_menu from '../../launcher_menu.svelte';
	// import Event_launcher_session_view from '../../launcher_session_view.svelte';

	// import native_app from '$lib/electron_native.js';

	// Exports

	// Variables
	// Quickly save the data passed from the parent(s) to the Svelte stores, localStorage, and other.
	// $slct.account_id = data.account_id;
	// console.log(`$slct.account_id = `, $slct.account_id);
	let ae_acct = data[$slct.account_id];
	// console.log(`ae_acct = `, ae_acct);

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

	console.log(`event_id: ${data.params.event_id}`);
	$events_slct.event_id = data.params.event_id;

	console.log(`event_location_id: ${data.params.event_location_id}`);
	$events_slct.event_location_id = data.params.event_location_id;

	// console.log(`ae_acct.slct.event_id:`, ae_acct.slct.event_id);
	// $events_slct.event_id = ae_acct.slct.event_id;
	// $events_slct.event_obj = ae_acct.slct.event_obj;
	$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 ?? [''];
	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 ?? [''];
	// $events_slct.id_li__event_session = ae_acct.slct.id_li__event_session ?? [''];

	// $events_trig = ae_acct.trig;
	// $events_trig.event_location_li = true;
	// $events_trig.event_session_li = true;

	// $events_trigger = 'event_location_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
		};
	}

	// if (browser) {
	//     console.log(`data.url.searchParams.get('location_id') = `, data.url.searchParams.get('location_id'));
	// }

	// if (ae_acct.slct.event_id) {
	//     $events_loc.launcher.slct.event_id = ae_acct.slct.event_id;
	// }
	// if (ae_acct.slct.event_location_id) {
	//     $events_loc.launcher.slct.event_location_id = ae_acct.slct.event_location_id;
	// }
	// if (ae_acct.slct.event_session_id) {
	//     $events_loc.launcher.slct.event_session_id = ae_acct.slct.event_session_id;
	// }
	// if (ae_acct.slct.event_presentation_id) {
	//     $events_loc.launcher.slct.event_presentation_id = ae_acct.slct.event_presentation_id;
	// }
	// if (ae_acct.slct.event_presenter_id) {
	//     $events_loc.launcher.slct.event_presenter_id = ae_acct.slct.event_presenter_id;
	// }

	// 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;

	// Process the location list to pull out just the IDs.
	// let tmp_li = []; // This is to prevent the array from constantly updating and triggering the liveQuery.
	// let bi_id_li__event_location: string[] = [];
	// for (let i = 0; i < $events_slct.event_location_obj_li.length; i++) {
	//     let event_location_obj = $events_slct.event_location_obj_li[i];
	//     let event_location_id_random = event_location_obj.event_location_id_random;
	//     tmp_li.push(event_location_id_random);
	// }
	// bi_id_li__event_location = tmp_li;
	// console.log(`bi_id_li__event_location:`, bi_id_li__event_location);

	// 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}`);
	//     bi_trig__event_location = 'event_location_li';
	//     bi_trig__event_session = 'event_session_li';

	//     // $events_slct.event_location_id = data.url.searchParams.get('location_id');
	//     // $events_slct.event_session_id = data.url.searchParams.get('session_id');
	// }

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

	//     // if ($ae_event_launcher.app_mode == 'native') {
	//         // handle_get_device_info($slct.event_device_id);
	//     // }

	//     // const native_app = require('$lib./script');

	//     // let native_app_config = native_app.load_init_config();
	//     // console.log('Native App Initial Config:', native_app_config);
	// });

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

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

		let get_device_info_promise = get_device_info({ event_device_id: event_device_id });
		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 = {};
			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 = {};

				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']);

			event_device_obj_up_promise = update_event_device_obj({
				event_device_id: event_device_id,
				data: 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>
