<?php
/**
 * ShopBar Theme Asset Enqueuing
 *
 * Handles enqueuing of all CSS and JavaScript assets.
 *
 * @package shopbar
 */

defined( 'ABSPATH' ) || exit;

/**
 * Enqueue Styles and Scripts
 *
 * Enqueues all front-end styles and scripts.
 *
 * @return void
 */
function shopbar_scripts() {
	// Google Fonts
	wp_enqueue_style( 'shopbar-google-fonts', get_template_directory_uri() . '/assets/fonts/google-fonts.css', array(), wp_get_theme()->get( 'Version' ) );
	
	// Style CSS
	$shopbar_style_ver = filemtime( get_template_directory() . '/style.css' );
	wp_enqueue_style( 'shopbar-style', get_stylesheet_uri(), array(), $shopbar_style_ver );
	
	// Main theme CSS
	wp_enqueue_style( 'shopbar-main-style', get_template_directory_uri() . '/assets/css/main.css', array(), wp_get_theme()->get('Version') );
	
	// Shop CSS
	if ( shopbar_is_active_woocommerce() ) {
		wp_enqueue_style( 'shopbar-shop-style', get_template_directory_uri() . '/assets/css/shop.css', array(), wp_get_theme()->get('Version') );
	}

	// Product Single CSS
	if ( shopbar_is_active_woocommerce()) {
		wp_enqueue_style( 'shopbar-product-single-style', get_template_directory_uri() . '/assets/css/product-single.css', array(), wp_get_theme()->get('Version') );
	}

	// Blog CSS
	if ( is_home() || is_archive() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_singular( 'post' ) ) {
		wp_enqueue_style( 'shopbar-blog-style', get_template_directory_uri() . '/assets/css/blog.css', array(), wp_get_theme()->get('Version') );
	}

	// Responsive CSS
	wp_enqueue_style( 'shopbar-responsive-style', get_template_directory_uri() . '/assets/css/responsive.css', array(), wp_get_theme()->get('Version') );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) :
		wp_enqueue_script( 'comment-reply' );
	endif;

	// Main theme JavaScript
	$shopbar_script_ver = filemtime( get_template_directory() . '/assets/js/main.js' );
	wp_enqueue_script( 'shopbar-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), $shopbar_script_ver, true );

	// Shop JavaScript
	if ( shopbar_is_active_woocommerce() ) {
		wp_enqueue_script( 'shopbar-shop-script', get_template_directory_uri() . '/assets/js/shop.js', array('jquery'), wp_get_theme()->get('Version'), true );

		// Inline script: shop base URL for archive product pages.
		if ( function_exists( 'wc_get_page_permalink' ) ) {
			$shop_base_url = esc_js( wp_parse_url( wc_get_page_permalink( 'shop' ), PHP_URL_PATH ) );
			wp_add_inline_script( 'shopbar-shop-script', "var shopbar_shop_base_url = '{$shop_base_url}';", 'before' );
		}

		// Product Single JavaScript
		if ( is_product() ) {
			wp_enqueue_script( 'shopbar-product-single-script', get_template_directory_uri() . '/assets/js/product-single.js', array('jquery'), wp_get_theme()->get('Version'), true );
			
			// Localize product single script with checkout URL and currency format data
			wp_localize_script( 'shopbar-product-single-script', 'shopbar_product_single', array(
				'checkout_url' => function_exists( 'wc_get_checkout_url' ) ? wc_get_checkout_url() : '',
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'currency_symbol' => function_exists( 'get_woocommerce_currency_symbol' ) ? get_woocommerce_currency_symbol() : '$',
				'currency_pos' => get_option( 'woocommerce_currency_pos', 'left' ),
				'decimal_sep' => function_exists( 'wc_get_price_decimal_separator' ) ? wc_get_price_decimal_separator() : '.',
				'thousand_sep' => function_exists( 'wc_get_price_thousand_separator' ) ? wc_get_price_thousand_separator() : ',',
				'decimals' => function_exists( 'wc_get_price_decimals' ) ? wc_get_price_decimals() : 2,
				'texts' => array(
					'review_submitted' => esc_html__( 'Thank you for your review!', 'shopbar' ),
					'review_pending' => esc_html__( 'Your review is pending approval.', 'shopbar' ),
					'select_options' => esc_html__( 'Please select all options before adding to cart.', 'shopbar' ),
				),
			) );
		}
		
		// Ensure WooCommerce variation scripts are loaded on product pages
		if ( is_product() ) {
			wp_enqueue_script( 'wc-add-to-cart-variation' );
		}
		
		// Build WooCommerce AJAX parameters
		$wc_ajax_params = array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'wc_ajax_url' => class_exists( 'WC_AJAX' ) ? WC_AJAX::get_endpoint( '%%endpoint%%' ) : '/?wc-ajax=%%endpoint%%',
			'i18n_view_cart' => esc_attr__( 'View cart', 'shopbar' ),
			'cart_url' => function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : '',
			'is_cart' => function_exists( 'is_cart' ) ? is_cart() : false,
			'cart_redirect_after_add' => get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ? 1 : 0,
			'cart_data_nonce' => wp_create_nonce( 'shopbar_cart_data_nonce' ),
		);
		
		// Localize script with WooCommerce AJAX parameters
		wp_localize_script( 'shopbar-script', 'shopbar_wc_params', $wc_ajax_params );
		wp_localize_script( 'shopbar-shop-script', 'shopbar_wc_params', $wc_ajax_params );
		
		// Localize shop script for AJAX
		wp_localize_script( 'shopbar-shop-script', 'shopbar_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce' => wp_create_nonce( 'shopbar_ajax_nonce' ),
		));
	}

	// Blog JavaScript
	if ( is_home() || is_archive() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_singular( 'post' ) ) {
		wp_enqueue_script( 'shopbar-blog-script', get_template_directory_uri() . '/assets/js/blog.js', array('jquery'), wp_get_theme()->get('Version'), true );

		// Localize blog script for AJAX
		wp_localize_script( 'shopbar-blog-script', 'shopbar_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'shopbar_ajax_nonce' ),
		));
	}

	// Cart Page Assets
	if ( shopbar_is_active_woocommerce() && is_cart() ) {
		wp_enqueue_style( 'shopbar-cart-style', get_template_directory_uri() . '/assets/css/cart.css', array(), wp_get_theme()->get('Version') );
		wp_enqueue_script( 'shopbar-cart-script', get_template_directory_uri() . '/assets/js/cart.js', array('jquery'), wp_get_theme()->get('Version'), true );
		wp_localize_script( 'shopbar-cart-script', 'shopbar_cart_ajax', array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'shopbar_cart_nonce' ),
		) );
		wp_localize_script( 'shopbar-cart-script', 'shopbar_cart_texts', array(
			'remove_confirm' => esc_html__( 'Remove %s from cart?', 'shopbar' ),
		) );

		// Cart Page AJAX Quantity Update
		wp_enqueue_script(
			'shopbar-cart-qty-ajax-js',
			get_template_directory_uri() . '/assets/js/cart-qty-ajax.js',
			array( 'jquery', 'shopbar-cart-script' ),
			wp_get_theme()->get( 'Version' ),
			true
		);
		wp_localize_script( 'shopbar-cart-qty-ajax-js', 'shopbarCartQty', array(
			'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
			'nonce'     => wp_create_nonce( 'shopbar_cart_qty_nonce' ),
			'i18nItem'  => __( '%s Item', 'shopbar' ),
			'i18nItems' => __( '%s Items', 'shopbar' ),
		) );
	}

	// Checkout Page Assets
	if ( shopbar_is_active_woocommerce() && is_checkout() ) {
		wp_enqueue_style( 'shopbar-checkout-style', get_template_directory_uri() . '/assets/css/checkout.css', array(), wp_get_theme()->get('Version') );
		wp_enqueue_script( 'shopbar-checkout-script', get_template_directory_uri() . '/assets/js/checkout.js', array('jquery'), wp_get_theme()->get('Version'), true );
	}

	// My Account Page Assets
	if ( shopbar_is_active_woocommerce() && is_account_page() ) {
		wp_enqueue_style( 'shopbar-account-style', get_template_directory_uri() . '/assets/css/account.css', array(), wp_get_theme()->get('Version') );
	}
}
add_action( 'wp_enqueue_scripts', 'shopbar_scripts' );

