/**
 * Havenlytics AJAX UI Module
 * 
 * @package     Havenlytics
 * @version     2.0.1
 */

(function($) {
    'use strict';

    class HavenlyticsUI {
        constructor(main) {
            this.main = main;
            this.loadingTimeout = null;
        }
        
        init() {
            // UI initialization if needed
            this.hvnly_bindEvents();
        }
        
        hvnly_bindEvents() {
            // Optional: Add any UI-specific event bindings here
            $(document).on('hvnly_ui_reset_loading', () => {
                this.hideLoading();
            });
        }
        
        showLoading() {
            // Clear any pending hide timeout
            if (this.loadingTimeout) {
                clearTimeout(this.loadingTimeout);
                this.loadingTimeout = null;
            }
            
            const propertyGrid = $('#hvnly-property-grid');
            if (propertyGrid.length) {
                propertyGrid.addClass('hvnly-loading');
            }
            
            // Get loading text from data attribute or window object
            const loadMoreContainer = $('#hvnly-load-more');
            const loadingText = loadMoreContainer.data('loading-text') || 
                               (window.hvnly_PROPERTY_ajax?.loading_text || 'Loading...');
            
            const loadMoreBtn = $('#hvnlyLoadMorePropertyBtn');
            if (loadMoreBtn.length) {
                loadMoreBtn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> ' + loadingText);
            }
        }
        
        hideLoading() {
            const propertyGrid = $('#hvnly-property-grid');
            if (propertyGrid.length) {
                propertyGrid.removeClass('hvnly-loading');
            }
            
            // Get load more text from data attribute or window object
            const loadMoreContainer = $('#hvnly-load-more');
            const loadMoreText = loadMoreContainer.data('load-more-text') || 
                                (window.hvnly_PROPERTY_ajax?.load_more_text || 'Load More Properties');
            
            const loadMoreBtn = $('#hvnlyLoadMorePropertyBtn');
            if (loadMoreBtn.length) {
                loadMoreBtn.prop('disabled', false).html('<span class="hvnly-property-load-more-text">' + loadMoreText + '</span><i class="fas fa-plus"></i>');
            }
        }
        
        showError(message) {
            // Remove existing error messages
            $('.hvnly-ajax-error').remove();
            
            // Don't show error if it's just a cancelled request
            if (message && message === 'abort') {
                return;
            }
            
            const errorDiv = $('<div class="hvnly-ajax-error" role="alert">')
                .text(message || window.hvnly_PROPERTY_ajax?.error_message || 'An error occurred. Please try again.')
                .css({
                    background: '#f8d7da',
                    color: '#721c24',
                    padding: '10px 15px',
                    margin: '10px 0',
                    border: '1px solid #f5c6cb',
                    borderRadius: '4px',
                    fontSize: '14px'
                });
            
            const propertyGrid = $('#hvnly-property-grid');
            if (propertyGrid.length) {
                errorDiv.insertBefore(propertyGrid);
                
                // Auto-hide error after 5 seconds
                setTimeout(() => {
                    errorDiv.fadeOut(300, function() {
                        $(this).remove();
                    });
                }, 5000);
            } else {
                // Fallback: prepend to container
                const container = $('#hvnly-property-container');
                if (container.length) {
                    errorDiv.prependTo(container);
                    setTimeout(() => errorDiv.fadeOut(300, () => errorDiv.remove()), 5000);
                }
            }
        }
        
        // Update load more button text dynamically after AJAX load
        updateLoadMoreButtonText() {
            const loadMoreContainer = $('#hvnly-load-more');
            if (loadMoreContainer.length) {
                const loadMoreText = loadMoreContainer.data('load-more-text') || 
                                    (window.hvnly_PROPERTY_ajax?.load_more_text || 'Load More Properties');
                const loadMoreBtn = $('#hvnlyLoadMorePropertyBtn');
                if (loadMoreBtn.length) {
                    loadMoreBtn.html('<span class="hvnly-property-load-more-text">' + loadMoreText + '</span><i class="fas fa-plus"></i>');
                }
            }
        }
        
        // New helper method to show success message
        hvnly_showSuccess(message) {
            $('.hvnly-ajax-success').remove();
            
            const successDiv = $('<div class="hvnly-ajax-success" role="status">')
                .text(message || window.hvnly_PROPERTY_ajax?.success_message || 'Success!')
                .css({
                    background: '#d4edda',
                    color: '#155724',
                    padding: '10px 15px',
                    margin: '10px 0',
                    border: '1px solid #c3e6cb',
                    borderRadius: '4px',
                    fontSize: '14px'
                });
            
            const propertyGrid = $('#hvnly-property-grid');
            if (propertyGrid.length) {
                successDiv.insertBefore(propertyGrid);
                setTimeout(() => successDiv.fadeOut(300, () => successDiv.remove()), 3000);
            }
        }
        
        // Reset UI state completely
        hvnly_resetUI() {
            this.hideLoading();
            $('.hvnly-ajax-error, .hvnly-ajax-success').remove();
            
            // Reset scroll position if needed
            const container = $('#hvnly-property-container');
            if (container.length) {
                container.scrollTop(0);
            }
        }
    }

    window.HavenlyticsUI = HavenlyticsUI;

})(jQuery);