<script lang="ts">
import { createEventDispatcher, onMount, tick } from 'svelte';
import { RadioGroup, RadioItem } from '@skeletonlabs/skeleton';

import { ae_util } from '$lib/ae_utils';
import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores';

let notes: null|string = null;
let all: boolean = false;

export let theme_mode: null|string = null;
export let set_theme_mode: null|boolean = null;
export let theme_name: null|string = null;
export let set_theme_name: null|boolean = null;

const dispatch = createEventDispatcher();


onMount(() => {
    console.log('** Element Mounted: ** Element App Config');
    if (set_theme_mode) {
        $slct_trigger = 'set_theme_mode';
    }
    if (set_theme_name) {
        $slct_trigger = 'set_theme_name';
    }
});

$: if ($slct_trigger == 'set_theme_mode' && $ae_loc.hub.theme_mode) {
    console.log(`$ae_loc.hub.theme_mode=${$ae_loc.hub.theme_mode}`);
    $slct_trigger = null;
    if ($ae_loc.hub.theme_mode == 'light') {
        document.documentElement.classList.remove('dark');
        document.documentElement.classList.add('light');
    } else if ($ae_loc.hub.theme_mode == 'dark') {
        document.documentElement.classList.remove('light');
        document.documentElement.classList.add('dark');
    }
}

$: if ($slct_trigger == 'set_theme_name' && $ae_loc.hub.theme_name) {
    console.log(`$ae_loc.hub.theme_name=${$ae_loc.hub.theme_name}`);
    $slct_trigger = null;
    // Update the body attribute named "data-theme" to the current theme name.
    document.body.setAttribute('data-theme', $ae_loc.hub.theme_name);
}

// $: if (entered_passcode && entered_passcode.length >= 5) {
//     console.log(`entered_passcode=${entered_passcode}`);
//     handle_check_access_type_passcode();
// }

// $: if (trigger && $ae_loc.access_type) {
//     console.log(`$ae_loc.access_type=${$ae_loc.access_type}`);

//     let access_checks_results =  ae_util.process_permission_checks($ae_loc.access_type);

//     $ae_loc = {...$ae_loc, ...access_checks_results};
// } else if (trigger) {
//     console.log(`$ae_loc.access_type=not set`);

//     // Send an empty string to reset the permissions. This is the same as sending 'anonymous'.
//     let access_checks_results =  ae_util.process_permission_checks('');

//     $ae_loc = {...$ae_loc, ...access_checks_results};
// }


function handle_something() {
    // console.log('*** handle_something() ***');

}

function handle_clear_storage(item: null|string) {
    // console.log('*** handle_clear_storage() ***');
    // window.localStorage.setItem('access_type', 'anonymous');
    // return true;
}

function dispatch_something_changed() {
    console.log('*** dispatch_something_changed() ***');

    console.log(ae_util);
    console.log($ae_loc);

    dispatch('access_type_changed', {
        access_type: $ae_loc.access_type
    });
}
</script>


<section id="AE-App-Cfg" class="ae_app_cfg transition duration-300 delay-150 hover:delay-300 hover:transition-all hidden-print">

