/**
 * SalonPro X - Animations Controller
 * Scroll-based animations, counter animations, entrance effects
 */

$(function() {
    'use strict';

    // Initialize intersection observer for scroll animations
    initScrollAnimations();

    // Initialize counter animations
    initCounters();

    // Add entrance animations to cards on page load
    initEntranceAnimations();
});

/**
 * Initialize Intersection Observer for scroll-triggered animations
 */
function initScrollAnimations() {
    if (!('IntersectionObserver' in window)) {
        $('.animate-on-scroll').addClass('animate-fade-in');
        return;
    }

    const observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                const $el = $(entry.target);
                const animation = $el.data('animation') || 'animate-fade-in-up';
                const delay = $el.data('delay') || 0;

                setTimeout(function() {
                    $el.addClass(animation);
                    $el.css('opacity', '1');
                }, delay);

                observer.unobserve(entry.target);
            }
        });
    }, {
        threshold: 0.1,
        rootMargin: '0px 0px -50px 0px'
    });

    $('.animate-on-scroll').each(function() {
        $(this).css('opacity', '0');
        observer.observe(this);
    });
}

/**
 * Initialize number counter animations
 */
function initCounters() {
    $('.counter').each(function() {
        const $el = $(this);
        const target = parseFloat($el.data('target') || $el.text().replace(/[^0-9.]/g, ''));
        const duration = parseInt($el.data('duration') || 1500);
        const prefix = $el.data('prefix') || '';
        const suffix = $el.data('suffix') || '';
        const decimals = parseInt($el.data('decimals') || 0);
        let counted = false;

        const observer = new IntersectionObserver(function(entries) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting && !counted) {
                    counted = true;
                    animateCounter($el, 0, target, duration, prefix, suffix, decimals);
                    observer.unobserve(entry.target);
                }
            });
        }, { threshold: 0.5 });

        if ($el.is(':visible')) {
            observer.observe($el[0]);
        } else {
            $el.text(prefix + target.toFixed(decimals) + suffix);
        }
    });
}

/**
 * Animate a single counter element
 */
function animateCounter($el, start, end, duration, prefix, suffix, decimals) {
    const startTime = performance.now();

    function update(currentTime) {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);
        const ease = 1 - Math.pow(1 - progress, 3);
        const current = start + (end - start) * ease;

        $el.text(prefix + current.toFixed(decimals) + suffix);

        if (progress < 1) {
            requestAnimationFrame(update);
        } else {
            $el.text(prefix + end.toFixed(decimals) + suffix);
        }
    }

    requestAnimationFrame(update);
}

/**
 * Entrance animations for cards on page load with stagger
 */
function initEntranceAnimations() {
    $('.stagger-animate').each(function() {
        const $container = $(this);
        $container.children().each(function(index) {
            const $el = $(this);
            const delay = index * 80;
            setTimeout(function() {
                $el.addClass('animate-fade-in-up');
                $el.css('opacity', '1');
            }, delay);
        });
    });
}

/**
 * Add shimmer loading effect
 */
function addShimmerEffect($container) {
    $container.addClass('shimmer-loading');
}

/**
 * Remove shimmer loading effect
 */
function removeShimmerEffect($container) {
    $container.removeClass('shimmer-loading');
}
