/**
 * @file notificationsButton.js
 * @description Hanterar notifikationsknappen för att toggla Windows-notifikationer
 * 
 * Author: Linus Olofsson - Iver 
 * Date: 2026-02-18
 * Last modified: Linus Olofsson (2026-02-18)
 * 
 * @purpose Toggle Windows-notifikationer på/av
 * @invoked_by popup.js
 */

const initNotificationsButton = () => {
    const notificationsBtn = document.getElementById('notificationsBtn');
    const notificationIcon = document.getElementById('notificationIcon');
    
    if (!notificationsBtn || !notificationIcon) {
        console.error('Notifications button or icon not found');
        return;
    }
    
    // Ladda nuvarande status
    chrome.storage.local.get(['notificationsEnabled'], (result) => {
        const enabled = result.notificationsEnabled !== false; // Default true
        updateButtonState(enabled);
    });
    
    // Toggle notifikationer vid klick
    notificationsBtn.addEventListener('click', async () => {
        try {
            const result = await chrome.storage.local.get(['notificationsEnabled']);
            const currentState = result.notificationsEnabled !== false;
            const newState = !currentState;
            
            await chrome.storage.local.set({ notificationsEnabled: newState });
            updateButtonState(newState);
            
            console.log(`Notifications ${newState ? 'enabled' : 'disabled'}`);
        } catch (error) {
            console.error('Error toggling notifications:', error);
        }
    });
    
    const notificationBtnIcon = document.getElementById('notificationBtnIcon');

    function updateButtonState(enabled) {
        if (enabled) {
            notificationIcon.textContent = '🔔';
            if (notificationBtnIcon) notificationBtnIcon.textContent = '🔔';
            notificationsBtn.style.opacity = '1';
        } else {
            notificationIcon.textContent = '🔕';
            if (notificationBtnIcon) notificationBtnIcon.textContent = '🔕';
            notificationsBtn.style.opacity = '0.6';
        }
    }
};

export { initNotificationsButton };