<div
    class="ae_cfg_content"
    class:hidden={!$ae_loc.hub.show_element__cfg_detail}
    >

    <div>
    {#if $ae_loc.access_type && $ae_loc.access_type != 'anonymous'}
        {#if $ae_loc.access_type == 'super'}
            <span class="fas fa-secret"></span> Super Access
        {:else if $ae_loc.access_type == 'manager'}
            <span class="fas fa-user-shield"></span> Manager Access
        {:else if $ae_loc.access_type == 'administrator'}
            <span class="fas fa-user-ninja"></span> Administrator Access
        {:else if $ae_loc.access_type == 'trusted'}
            <span class="fas fa-user-nurse"></span> Trusted Access
        {:else if $ae_loc.access_type == 'authenticated'}
            <span class="fas fa-user-friends"></span> Authenticated Access
        {:else if $ae_loc.access_type == 'anonymous'}
            <span class="fas fa-users"></span> Anonymous Access
        {:else}
            <span class="fas fa-unlock mx-1"></span> Unknown Access
        {/if}

        <button
            class="btn btn-sm variant-soft-secondary access_type_lock_btn hover:transition-all"
            on:click={() => {
                handle_clear_access();
            }}
            title="Access mode is currently enabled/unlocked. Click to exit and lock."
            >
            <span class="fas fa-lock mx-1"></span> Lock
        </button>
    {:else}
        Not logged in
    {/if}
    </div>

    <div>
        Theme:
    <div>
    <!-- Light/Dark Theme: -->
    <RadioGroup
        active="variant-soft"
        hover="hover:variant-ringed-primary"
        >
        <RadioItem
            on:change={() => {
                $slct_trigger = 'set_theme_mode';
            }}
            bind:group={$ae_loc.hub.theme_mode}
            name="theme_light"
            value={'light'}
            >
            Light
        </RadioItem>
        <RadioItem
            on:change={() => {
                $slct_trigger = 'set_theme_mode';
            }}
            bind:group={$ae_loc.hub.theme_mode}
            name="theme_dark"
            value={'dark'}
            >
            Dark
        </RadioItem>
    </RadioGroup>
    </div>

    <div>
    <!-- Theme Name: -->
    <select
        on:change={() => {
            $slct_trigger = 'set_theme_name';
        }}
        bind:value={$ae_loc.hub.theme_name}
        class="select"
        title="Theme name"
        >
        <option value="">-- None --</option>
        <option value="gold-nouveau">Gold Nouveau</option>
        <option value="hamlindigo">Hamlindigo</option>
        <option value="modern">Modern</option>
        <option value="rocket">Rocket</option>
        <option value="wintry">Wintry</option>
    </select>
    </div>
    </div>

</div>

<button
    class="btn btn-sm ae_cfg_btn hover:transition-all"
    on:click={() => {
        $ae_loc.hub.show_element__cfg_detail = !$ae_loc.hub.show_element__cfg_detail;
    }}
    >
    <span class="fas fa-cog mx-1"></span>
    <span class="cfg_text">Config</span>
</button>

</section>


<style lang="postcss">
/* BEGIN: AE's Svelte App Config component  */
#AE-App-Cfg {
    /* position: absolute; */
    position: fixed;
    /* position: relative; */
    /* position: static; */
    /* position: sticky; */
    /* top: 1em; */
    bottom: 1.5rem;
    left: 0rem;

    padding: .5rem;

    /* lightyellow */
    /* background-color: hsla(60,100%,90%,.30);  */
    background-color: rgba(var(--color-surface-500) / .5);

    border-top: solid thin black;
    border-right: solid thin black;
    border-bottom: solid thin black;
    border-top-right-radius: .5em;
    border-bottom-right-radius: .5em;

    opacity: .50;

    font-size: .75rem;

    z-index: 5;

    /* NOTE: transition when no longer hovering */
    transition-property: opacity, background-color;
    transition-delay: .1s;
    transition-duration: .4s;
    transition-timing-function: linear;
}

#AE-App-Cfg:hover {
    /* lightyellow */
    /* background-color: hsla(60,100%,90%,.95); */
    background-color: rgba(var(--color-surface-500) / 1);
    opacity: 1;

    /* NOTE: transition when hover starts */
    transition-property: opacity, background-color;
    /* transition-delay: .5s; */
    transition-duration: .10s;
    transition-timing-function: linear;
}

.ae_cfg_btn .cfg_text {
    display: none;
}

.ae_cfg_btn:hover .cfg_text {
    display: initial;
    /* outline: solid thin red; */
}

.access_type .current_text {
    display: none;
}

.access_type:hover .current_text {
    display: initial;
    /* outline: solid thin red; */
}
/* END: AE's Svelte App Config component */
</style>
