document.addEventListener('DOMContentLoaded', async function () {
    const backupButton = document.getElementById('backup');
    const manageDepartmentButton = document.getElementById('manage_department');
    const manageUsersButton = document.getElementById('manage_users');
    const changeInfoButton = document.getElementById('change_info');
    const shareFileButton = document.getElementById('share_file');
    const decryptButton = document.getElementById('decrypt');
    const logoutButton = document.getElementById('logout');

    await insertUsername();
    checkDirBackupFileExists()
        .then(() => console.log('verificare facuta'));


    manageUsersButton.addEventListener('click', async function(){
        window.electronAPI.changeContent('manage_users.html')
            .then(() => console.log('Navigated to dashboard'))
            .catch(error => console.error('Error navigating:', error));
    });

    manageDepartmentButton.addEventListener('click', async function(){
        window.electronAPI.changeContent('manage_departments.html')
            .then(() => console.log('Navigated to dashboard'))
            .catch(error => console.error('Error navigating:', error));
    });

    backupButton.addEventListener('click', function () {
        console.log('Set backup directory button clicked!');

        window.electronAPI.openBackupDirDialog()
            .then(() => console.log('Back-up directory set'))
            .catch(async error => await window.electronAPI.showAlert(error.message)
                .then(() => console.log('Alert window opened'))
                .catch(error => console.error('Error showing alert:', error)));
    });

    changeInfoButton.addEventListener('click', function () {
        console.log('Change your info button clicked!');

        window.electronAPI.changeContent('profile.html')
            .then(() => console.log('Navigated to dashboard'))
            .catch(error => console.error('Error navigating:', error));
    });

    shareFileButton.addEventListener('click', function () {
        console.log('Share a file button clicked!');

        window.electronAPI.changeContent('share_file.html')
            .then(() => console.log('Navigated to dashboard'))
            .catch(error => console.error('Error navigating:', error));
    });

    logoutButton.addEventListener('click', async function () {
        console.log('Logout button clicked!');

        await window.electronAPI.deleteFile('loginData.json');
        window.electronAPI.changeContent('login.html')
            .then(() => console.log('Navigated to dashboard'))
            .catch(error => console.error('Error navigating:', error));
    });

    decryptButton.addEventListener('click', async function (){

    });

    async function decryptFiles(){

    }

    async function checkDirBackupFileExists() {
        try {
            // Make an IPC call to check file existence
            const fileExists = await window.electronAPI.checkFileExists('dirBackup.json');

            if (!fileExists) {
                const notificationsDiv = document.getElementById('notifications');
                const button = document.createElement('button');
                button.id = 'backup_alert';
                button.name = 'alert';
                button.textContent = 'Set your backup directory!';
                button.addEventListener('click', handleButtonClick);
                notificationsDiv.appendChild(button);
            }
        } catch (error) {
            console.error('Error checking file existence:', error);
        }
    }

    async function handleButtonClick() {
        console.log('Button clicked!');

        await window.electronAPI.openBackupDirDialog()
            .then(() => console.log('Back-up directory set'))
            .catch(async error => await window.electronAPI.showAlert(error.message)
                .then(() => console.log('Alert window opened'))
                .catch(error => console.error('Error showing alert:', error)));

        const button = document.getElementById('backup_alert');
        button.remove();
    }

    async function insertUsername() {
        try {
            const userData = await window.electronAPI.readFile('loginData.json');
            const userJson = JSON.parse(userData.content);
            const username = userJson.name;

            const usernameField = document.getElementById('username_field');
            if (usernameField) {
                usernameField.textContent = username + '!';
            }
        } catch (error) {
            console.error('Error loading username:', error);
            const usernameField = document.getElementById('username_field');
            usernameField.textContent = 'User!';
        }
    }
});
