/**
 * Wishlist page JavaScript functionality
 */
(function($) {
    'use strict';

    const WishlistManager = {
        // Initialize the wishlist functionality
        init() {
            this.bindEvents();
            this.updateWishlistCount();
            this.loadInitialWishlistCount();
        },

        // Load initial wishlist count
        loadInitialWishlistCount() {
            $.ajax({
                url: proddisp_params.ajax_url,
                type: 'POST',
                data: {
                    action: 'proddisp_get_wishlist',
                    nonce: proddisp_params.nonce
                },
                success: (response) => {
                    if (response.success) {
                        this.updateWishlistCount(response.data.count);
                        this.updateFloatingButtonCount(response.data.count);
                        
                        // Also update sidebar count if available
                        if (typeof window.WishlistSidebar !== 'undefined') {
                            window.WishlistSidebar.updateFloatingButtonCount(response.data.count);
                        }
                    }
                },
                error: () => {
                    console.log('Failed to load initial wishlist count');
                }
            });
        },

        // Bind all event listeners
        bindEvents() {
            // Wishlist toggle buttons (main functionality)
            $(document).on('click', '.proddisp-wishlist-button, .wcps-wishlist-button', this.toggleWishlist.bind(this));
           
            // Remove from wishlist buttons
            $(document).on('click', '.wcps-wishlist-remove-item, .wcps-wishlist-remove-btn', this.removeFromWishlist.bind(this));
            
            // Add to cart from wishlist
            $(document).on('click', '.wcps-wishlist-add-to-cart', this.addToCartFromWishlist.bind(this));
            
            // Floating button click (open sidebar if available, otherwise modal)
            $(document).on('click', '#proddisp-floating-wishlist, .proddisp-floating-wishlist, .wcps-floating-wishlist-btn', function(e) {
                e.preventDefault();
                console.log('Floating button clicked');
                
                // Check if sidebar functionality is available and sidebar assets are loaded
                if (typeof window.WishlistSidebar !== 'undefined' && 
                    window.WishlistSidebar.toggleSidebar && 
                    $('.wcps-wishlist-sidebar').length > 0) {
                    console.log('Opening sidebar');
                    window.WishlistSidebar.toggleSidebar();
                } else {
                    console.log('Opening modal (sidebar not available)');
                    WishlistManager.openWishlistModal();
                }
            });
            
            // Modal close
            $(document).on('click', '.wcps-modal-close, .wcps-modal.wishlist-modal-container', function(e) {
                if (e.target === this) {
                    WishlistManager.closeWishlistModal();
                }
            });
            
        },

        
        // Show notification
        showNotification(message, type = 'info') {
            // Remove existing notifications
            $('.wcps-notification-cart').remove();
            
            const $notification = $(`
                <div class="wcps-notification-cart wcps-notification-cart-${type}">
                    <span class="wcps-notification-cart-message">${message}</span>
                    <button class="wcps-notification-cart-close">&times;</button>
                </div>
            `);
            
            $('body').append($notification);
            
            // Show notification
            setTimeout(() => {
                $notification.addClass('show');
            }, 100);
            
            // Auto hide after 5 seconds
            setTimeout(() => {
                this.hideNotification($notification);
            }, 5000);
            
            // Close button
            $notification.find('.wcps-notification-cart-close').on('click', () => {
                this.hideNotification($notification);
            });
        },

        // Hide notification
        hideNotification($notification) {
            $notification.removeClass('show');
            setTimeout(() => {
                $notification.remove();
            }, 300);
        },

        // Open wishlist modal
        openWishlistModal() {
            const $modal = $('#wcps-wishlist-modal');
            if ($modal.length) {
                $modal.show();
                this.loadWishlistInModal();
            }
        },

        // Close wishlist modal
        closeWishlistModal() {
            $('#wcps-wishlist-modal').hide();
        },

        // Load wishlist items in modal
        loadWishlistInModal() {
            const $container = $('#wcps-wishlist-container');
            $container.html('<div class="loading">Loading wishlist...</div>');

            $.ajax({
                url: proddisp_params.ajax_url,
                type: 'POST',
                data: {
                    action: 'proddisp_get_wishlist',
                    nonce: proddisp_params.nonce
                },
                success: (response) => {
                    if (response.success) {
                        $container.html(response.data.html);
                        this.updateWishlistCount(response.data.count);
                    } else {
                        $container.html('<div class="error">Failed to load wishlist</div>');
                    }
                },
                error: () => {
                    $container.html('<div class="error">Failed to load wishlist</div>');
                }
            });
        },

    };

    // Initialize when document is ready
    $(document).ready(() => {
        WishlistManager.init();
    });

    // Make it globally available
    window.WishlistManager = WishlistManager;

})(jQuery);