<?php
/**
 * CCTV Services functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @subpackage CCTV Services
 * @since CCTV Services 1.0
 */

if ( ! function_exists( 'cctv_services_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which runs
 * before the init hook. The init hook is too late for some features, such as indicating
 * support post thumbnails.
 */
function cctv_services_setup() {
	load_theme_textdomain( 'cctv-services', get_template_directory() . '/languages' );

	// Enqueue editor styles.
	add_editor_style(get_stylesheet_directory_uri() . '/assets/css/editor-style.css');
}
endif;
add_action( 'after_setup_theme', 'cctv_services_setup' );

function cctv_services_block_assets(){
	wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/assets/font-awesome/css/all.css', array(), '7.1.0' );
	wp_enqueue_style( 'animatecss', get_template_directory_uri() . '/assets/css/animate.css', array(), '4.1.1' );
	wp_enqueue_style( 'cctv-services-style', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
	wp_enqueue_script('wow-script', get_template_directory_uri() . '/assets/js/wow.js', array('jquery'), false, true);
	wp_enqueue_style( 'owlcarousel-css', get_template_directory_uri() . '/assets/css/owl.carousel.css', array(), '2.3.4' );
	wp_enqueue_script('owlcarousel-js', get_template_directory_uri() . '/assets/js/owl.carousel.js', array('jquery'), false, true);
	wp_enqueue_script('cctv-services-script', get_template_directory_uri() . '/assets/js/script.js', array(
            'wp-hooks',
            'wp-dom-ready',
            'wp-compose',
            'wp-element',
            'wp-data',
            'wp-block-editor',
            'jquery'
        ), '1.0.0', true);
	wp_style_add_data( 'cctv-services-style', 'rtl', 'replace' );
}
add_action('enqueue_block_assets', 'cctv_services_block_assets');

function cctv_services_add_to_cart_text( $text ) {

    if ( is_shop() || is_product_category() ) {
        return 'Add+';
    }

    return $text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'cctv_services_add_to_cart_text' );

/**
 * Load TGM.
 */
require get_template_directory() . '/inc/tgm/tgm.php';

/**
 * Notice.
 */
require_once get_template_directory() . '/inc/notice/notice.php';

/**
 * Theme Info Page.
 */
require get_template_directory() . '/inc/addon.php';

/**
 * Customizer
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Demo import
 */
require get_template_directory() . '/inc/demo-content.php';

function cctv_services_setup_theme() {
	if ( ! defined( 'CCTV_SERVICES_PREMIUM_PAGE' ) ) {
		define('CCTV_SERVICES_PREMIUM_PAGE',__('https://www.theclassictemplates.com/products/cctv-wordpress-theme','cctv-services'));
	}
	if ( ! defined( 'CCTV_SERVICES_PRO_NAME' ) ) {
		define( 'CCTV_SERVICES_PRO_NAME', __( 'About CCTV Services', 'cctv-services' ));
	}
	if ( ! defined( 'CCTV_SERVICES_THEME_PAGE' ) ) {
		define('CCTV_SERVICES_THEME_PAGE',__('https://www.theclassictemplates.com/collections/best-wordpress-templates','cctv-services'));
	}
	if ( ! defined( 'CCTV_SERVICES_SUPPORT' ) ) {
		define('CCTV_SERVICES_SUPPORT',__('https://wordpress.org/support/theme/cctv-services/','cctv-services'));
	}
	if ( ! defined( 'CCTV_SERVICES_REVIEW' ) ) {
		define('CCTV_SERVICES_REVIEW',__('https://wordpress.org/support/theme/cctv-services/reviews/','cctv-services'));
	}
	if ( ! defined( 'CCTV_SERVICES_PRO_DEMO' ) ) {
		define('CCTV_SERVICES_PRO_DEMO',__('https://live.theclassictemplates.com/cctv-services/','cctv-services'));
	}
	if ( ! defined( 'CCTV_SERVICES_THEME_DOCUMENTATION' ) ) {
		define('CCTV_SERVICES_THEME_DOCUMENTATION',__('https://live.theclassictemplates.com/demo/docs/cctv-services-free/','cctv-services'));
	}
	if ( ! defined( 'CCTV_SERVICES_BUNDLE_PAGE' ) ) {
		define('CCTV_SERVICES_BUNDLE_PAGE',__('https://www.theclassictemplates.com/products/wordpress-theme-bundle','cctv-services'));
	}
}
add_action('after_setup_theme', 'cctv_services_setup_theme');

function cctv_services_enqueue_admin_script($hook) {
    // Enqueue admin JS for notices
    wp_enqueue_script('cctv-services-welcome-notice', get_template_directory_uri() . '/inc/notice/notice.js', array('jquery'), '', true);
    
    // Localize script to pass data to JavaScript
wp_localize_script('cctv-services-welcome-notice', 'cctv_services_localize', array(
    'ajax_url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('cctv_services_welcome_nonce'),
    'demo_nonce' => wp_create_nonce('cctv_services_demo_nonce'), // added
    'dismiss_nonce' => wp_create_nonce('cctv_services_welcome_nonce'), 
    'redirect_url' => admin_url('themes.php?page=cctv-services')
));
}
add_action('admin_enqueue_scripts', 'cctv_services_enqueue_admin_script');

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
    $cctv_services_plugin_map = [
    	'woocommerce'                  => 'woocommerce/woocommerce.php',
        'yith-woocommerce-wishlist'    => 'yith-woocommerce-wishlist/plugin.php',
        'classic-blog-grid'            => 'classic-blog-grid/plugin.php'
    ];

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

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

    $cctv_services_plugin_file = $cctv_services_plugin_map[$cctv_services_requested_plugin];
    $cctv_services_is_active   = is_plugin_active($cctv_services_plugin_file);

    wp_send_json_success(['active' => $cctv_services_is_active]);
});
add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

add_action( 'admin_init', function() {
  update_option( 'essential_blocks_quick_setup_shown', true );
  update_option( 'essential_blocks_user_type', 'old' );
  remove_submenu_page( 'admin.php', 'eb-quick-setup' );
});
