<?php
/**
 * Electronics Retail Shop functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package electronics-retail-shop
 * @since electronics-retail-shop 1.0
 */

if ( ! function_exists( 'electronics_retail_shop_support' ) ) :

	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 *
	 * @since electronics-retail-shop 1.0
	 *
	 * @return void
	 */
	function electronics_retail_shop_support() {

		load_theme_textdomain( 'electronics-retail-shop', get_template_directory() . '/languages' );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		// Add support for block styles.
		add_theme_support( 'wp-block-styles' );

		add_theme_support( 'align-wide' );

		// Enqueue editor styles.
		add_editor_style( 'style.css' );

		add_theme_support( 'responsive-embeds' );
		
		// Add support for experimental link color control.
		add_theme_support( 'experimental-link-color' );
	}

endif;

add_action( 'after_setup_theme', 'electronics_retail_shop_support' );

if ( ! function_exists( 'electronics_retail_shop_styles' ) ) :

	/**
	 * Enqueue styles.
	 *
	 * @since electronics-retail-shop 1.0
	 *
	 * @return void
	 */
	function electronics_retail_shop_styles() {

		// Register theme stylesheet.
		wp_register_style(
			'electronics-retail-shop-style',
			get_template_directory_uri() . '/style.css',
			array(),
			wp_get_theme()->get( 'Version' )
		);

		wp_enqueue_style(
			'electronics-retail-shop-animate-css',
			esc_url(get_template_directory_uri()).'/assets/css/animate.css' 
		);

		// Enqueue theme stylesheet.
		wp_enqueue_style( 'electronics-retail-shop-style' );

		wp_style_add_data( 'electronics-retail-shop-style', 'rtl', 'replace' );

		wp_enqueue_style( 'dashicons' );

		//font-awesome
		wp_enqueue_style( 'electronics-retail-shop-fontawesome', get_template_directory_uri() . '/inc/fontawesome/css/all.css', array(), '6.7.0' );

		wp_enqueue_style('electronics-retail-shop-owl.carousel-style',
		esc_url(get_template_directory_uri()) . '/assets/css/swiper-bundle.css',
		array()
		);
		wp_enqueue_style('electronics-retail-shop-swiper-css',
		esc_url(get_template_directory_uri()) . '/assets/css/owl.carousel.css',
		array()
		);
		
	}

endif;

add_action( 'wp_enqueue_scripts', 'electronics_retail_shop_styles' );

/* Enqueue Custom Js */
function electronics_retail_shop_scripts() {

	wp_enqueue_script( 
		'electronics-retail-shop-wow', esc_url(get_template_directory_uri()) . '/assets/js/wow.js', 
		array('jquery') 
	);

	wp_enqueue_script(
		'electronics-retail-shop-electronics-retail-shop-custom', esc_url(get_template_directory_uri()) . '/assets/js/custom.js',
		array('jquery')
	);

	wp_enqueue_script(
        'electronics-retail-shop-scroll-to-top',
        esc_url(get_template_directory_uri()) . '/assets/js/scroll-to-top.js',
        array(), 
        null, 
        true // Load in footer
    );

	wp_enqueue_script(
		'electronics-retail-shop-swiper-js',
		esc_url(get_template_directory_uri()) . '/assets/js/swiper-bundle.js',
		array(),
		true
	);

	wp_enqueue_script(
		'electronics-retail-shop-owl.carousel-js',
		esc_url(get_template_directory_uri()) . '/assets/js/owl.carousel.js',
		array(),
		true
	);
}
add_action( 'wp_enqueue_scripts', 'electronics_retail_shop_scripts' );

function electronics_retail_shop_theme_setting() {
	
// Add block patterns
require get_template_directory() . '/inc/block-pattern.php';

// TGM
require get_template_directory() . '/inc/tgm/plugin-activation.php';

// Add block Style
require get_template_directory() . '/inc/block-style.php';

// Get Started
require get_template_directory() . '/get-started/getstart.php';

// Get Notice
require get_template_directory() . '/get-started/notice.php';

// Get Notice
require get_template_directory() . '/inc/customizer.php';

}
add_action('after_setup_theme', 'electronics_retail_shop_theme_setting');


/* Enqueue admin-notice-script js */
function electronics_retail_shop_admin_notice_assets() {
    wp_enqueue_script(
        'admin-notice-script',
        get_template_directory_uri() . '/get-started/js/admin-notice-script.js',
        array( 'jquery' ),
        null,
        true
    );

    wp_localize_script(
        'admin-notice-script',
        'pluginInstallerData',
        array(
            'ajaxurl'     => admin_url( 'admin-ajax.php' ),
            'nonce'       => wp_create_nonce( 'install_activate_nonce' ),
            'redirectUrl' => admin_url( 'themes.php?page=electronics-retail-shop' ),
        )
    );
}
add_action( 'admin_enqueue_scripts', 'electronics_retail_shop_admin_notice_assets' );

add_action('wp_ajax_check_plugin_activation', function () {
    if (!isset($_POST['plugin']) || empty($_POST['plugin'])) {
        wp_send_json_error(['message' => 'Missing plugin identifier']);
    }

    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    // Map plugin identifiers to their main files
    $electronics_retail_shop_plugin_map = [
        'woocommerce'          => 'woocommerce/woocommerce.php',
        'wordclever_ai_content_writer'    => 'wordclever-ai-content-writer/wordclever.php',
        'wishlist'             => 'yith-woocommerce-wishlist/init.php',        
        'stackable_ultimate_gutenberg_blocks'             => 'stackable-ultimate-gutenberg-blocks/plugin.php',
    ];

    $electronics_retail_shop_requested_plugin = sanitize_text_field($_POST['plugin']);

    if (!isset($electronics_retail_shop_plugin_map[$electronics_retail_shop_requested_plugin])) {
        wp_send_json_error(['message' => 'Invalid plugin']);
    }

    $electronics_retail_shop_plugin_file = $electronics_retail_shop_plugin_map[$electronics_retail_shop_requested_plugin];
    $electronics_retail_shop_is_active   = is_plugin_active($electronics_retail_shop_plugin_file);

    wp_send_json_success(['active' => $electronics_retail_shop_is_active]);
});

add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );
