import { localStorageStore } from '@skeletonlabs/skeleton';
import { writable } from 'svelte/store';
import type { Writable } from 'svelte/store';

import type { key_val } from '$lib/ae_stores';

/* *** BEGIN *** Initialize events_local_data_struct */
// Longer-term app data. This should be stored to *local* storage.
// Updated 2024-03-06
let events_local_data_struct: key_val = {
    'ver': '2024-03-12_19',
    // Shared
    'name': 'Aether - Events (SvelteKit 2.x Svelte 4.x)',
    'title': `OSIT's Æ Events`, // - Dev SvelteKit`, // &AElig;

    'ds': {},

    'events_cfg_json': {},

    'default__event_id': null, // OSIT Demo pjrcghqwert
    'default__location_id': null,
    'default__session_id': null,

    // all, disabled, enabled
    'qry__enabled': 'enabled',
    // all, hidden, not_hidden
    'qry__hidden': 'not_hidden',
    'qry__limit': 20,
    'qry__offset': 0,

    // The show details is intended for things like meta data and additional details that are not always needed.
    show_details: false,

    // Badge Printing
    'badges': {
        'show_element__cfg': true,
        'show_element__cfg_detail': false,
        'show_element__access_type': true,
        'theme_mode': 'dark',
        'theme_name': 'wintry',

        'classes__form': 'border border-surface-200 p-4 space-y-4 rounded-container-token',
    },

    // Lead Retrievals (Exhibit)
    'leads': {
        show_content__scan_requirements: true,

        auto_view: true,

        // The entered_passcode is the exhibit booths shared passcode for staff. This is used to initially access the lead retrieval service.
        entered_passcode: null,

        // The auth_exhibit_kv (key value pairs) is used to store the exhibit IDs that the browser client can access. This is a key value list of exhibit ID and created datetime stamp. These should not be more than X days old. The entered_passcode for events_sess.leads is what they are entering to log in.
        auth_exhibit_kv: {
            // {'LNDF-67-89-92': {key: 'example@oneskyit.com', updated_on: '2024-03-13T08:05:29Z}}
        },

        // The auth_exhibit_license_li is used to store the exhibit license(s) being used on the browser client. There can be multiple exhibit IDs, but only one license per exhibit ID for the browser client. This is used to determine who can actually access and use the lead retrieval service. This is a key value list of key (email address) and created datetime stamp. These should not be more than X days old.
        // auth_exhibit_license_li: {
        //     // 'LNDF-67-89-92': { 'key': 'example@oneskyit.com', 'updated_on': '2024-03-13T08:05:29Z'}
        // },
        edit_license_li: false,

        // The "tab" is a key value list of exhibit ID and tab name. This is intentionally using local storage to store the current tab for each exhibit.
        // example: {'LNDF-67-89-92': 'start', 'OFLN-32-38-14': 'add_scan'}
        tab: {},
    },

    // Presentation Management (Distributing)

    // Speakers Management (Collection)
    'speakers': {
        'default__session_id': null, // Assign to new presenters by default
        status__submit: null, // 'saving', 'created', 'updated'
    },

    // other
}
console.log(`AE Stores - App Events Local Storage Data:`, events_local_data_struct);

// This works, but does not uses local storage:
// export let ae_loc = writable(events_local_data_struct);

// This works and uses *local* storage:
export let events_loc: Writable<key_val> = localStorageStore('ae_ events_loc', events_local_data_struct);
// console.log(`AE Stores - App Local Storage Data:`, get(ae_loc));


/* *** BEGIN *** Initialize events_session_data_struct */
// Temporary app data. This should be stored to session storage.
// Updated 2024-03-06
let events_session_data_struct: key_val = {
    'ver': '2024-03-06_17',
    // Shared
    'ds': {
        'submit_status': null,
    },
    'ds_loaded': {
    },

    // Badge Printing
    'badges': {
        'fulltext_search_qry_str': null,
        'status_qry__search': null,
    },

    // Lead Retrievals (Exhibit)
    'leads': {
        example: true,

        show_form__license: false,

        submit_status__license: null, // 'saving', 'created', 'updated'
        // create_status__license: null, // 'creating', 'created', 'updated'
        // update_status__license: null, // 'updating', 'created', 'updated'

        // The entered_passcode is the exhibit booths shared passcode for staff. This is used to initially access the lead retrieval service.
        entered_passcode: null,

        tmp_license: {
            // 'agree' : false, // The user must agree to the license agreement.
            'email': '',
            'full_name': '',
            'passcode': '',
            'session_count': 0,
            'updated_on': new Date().toISOString()
        },

        qr_scan_result: null,
    },

    // Presentation Management (Distributing)

    // Speakers Management (Collection)

    // other
};
console.log(`AE Stores - App Events Session Storage Data:`, events_session_data_struct);
export let events_sess = writable(events_session_data_struct);


/* *** BEGIN *** Initialize events_slct and events_trigger */
/* The slct and slct_trigger variable should not be stored in local storage. Only use session storage because browser tabs can be open to different events, badges, exhibits, etc. */

// Intended for temporary session storage.
// Updated 2024-03-06
let events_slct_obj_template: key_val = {
    // Top level
    'event_id': null,
    'event_obj': {},
    'event_obj_li': [],

    // Sub-level event_
    'abstract_id': null,
    'abstract_obj': {},
    'abstract_obj_li': [],

    'badge_id': null,
    'badge_obj': {},
    'badge_obj_li': [],

    'badge_template_id': null,
    'badge_template_obj': {},
    'badge_template_obj_li': [],

    'device_id': null,
    'device_obj': {},
    'device_obj_li': [],

    'exhibit_id': null,
    'exhibit_obj': {},
    'exhibit_obj_li': [],

    // Rename these to badge_tracking_*?
    'exhibit_tracking_id': null,
    'exhibit_tracking_obj': {},
    'exhibit_tracking_obj_li': [],

    'file_id': null,
    'file_obj': {},
    'file_obj_li': [],

    'location_id': null,
    'location_obj': {},
    'location_obj_li': [],

    'person_id': null,
    'person_obj': {},
    'person_obj_li': [],

    'presentation_id': null,
    'presentation_obj': {},
    'presentation_obj_li': [],

    'presenter_id': null,
    'presenter_obj': {},
    'presenter_obj_li': [],

    'session_id': null,
    'session_obj': {},
    'session_obj_li': [],
};
// console.log(`AE Stores - Selected Events Objects:`, events_slct_obj_template);

// This works, and uses *session* (not local) storage:
export let events_slct = writable(events_slct_obj_template);

// This works and uses *local* storage:
// export let events_slct: Writable<key_val> = localStorageStore('ae_events_slct', events_slct_obj_template);


/* *** BEGIN *** Initialize events_trigger */
// Intended for temporary session storage.
// Updated 2024-03-06
export let events_trigger: any = writable(null);
// console.log(`AE Events Stores - Events Trigger:`, events_trigger);
