<?php
/**
 * Toast Notification System Integration Helper
 * 
 * This file provides easy integration of the toast notification system
 * 
 * @package VMPFence
 * @since 2.1.0
 * 
 * Usage Example:
 * ---------------
 * 
 * // In your main plugin file or admin class:
 * require_once plugin_dir_path(__FILE__) . 'admin/views/partials/toast-notification-helper.php';
 * 
 * // Then add to your admin_enqueue_scripts action:
 * add_action('admin_enqueue_scripts', 'vmpfence_enqueue_notifications');
 * 
 * // And include the container in your admin pages:
 * vmpfence_render_notification_container();
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Enqueue toast notification assets
 * Call this from your admin_enqueue_scripts action
 * 
 * @param string $hook The current admin page hook
 */
function vmpfence_enqueue_notifications($hook = '') {
    // Only load on VMPFence admin pages
    // You can customize this condition based on your needs
    if (strpos($hook, 'vmpfence') === false && strpos($hook, 'vmp-fence') === false) {
        // Uncomment the line below if you want to load ONLY on specific pages
        // return;
    }
    
    // Enqueue CSS
    wp_enqueue_style(
        'vmpfence-toast-notifications',
        plugins_url('../../assets/css/toast-notifications.css', __FILE__),
        array(),
        '1.0.0',
        'all'
    );
    
    // Enqueue JavaScript
    wp_enqueue_script(
        'vmpfence-toast-notifications',
        plugins_url('../../assets/js/toast-notifications.js', __FILE__),
        array('jquery'),
        '1.0.0',
        true
    );
}

/**
 * Render notification container
 * Call this in your admin page templates
 * 
 * @return void
 */
function vmpfence_render_notification_container() {
    require_once plugin_dir_path(__FILE__) . 'toast-notification-container.php';
}

/**
 * JavaScript helper function to show notification from PHP
 * This creates inline JS to show a notification after page load
 * 
 * @param string $message The notification message
 * @param string $type The notification type (success, error, warning, info)
 * @param string $title Optional title for the notification
 * @param int $duration Optional duration in milliseconds
 * 
 * Usage Example:
 * if (isset($_POST['save_settings'])) {
 *     // Save settings...
 *     vmpfence_show_notification('Settings saved successfully!', 'success');
 * }
 */
function vmpfence_show_notification($message, $type = 'info', $title = '', $duration = null) {
    // Escape the message for JavaScript
    $message = esc_js($message);
    $type = esc_js($type);
    $title = $title ? esc_js($title) : '';
    
    // Build JavaScript call
    $js = "jQuery(document).ready(function($) {";
    $js .= "if (typeof VMPFenceNotifications !== 'undefined') {";
    
    if ($duration !== null) {
        if ($title) {
            $js .= "VMPFenceNotifications.showToast('{$message}', '{$type}', {$duration}, '{$title}');";
        } else {
            $js .= "VMPFenceNotifications.showToast('{$message}', '{$type}', {$duration});";
        }
    } else {
        if ($title) {
            $js .= "VMPFenceNotifications.showToast('{$message}', '{$type}', null, '{$title}');";
        } else {
            switch ($type) {
                case 'success':
                    $js .= "VMPFenceNotifications.showSuccess('{$message}');";
                    break;
                case 'error':
                    $js .= "VMPFenceNotifications.showError('{$message}');";
                    break;
                case 'warning':
                    $js .= "VMPFenceNotifications.showWarning('{$message}');";
                    break;
                default:
                    $js .= "VMPFenceNotifications.showInfo('{$message}');";
            }
        }
    }
    
    $js .= "}";
    $js .= "});";
    
    // Output inline script
    wp_add_inline_script('vmpfence-toast-notifications', $js);
}

/**
 * Store notification in session/transient to show after redirect
 * Useful for showing notifications after form submissions that redirect
 * 
 * @param string $message The notification message
 * @param string $type The notification type
 * @param string $title Optional title
 */
function vmpfence_set_flash_notification($message, $type = 'info', $title = '') {
    $user_id = get_current_user_id();
    $notification = array(
        'message' => $message,
        'type' => $type,
        'title' => $title
    );
    set_transient('vmpfence_flash_notification_' . $user_id, $notification, 60);
}

/**
 * Display and clear flash notification
 * Call this at the beginning of your admin page
 */
function vmpfence_display_flash_notification() {
    $user_id = get_current_user_id();
    $notification = get_transient('vmpfence_flash_notification_' . $user_id);
    
    if ($notification) {
        delete_transient('vmpfence_flash_notification_' . $user_id);
        vmpfence_show_notification(
            $notification['message'],
            $notification['type'],
            $notification['title']
        );
    }
}
