jQuery( document ).ready( function( $ ) {
	'use strict';

	var cookieName = 'woostify_compare_list';

	function getCompareList() {
		var cookie = getCookie( cookieName );
		return cookie ? cookie.split(',') : [];
	}

	function setCompareList( list ) {
		setCookie( cookieName, list.join(','), 30 );
	}

	function setCookie(cname, cvalue, exdays) {
		var d = new Date();
		d.setTime(d.getTime() + (exdays*24*60*60*1000));
		var expires = "expires="+ d.toUTCString();
		document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
	}

	function getCookie(cname) {
		var name = cname + "=";
		var decodedCookie = decodeURIComponent(document.cookie);
		var ca = decodedCookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') {
				c = c.substring(1);
			}
			if (c.indexOf(name) == 0) {
				return c.substring(name.length, c.length);
			}
		}
		return "";
	}

	// Update active state of buttons on load
	function updateButtonsState() {
		var list = getCompareList();
		$( '.product-compare-btn, .woostify-compare-button-single' ).removeClass( 'active' );
		list.forEach( function( pid ) {
			$( '.product-compare-btn[data-pid="' + pid + '"], .woostify-compare-button-single[data-pid="' + pid + '"]' ).addClass( 'active' );
		});
	}

	// Update header compare count badge
	function updateHeaderCompareCount() {
		var list = getCompareList();
		var count = list.length;
		var $countBadge = $( '.compare-item-count' );
		if ( $countBadge.length ) {
			$countBadge.text( count );
		}
	}

	updateButtonsState();
	updateHeaderCompareCount();

	// Add/Remove from compare handler
	$( document.body ).on( 'click', '.product-compare-btn, .woostify-compare-button-single', function( e ) {
		var $this = $( this );

		// If already active, let the link redirect to the compare page
		if ( $this.hasClass( 'active' ) ) {
			var href = $this.attr( 'href' );
			if ( href && href !== '#' ) {
				return;
			}
		}

		e.preventDefault();
		var pid = $this.data( 'pid' ).toString();
		var list = getCompareList();
		var index = list.indexOf( pid );

		if ( index > -1 ) {
			// Remove
			list.splice( index, 1 );
			$( '.product-compare-btn[data-pid="' + pid + '"], .woostify-compare-button-single[data-pid="' + pid + '"]' ).removeClass( 'active' );
		} else {
			// Add
			list.push( pid );
			$( '.product-compare-btn[data-pid="' + pid + '"], .woostify-compare-button-single[data-pid="' + pid + '"]' ).addClass( 'active' );
		}

		setCompareList( list );
		updateHeaderCompareCount();

		// If on compare page, reload to update table
		if ( $( '.woostify-compare-page' ).length ) {
			window.location.reload();
		}
	});

	// Remove button in table handler
	$( document.body ).on( 'click', '.woostify-remove-compare', function( e ) {
		e.preventDefault();
		var pid = $( this ).data( 'product-id' ).toString();
		var list = getCompareList();
		var index = list.indexOf( pid );

		if ( index > -1 ) {
			list.splice( index, 1 );
			setCompareList( list );
			updateHeaderCompareCount();
		}

		// Dynamically remove the column
		var $table = $( '.woostify-compare-table' );
		var $cols = $table.find( '[data-product-id="' + pid + '"]' );
		
		$cols.fadeOut( 300, function() {
			$( this ).remove();
			// Check if table is empty
			if ( $table.find( 'thead th.compare-product-header' ).length === 0 ) {
				window.location.reload(); // Reload to show empty message
			}
		});
	});
});
