<template>
    <section id="content">
        <div id="content-header">
            <pre :class="[loggedInUserDarkModeBoolean ? 'pre-darkmode' : 'pre-lightmode']" id="page-name">Settings</pre>
        </div>
        <div id="content-body">
            <div class="buttonOptions">
                <input :class="[(loggedInUserDarkModeBoolean && accountSelected) ? 'selected-input-darkmode' : '',
            (loggedInUserDarkModeBoolean && !accountSelected) ? 'input-darkmode' : '',
            (!loggedInUserDarkModeBoolean && accountSelected) ? 'selected-input-lightmode' : '',
            (!loggedInUserDarkModeBoolean && !accountSelected) ? 'input-lightmode' : '',]" type="button" id="account"
                    value="Account" @click="ActivateAccount">
                <input :class="[(loggedInUserDarkModeBoolean && appearanceSelected) ? 'selected-input-darkmode' : '',
            (loggedInUserDarkModeBoolean && !appearanceSelected) ? 'input-darkmode' : '',
            (!loggedInUserDarkModeBoolean && appearanceSelected) ? 'selected-input-lightmode' : '',
            (!loggedInUserDarkModeBoolean && !appearanceSelected) ? 'input-lightmode' : '',]" type="button"
                    id="appearance" value="Appearance" @click="ActivateAppearance">
                <input :class="[(loggedInUserDarkModeBoolean) ? 'input-darkmode' : '',
            (!loggedInUserDarkModeBoolean) ? 'input-lightmode' : '',]" type="button" id="logout" value="Logout"
                    @click="handleLogout">
            </div>
            <UserProfile v-if="accountSelected" />
            <UserAccount v-if="accountSelected" />
            <UserAppearance v-if="appearanceSelected" />
            <!-- <UserRightsList v-if="usersSelected"/> -->
        </div>
    </section>
</template>


<script setup>
import { ref, onMounted, watch } from 'vue';

import UserProfile from "../components/server/UserProfile.vue";
import UserAccount from "../components/server/UserAccount.vue";
import UserAppearance from "../components/server/UserAppearance.vue";
import { useStore } from "vuex";
import { computed } from 'vue';

const store = useStore();
const modeChanged = computed(() => store.state.updateDarkMode);

const { signOut } = useAuth()

definePageMeta({
    layout: 'default',
    title: 'Settings'
})

const loggedInUserDarkModeBoolean = ref('');
const accountSelected = ref(true)
const appearanceSelected = ref(false)

const handleLogout = async () => {
    localStorage.setItem('logged-in-bool', false);
    localStorage.removeItem('logged-in-user-id');
    localStorage.removeItem('logged-in-user-username');
    localStorage.removeItem('logged-in-user-registered');
    localStorage.removeItem('logged-in-user-lastLogin');
    localStorage.removeItem('logged-in-user-fullName');
    localStorage.removeItem('logged-in-user-email');
    localStorage.removeItem('logged-in-user-phonenumber');
    localStorage.removeItem('logged-in-user-address');
    localStorage.removeItem('logged-in-user-city');
    localStorage.removeItem('logged-in-user-postcode');
    localStorage.removeItem('logged-in-user-adminBool');
    localStorage.removeItem('logged-in-user-technicianBool');
    localStorage.removeItem('logged-in-user-readerBool');
    localStorage.removeItem('logged-in-user-darkMode');
    store.commit('resetChangeToSet');
    store.commit('setLocalStorageChanged');
    let res = await signOut({ callbackUrl: '/login' })
    store.commit('resetStore');
    store.commit('logout');
}

// fuctions to change the option button
const ActivateAccount = () => {
    accountSelected.value = true;
    appearanceSelected.value = false;
    // usersSelected.value = false;
};
const ActivateAppearance = () => {
    accountSelected.value = false;
    appearanceSelected.value = true;
};

const getSession = async () => {
    const loggedInUserDarkModeBool = getItem('logged-in-user-darkMode');
    if (loggedInUserDarkModeBool == 1) {
        loggedInUserDarkModeBoolean.value = true;
    } else {
        loggedInUserDarkModeBoolean.value = false;
    }
}

function getItem(item) {
    if (process.client) {
        return localStorage.getItem(item)
    } else {
        return undefined
    }
}

watch(modeChanged, getSession)

onMounted(async () => {
    await getSession();
});
</script>

<script>
export default {
    name: "SettingsPage",
}
</script>


<style>
* {
    box-sizing: border-box;
}

#content {
    display: flex;
    flex-direction: column;
    float: left;
    justify-content: stretch;
    align-items: stretch;
    width: 100%;
    flex-grow: 1;
    gap: 0.625rem;
    padding: 0 1.25rem;
}

#content-header {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 3.125rem;
}

#page-name {
    letter-spacing: 5%;
    font: 400 1.5rem/125% Overpass, -apple-system, Roboto, Helvetica, sans-serif;
    text-decoration: underline;
}

.pre-darkmode {
    color: #fff;
}

.pre-lightmode {
    color: #000;
}

#content-body {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 1.25rem;
    margin-bottom: 0.625rem;
    border-radius: 0.625rem;
}

.buttonOptions {
    display: flex;
    flex-direction: row;
    align-content: center;
}

input#account,
input#appearance,
input#logout {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 12.5rem;
    height: 2.5rem;
    border-radius: 0.625rem;
    border: none;
    text-align: center;
    letter-spacing: 5%;
    font: 400 0.875rem/2rem Overpass, sans-serif;
    margin-right: 1.25rem;
    box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

.input-darkmode {
    color: #fff;
    background-color: #2c2c2c;
}

.selected-input-darkmode {
    color: #fff;
    background-color: #454545;
}

input#logout:hover {
    background-color: #454545;
}

.input-lightmode {
    color: #000;
    background-color: #E4E4E4;
}

.selected-input-lightmode {
    color: #000;
    background-color: #D9D9D9;
}

.input-darkmode:hover,
.input-lightmode:hover,
.selected-input-darkmode:hover,
.selected-input-lightmode:hover {
    cursor: pointer;
}
</style>