/**
 * Dashboard Global Options and Notifications
 * @package VMPFence
 */

function showGlobalOptions() {
    document.querySelector('.vmpfence-main-grid').style.display = 'none';
    document.getElementById('vmpfence-global-options').style.display = 'block';
}

function hideGlobalOptions() {
    document.querySelector('.vmpfence-main-grid').style.display = 'block';
    document.getElementById('vmpfence-global-options').style.display = 'none';
}

function closeNotification(closeBtn) {
    // Get the notification item and remove it with animation
    const notificationItem = closeBtn.parentElement;
    const notificationType = notificationItem.getAttribute('data-type');
    
    notificationItem.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
    notificationItem.style.opacity = '0';
    notificationItem.style.transform = 'translateX(100%)';
    
    // Store dismissed notifications with scan timestamp to allow new notifications after new scans
    if (notificationType) {
        const currentScanTime = vmpfence_dashboard_data.last_scan_time;
        const dismissedNotifications = JSON.parse(localStorage.getItem('vmpfence_dismissed_notifications') || '{}');
        
        dismissedNotifications[notificationType] = {
            dismissedAt: new Date().toISOString(),
            scanTime: currentScanTime
        };
        
        localStorage.setItem('vmpfence_dismissed_notifications', JSON.stringify(dismissedNotifications));
    }
    
    setTimeout(function() {
        notificationItem.remove();
        
        // Update the notification counter
        const counterElement = document.querySelector('.vmpfence-notification-count');
        const sectionContent = document.querySelector('.vmpfence-section-content');
        const remainingNotifications = sectionContent.querySelectorAll('.vmpfence-notification-item');
        
        if (counterElement) {
            const newCount = remainingNotifications.length;
            
            if (newCount === 0) {
                counterElement.style.display = 'none';
                sectionContent.innerHTML = '<p style="color: #666; font-style: italic; text-align: center; padding: 20px;">No notifications</p>';
            } else {
                counterElement.textContent = newCount;
            }
        }
    }, 300);
}

// Check for previously dismissed notifications on page load
document.addEventListener('DOMContentLoaded', function() {
    const dismissedNotifications = JSON.parse(localStorage.getItem('vmpfence_dismissed_notifications') || '{}');
    const currentScanTime = vmpfence_dashboard_data.last_scan_time;
    
    // Check each notification type to see if it should be hidden
    Object.keys(dismissedNotifications).forEach(function(type) {
        const dismissedData = dismissedNotifications[type];
        const notification = document.querySelector('.vmpfence-notification-item[data-type="' + type + '"]');
        
        if (notification && dismissedData.scanTime === currentScanTime) {
            notification.style.display = 'none';
        }
    });
    
    // Clean up old dismissed notifications for scans that no longer exist
    const updatedDismissed = {};
    Object.keys(dismissedNotifications).forEach(function(type) {
        if (dismissedNotifications[type].scanTime === currentScanTime) {
            updatedDismissed[type] = dismissedNotifications[type];
        }
    });
    localStorage.setItem('vmpfence_dismissed_notifications', JSON.stringify(updatedDismissed));
    
    // Update counter after hiding dismissed notifications
    const sectionContent = document.querySelector('.vmpfence-section-content');
    const visibleNotifications = sectionContent.querySelectorAll('.vmpfence-notification-item:not([style*="display: none"])');
    const counterElement = document.querySelector('.vmpfence-notification-count');
    
    if (counterElement) {
        const visibleCount = visibleNotifications.length;
        if (visibleCount === 0) {
            counterElement.style.display = 'none';
            sectionContent.innerHTML = '<p style="color: #666; font-style: italic; text-align: center; padding: 20px;">No notifications</p>';
        } else {
            counterElement.textContent = visibleCount;
        }
    }
});

function saveGlobalOptions() {
    // TODO: Implement saving functionality
    
    // Use toast notification if available, otherwise fallback to alert
    if (typeof VMPFenceNotifications !== 'undefined') {
        VMPFenceNotifications.showSuccess(
            'Your global settings have been saved.',
            4000,
            'Settings Saved Successfully!'
        );
    } else {
        // Fallback to alert if toast system not loaded
        alert('Settings saved successfully!');
    }
}

function resetGlobalOptions() {
    // Use toast modal if available, otherwise fallback to confirm/alert
    if (typeof VMPFenceNotifications !== 'undefined') {
        VMPFenceNotifications.showConfirmModal({
            title: 'Reset to Defaults',
            message: 'Are you sure you want to reset all settings to defaults? This action cannot be undone.',
            confirmText: 'Yes, Reset',
            cancelText: 'Cancel',
            type: 'warning',
            showIcon: true
        }).then(function(confirmed) {
            if (confirmed) {
                // TODO: Implement reset functionality
                VMPFenceNotifications.showInfo(
                    'All settings have been reset to their default values.',
                    4000,
                    'Settings Reset Complete'
                );
            }
        });
    } else {
        // Fallback to confirm/alert if toast system not loaded
        if (confirm('Are you sure you want to reset all settings to defaults?')) {
            // TODO: Implement reset functionality
            alert('Settings reset to defaults!');
        }
    }
}
