<?php
/**
 * Creative Designer functions and definitions
 *
 * @package creative-designer
 */

if ( ! function_exists( 'creative_designer_support' ) ) :

	function creative_designer_support() {

		load_theme_textdomain( 'creative-designer', get_template_directory() . '/languages' );

		add_theme_support( 'automatic-feed-links' );
		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'align-wide' );
		add_theme_support( 'responsive-embeds' );
		add_theme_support( 'experimental-link-color' );

		// Editor style
		add_editor_style( 'style.css' );
	}

endif;
add_action( 'after_setup_theme', 'creative_designer_support' );

/**
 * Enqueue Styles & Scripts
 */
function creative_designer_assets() {

	$theme_version = wp_get_theme()->get( 'Version' );

	/**
	 * ======================
	 * STYLES
	 * ======================
	 */

	// Main stylesheet
	wp_enqueue_style(
		'creative-designer-style',
		get_template_directory_uri() . '/style.css',
		array(),
		$theme_version
	);

	wp_style_add_data( 'creative-designer-style', 'rtl', 'replace' );

	// Dashicons
	wp_enqueue_style( 'dashicons' );

	// Font Awesome
	wp_enqueue_style(
		'fontawesome',
		get_template_directory_uri() . '/inc/fontawesome/css/all.css',
		array(),
		'6.7.0'
	);

	// Animate CSS
	wp_enqueue_style(
		'animate-css',
		get_template_directory_uri() . '/assets/css/animate.css',
		array(),
		$theme_version
	);

	// Owl Carousel
	wp_enqueue_style(
		'owl-carousel-style',
		get_template_directory_uri() . '/assets/css/owl-carousel.css',
		array(),
		$theme_version
	);

	// Swiper CSS
	wp_enqueue_style(
		'swiper-css',
		get_template_directory_uri() . '/assets/css/swiper-bundle.css',
		array(),
		$theme_version
	);

	/**
	 * ======================
	 * SCRIPTS
	 * ======================
	 */

	// WOW JS
	wp_enqueue_script(
		'wow',
		get_template_directory_uri() . '/assets/js/wow.js',
		array('jquery'),
		$theme_version,
		true
	);

	// Custom JS
	wp_enqueue_script(
		'creative-designer-custom',
		get_template_directory_uri() . '/assets/js/custom.js',
		array('jquery'),
		$theme_version,
		true
	);

	// Scroll to Top
	wp_enqueue_script(
		'creative-designer-scroll-to-top',
		get_template_directory_uri() . '/assets/js/scroll-to-top.js',
		array(),
		$theme_version,
		true
	);

	// Swiper JS
	wp_enqueue_script(
		'swiper-js',
		get_template_directory_uri() . '/assets/js/swiper-bundle.js',
		array(),
		$theme_version,
		true
	);

	// Owl Carousel JS
	wp_enqueue_script(
		'owl-carousel-js',
		get_template_directory_uri() . '/assets/js/owl-carousel.js',
		array('jquery'),
		$theme_version,
		true
	);
}
add_action( 'wp_enqueue_scripts', 'creative_designer_assets' );

/* Enqueue admin-notice-script js */
add_action('admin_enqueue_scripts', function ($hook) {
    if ($hook !== 'appearance_page_creative-designer') return;

    wp_enqueue_script('admin-notice-script', get_template_directory_uri() . '/get-started/js/admin-notice-script.js', ['jquery'], null, true);
    wp_localize_script('admin-notice-script', 'pluginInstallerData', [
        'ajaxurl'     => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('install_activate_nonce'),
        'redirectUrl' => admin_url('themes.php?page=creative-designer'),
    ]);
});

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
    $creative_designer_plugin_map = [
        'woocommerce'          => 'woocommerce/woocommerce.php',
        'wordclever_ai_content_writer'    => 'wordclever-ai-content-writer/wordclever.php',  
		'gutentor' => 'gutentor/gutentor.php',  
    ];

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

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

    $creative_designer_plugin_file = $creative_designer_plugin_map[$creative_designer_requested_plugin];
    $creative_designer_is_active   = is_plugin_active($creative_designer_plugin_file);

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

add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

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

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

// TGM
require get_template_directory() . '/inc/tgm/plugin-activation.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', 'creative_designer_theme_setting');
