window.addEventListener("load", function () {

    // Remove Loader
    let load_screen = document.getElementById("load_screen");
    if (load_screen) {
        document.body.removeChild(load_screen);
    }

    let layoutName = 'JavaDle Dashboard';

    let settingsObject = {
        admin: 'Cork Admin Template',
        settings: {
            layout: {
                name: layoutName,
                toggle: true,
                darkMode: false,
                boxed: true,
                logo: {
                    darkLogo: '/../assets/admin/assets/img/logo.svg',
                    lightLogo: '/../assets/admin/assets/img/logo2.svg'
                }
            }
        },
        reset: false
    }

    if (settingsObject.reset) {
        localStorage.clear();
    }

    var corkThemeObject;
    var getcorkThemeObject = localStorage.getItem("theme");
    var getParseObject = JSON.parse(getcorkThemeObject);

    if (getcorkThemeObject !== null) {
        if (getParseObject.admin === 'Cork Admin Template') {
            if (getParseObject.settings.layout.name === layoutName) {
                corkThemeObject = getParseObject;
            } else {
                corkThemeObject = settingsObject;
            }
        } else {
            if (getParseObject.admin === undefined) {
                corkThemeObject = settingsObject;
            }
        }
    } else {
        corkThemeObject = settingsObject;
    }

    // Get Dark Mode Information i.e darkMode: true or false
    if (corkThemeObject.settings.layout.darkMode) {
        localStorage.setItem("theme", JSON.stringify(corkThemeObject));
        getParseObject = JSON.parse(localStorage.getItem("theme"));

        if (getParseObject.settings.layout.darkMode) {
            let ifStarterKit = document.body.getAttribute('page') === 'starter-pack';
            document.body.classList.add('dark');
            if (ifStarterKit && document.querySelector('.navbar-logo')) {
                document.querySelector('.navbar-logo').setAttribute('src', '../../src/assets/img/logo.svg');
            } else if (document.querySelector('.navbar-logo')) {
                document.querySelector('.navbar-logo').setAttribute('src', getParseObject.settings.layout.logo.darkLogo);
            }
        }
    } else {
        localStorage.setItem("theme", JSON.stringify(corkThemeObject));
        getParseObject = JSON.parse(localStorage.getItem("theme"));

        if (!getParseObject.settings.layout.darkMode) {
            let ifStarterKit = document.body.getAttribute('page') === 'starter-pack';
            document.body.classList.remove('dark');
            if (ifStarterKit && document.querySelector('.navbar-logo')) {
                document.querySelector('.navbar-logo').setAttribute('src', '../../src/assets/img/logo2.svg');
            } else if (document.querySelector('.navbar-logo')) {
                document.querySelector('.navbar-logo').setAttribute('src', getParseObject.settings.layout.logo.lightLogo);
            }
        }
    }

    // Get Layout Information i.e boxed: true or false
    if (corkThemeObject.settings.layout.boxed) {
        localStorage.setItem("theme", JSON.stringify(corkThemeObject));
        getParseObject = JSON.parse(localStorage.getItem("theme"));

        if (getParseObject.settings.layout.boxed) {
            if (document.body.getAttribute('layout') !== 'full-width') {
                document.body.classList.add('layout-boxed');
                if (document.querySelector('.header-container')) {
                    document.querySelector('.header-container').classList.add('container-xxl');
                }
                if (document.querySelector('.middle-content')) {
                    document.querySelector('.middle-content').classList.add('container-xxl');
                }
            } else {
                document.body.classList.remove('layout-boxed');
                if (document.querySelector('.header-container')) {
                    document.querySelector('.header-container').classList.remove('container-xxl');
                }
                if (document.querySelector('.middle-content')) {
                    document.querySelector('.middle-content').classList.remove('container-xxl');
                }
            }
        }
    } else {
        localStorage.setItem("theme", JSON.stringify(corkThemeObject));
        getParseObject = JSON.parse(localStorage.getItem("theme"));

        if (!getParseObject.settings.layout.boxed) {
            if (document.body.getAttribute('layout') !== 'boxed') {
                document.body.classList.remove('layout-boxed');
                if (document.querySelector('.header-container')) {
                    document.querySelector('.header-container').classList.remove('container-xxl');
                }
                if (document.querySelector('.middle-content')) {
                    document.querySelector('.middle-content').classList.remove('container-xxl');
                }
            } else {
                document.body.classList.add('layout-boxed');
                if (document.querySelector('.header-container')) {
                    document.querySelector('.header-container').classList.add('container-xxl');
                }
                if (document.querySelector('.middle-content')) {
                    document.querySelector('.middle-content').classList.add('container-xxl');
                }
            }
        }
    }
});
