<template>
    <header :class="[darkMode ? 'header-darkmode' : 'header-lightmode']">
        <img id="header-logo" loading="lazy" src="/tüit-logo.svg.png" />
        <router-link v-if="visibilityBool" to="/settings" class="button"
            :class="[darkMode ? 'button-darkmode' : 'button-lightmode']" @click="goToSettings">
            <div v-if="visibilityBool" class="profile">
                <pre :class="['username', darkMode ? 'username-darkmode' : 'username-lightmode']"
                    id='username'>{{ loggedInUserUsername }}</pre>
                <div v-if="visibilityBool"
                    :class="['picture', visibilityBool && !darkMode ? 'picture-loggedin-lightmode' : '', !visibilityBool && !darkMode ? 'picture-not-loggedin-lightmode' : '', visibilityBool && darkMode ? 'picture-loggedin-darkmode' : '', !visibilityBool && darkMode ? 'picture-not-loggedin-darkmode' : '']">
                    <img id="picture" loading="lazy" src="" />
                </div>
            </div>
        </router-link>
    </header>
</template>


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

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

// watch(modeChanged, getSession)

const loggedInUserUsername = ref('');
const loggedInBool = ref(false);
const visibilityBool = computed(() => loggedInBool.value);
const storeChanged = computed(() => store.state.localStorageChanged);

const route = useRoute()
const id = computed(() => route)

const darkMode = ref('')

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

const goToSettings = () => {
    store.commit('resetStore');
    store.commit('toggleChangeToSet');
    store.commit('changeToSettings');
};

const getSession = async () => {
    const loggedInUserDarkModeBool = getItem('logged-in-user-darkMode');
    if (loggedInUserDarkModeBool == 1) {
        darkMode.value = true;
    } else {
        darkMode.value = false;
    }
    if ((!(id.value.fullPath == '/login')) && (!(id.value.fullPath == '/settings'))) {
        loggedInBool.value = false;
        loggedInBool.value = getItem('logged-in-bool');
        loggedInUserUsername.value = getItem('logged-in-user-username');
    }
    if (storeChanged.value) {
        loggedInBool.value = getItem('logged-in-bool');
        loggedInUserUsername.value = getItem('logged-in-user-username');
        store.commit('resetLocalStorageChanged');
    }
}

const checkPath = async () => {
    if (id.value.fullPath == '/login') {
        loggedInBool.value = false;
        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('logout');
    }
}

watch(storeChanged, getSession);
watch(visibilityBool, getSession);

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


<script>

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



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

header {
    display: flex;
    flex-direction: row;
    position: sticky;
    top: 0;
    width: 100%;
    height: 3.125rem;
    align-self: stretch;
    justify-content: space-between;
    align-items: center;
    padding: 0.375rem 1.875rem;
}

.header-darkmode {
    background-color: #212121;
    border-bottom: 0.125em solid #000000;
}

.header-lightmode {
    background-color: #ebebeb;
    border-bottom: 0.125em solid #8e8e8e;
}

#header-logo {
    object-fit: contain;
    object-position: center;
    width: 4rem;
    height: 2rem;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.profile {
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    gap: 0.625rem;
    padding: 0 0.375rem;
}

.username {
    align-self: center;
    text-align: right;
    font: 200 0.75rem/1.25rem Overpass, sans-serif;
    letter-spacing: 0.01rem;
}

.username-darkmode {
    color: #ffffff;
}

.username-lightmode {
    color: #000000;
}

.pre#username {
    text-decoration: none;
}

.picture {
    display: flex;
    width: 1.875rem;
    height: 1.875rem;
    border-radius: 50%;
}

.picture-loggedin-darkmode {
    background-color: #ffffff;
}

.picture-not-loggedin-darkmode {
    background-color: #212121;
}

.picture-loggedin-lightmode {
    background-color: #000000;
}

.picture-not-loggedin-lightmode {
    background-color: #ebebeb;
}
</style>