/**
 * Inject cart AJAX update CSS.
 * Adds a subtle loading indicator when cart quantities are being updated via AJAX.
 */
function shopbar_cart_ajax_inline_css() {
	if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'is_cart' ) || ! is_cart() ) {
		return;
	}

	$css = '
		.shopbar-cart-updating .ci-total {
			opacity: 0.4;
			transition: opacity 0.2s ease;
		}
		.shopbar-cart-updating .ci-qty {
			opacity: 0.6;
			pointer-events: none;
		}
	';

	wp_register_style( 'shopbar-cart-ajax', false );
	wp_enqueue_style( 'shopbar-cart-ajax' );
	wp_add_inline_style( 'shopbar-cart-ajax', $css );
}
add_action( 'wp_enqueue_scripts', 'shopbar_cart_ajax_inline_css', 20 );

/**
 * Inject inline CSS for the custom logo width.
 *
 * @return void
 */
function shopbar_logo_width_inline_css() {
	$logo_width = absint( get_theme_mod( 'shopbar_logo_width', 180 ) );

	if ( $logo_width > 0 ) {
		$css = '.site-branding .custom-logo{max-width:' . $logo_width . 'px !important;}';
		wp_add_inline_style( 'shopbar-style', $css );
	}
}
add_action( 'wp_enqueue_scripts', 'shopbar_logo_width_inline_css', 20 );
