$(document).ready(function() {
    
    $('#mmlMainContent').load('../components/homepage.html');
    $('#top_nav_bar').load('../components/top_nav.html');
    $('#main_nav_bar').load('../components/main_nav.html', function() {
        updateCartCount();
        if (document.cookie.indexOf('loggedIn=true') != -1) {
            loggedInState();
        } else {
            loggedOutState();
        }
        document.getElementById('homeLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/homepage.html');
        },);
        document.getElementById('aboutLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/about.html');
        },);
        document.getElementById('shopLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/shop.html');
        },);
        document.getElementById('contactLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/contact.html');
        },);
        document.getElementById('loginLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/login.html');
        },);
        document.getElementById('registerLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/register.html');
        },);
        document.getElementById('logoutLink')?.addEventListener('click', function() {
            logout();
        });
        document.getElementById('profileLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/profile.html');
        });
        document.getElementById('cartLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/cart.html');
        });
        document.getElementById('imprintLink')?.addEventListener('click', function() {
            $('#mmlMainContent').load('../components/imprint.html');
        });
    });
    $('#marmeladenladen_footer').load('../components/footer.html');
    $('#marmeladenladen_search').load('../components/modal.html');
});

function loggedInState() {
    $('#main_nav_bar').find('#loginLink').hide();
    $('#main_nav_bar').find('#registerLink').hide();
    $('#main_nav_bar').find('#logoutLink').show();
    $('#main_nav_bar').find('#profileLink').show();
}

function loggedOutState() {
    $('#main_nav_bar').find('#loginLink').show();
    $('#main_nav_bar').find('#registerLink').show();
    $('#main_nav_bar').find('#logoutLink').hide();
    $('#main_nav_bar').find('#profileLink').hide();
}

function logout() {
    document.cookie = 'loggedIn=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
    document.cookie = 'email=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
    document.cookie = 'rememberme=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
    $('#mmlMainContent').load('../components/homepage.html');
    $('#main_nav_bar').find('#loginLink').show();
    $('#main_nav_bar').find('#registerLink').show();
    $('#main_nav_bar').find('#logoutLink').hide();
    $('#main_nav_bar').find('#profileLink').hide();
}

function updateCartCount() {
    var items = getCartCount();
    $('#cartCount').text(items);
}

function getCartCount() {
    var items;
    if (sessionStorage.getItem('cart')) {
        var cart = JSON.parse(sessionStorage.getItem('cart')!) ?? [];
        items = 0;
        for (let i = 0; i < cart.length; i++) {
            var object = JSON.parse(cart[i]);
            items += object.count;
        }
    } else {
        items = 0;
    }
    return items;
}