<template>
    <section :class="['userAppearance', loggedInUserDarkModeBoolean ? 'section-darkmode' : 'section-lightmode']">
        <div class="userData">
            <div class="data-field" id="mode">
                <pre :class="['label', loggedInUserDarkModeBoolean ? 'pre-darkmode' : 'pre-lightmode']">Mode:</pre>
                <pre
                    :class="['smallLabel', loggedInUserDarkModeBoolean ? 'pre-darkmode' : 'pre-lightmode']">Lightmode</pre>
                <Toggle v-model="loggedInUserDarkModeBoolean" id="toggleAppearance" />
                <pre
                    :class="['smallLabel', loggedInUserDarkModeBoolean ? 'pre-darkmode' : 'pre-lightmode']">Darkmode</pre>
            </div>
            <div class="data-field" id="language">
                <pre :class="['label', loggedInUserDarkModeBoolean ? 'pre-darkmode' : 'pre-lightmode']">Language:</pre>
                <pre :class="['data', loggedInUserDarkModeBoolean ? 'data-darkmode' : 'data-lightmode']">...</pre>
            </div>
        </div>
    </section>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import Toggle from '@vueform/toggle';
import { useStore } from 'vuex';
import Axios from '../axios.config.js';
import clientsideConfig from '../../clientsideConfig.js'

const store = useStore();

const loggedInUserId = ref('');
const loggedInUserUsername = ref('');
const loggedInUserRegistered = ref('');
const loggedInUserLastLogin = ref('');
const loggedInUserFullName = ref('');
const loggedInUserEmail = ref('');
const loggedInUserPhonenumber = ref('');
const loggedInUserAddress = ref('');
const loggedInUserCity = ref('');
const loggedInUserPostcode = ref('');
const loggedInUserAdminBool = ref('');
const loggedInUserTechnicianBool = ref('');
const loggedInUserReaderBool = ref('');
const loggedInUserDarkModeBool = ref('');
const loggedInUserDarkModeBoolean = ref(true);

function setItem(item, value) {
    if (process.client) {
        localStorage.setItem(item, value)
        return true
    } else {
        return false
    }
}

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

//update data
const updateUser = async () => {
    try {
        await Axios.put(
            `https://${clientsideConfig.url}:${clientsideConfig.port}/api/updateEmployee`,
            {
                id: loggedInUserId.value,
                username: loggedInUserUsername.value,
                fullName: loggedInUserFullName.value,
                email: loggedInUserEmail.value,
                phonenumber: loggedInUserPhonenumber.value,
                address: loggedInUserAddress.value,
                city: loggedInUserCity.value,
                postcode: loggedInUserPostcode.value,
                adminBool: loggedInUserAdminBool.value,
                technicianBool: loggedInUserTechnicianBool.value,
                readerBool: loggedInUserReaderBool.value,
                darkModeBool: loggedInUserDarkModeBoolean.value,
            }
        )
        setItem('logged-in-user-username', loggedInUserUsername.value)
        setItem('logged-in-user-fullName', loggedInUserFullName.value)
        setItem('logged-in-user-email', loggedInUserEmail.value)
        setItem('logged-in-user-phonenumber', loggedInUserPhonenumber.value)
        setItem('logged-in-user-address', loggedInUserAddress.value)
        setItem('logged-in-user-city', loggedInUserCity.value)
        setItem('logged-in-user-postcode', loggedInUserPostcode.value)
        if (loggedInUserDarkModeBoolean.value) {
            loggedInUserDarkModeBool.value = 1;
        } else {
            loggedInUserDarkModeBool.value = 0;
        }
        setItem('logged-in-user-darkMode', loggedInUserDarkModeBool.value)
        getSession();
        store.commit('setLocalStorageChanged');
        store.commit('toggleUpdateDarkMode');
    } catch (err) {
        console.log(err.response.statusText);
    }
}

const getSession = async () => {
    loggedInUserId.value = getItem('logged-in-user-id');
    loggedInUserUsername.value = getItem('logged-in-user-username');
    loggedInUserRegistered.value = getItem('logged-in-user-registered');
    loggedInUserLastLogin.value = getItem('logged-in-user-lastLogin');
    loggedInUserFullName.value = getItem('logged-in-user-fullName');
    loggedInUserEmail.value = getItem('logged-in-user-email');
    loggedInUserPhonenumber.value = getItem('logged-in-user-phonenumber');
    loggedInUserAddress.value = getItem('logged-in-user-address');
    loggedInUserCity.value = getItem('logged-in-user-city');
    loggedInUserPostcode.value = getItem('logged-in-user-postcode');
    loggedInUserAdminBool.value = getItem('logged-in-user-adminBool');
    loggedInUserTechnicianBool.value = getItem('logged-in-user-technicianBool');
    loggedInUserReaderBool.value = getItem('logged-in-user-readerBool');
    loggedInUserDarkModeBool.value = getItem('logged-in-user-darkMode');
    if (loggedInUserDarkModeBool.value == 1) {
        loggedInUserDarkModeBoolean.value = true;
    } else {
        loggedInUserDarkModeBoolean.value = false;
    }
}

watch(loggedInUserDarkModeBoolean, updateUser)

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

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

<style src="@vueform/toggle/themes/default.css"></style>

<style scoped>
.userAppearance {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    align-self: stretch;
    width: 100%;
    padding-left: 1.875rem;
    padding-right: 1.875rem;
    padding-top: 1.875rem;
    padding-bottom: 1.875rem;
    gap: 0.625rem;
    border-radius: 0.625rem;
    box-shadow: 0.25rem 0.25rem 0.25rem 0rem rgba(0, 0, 0, 0.25);
}

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

.section-lightmode {
    background-color: #fff;
}


.userData {
    display: flex;
    flex-direction: column;
    align-content: center;
    gap: 0.625rem
}

.data-field {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    padding: 0.8rem 1.875rem;
    border-radius: 0.625rem;
    gap: 1.25rem;
}

.pre-darkmode {
    color: #fff;
}

.pre-lightmode {
    color: #000;
}

.data-darkmode {
    background-color: #212121;
    color: #fff;
}

.data-lightmode {
    background-color: #EBEBEB;
    color: #000;
}

.data {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    padding: 0 0.625rem;
    border-radius: 0.3125rem;
    box-shadow: 0.0625rem 0.0625rem 0.25rem 0rem rgba(0, 0, 0, 0.25) inset;
    letter-spacing: 5%;
    font: 400 0.75rem/250% Overpass, sans-serif;
    width: 15.625em;
}

.label {
    width: 6.25rem;
}

.smallLabel {
    font-size: 0.875em;
}
</style>