<?php
/**
 * FSE Pet Shop functions and definitions
 *
 * @package fse_pet_shop
 * @since 1.0
 */

if ( ! function_exists( 'fse_pet_shop_support' ) ) :
	function fse_pet_shop_support() {

		load_theme_textdomain( 'fse-pet-shop', get_template_directory() . '/languages' );

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

		add_theme_support('woocommerce');

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

		/* Theme Credit link */
		define('FSE_PET_SHOP_BUY_NOW',__('https://www.cretathemes.com/products/pet-shop-wordpress-theme','fse-pet-shop'));
		define('FSE_PET_SHOP_PRO_DEMO',__('https://pattern.cretathemes.com/fse-pet-shop/','fse-pet-shop'));
		define('FSE_PET_SHOP_THEME_DOC',__('https://pattern.cretathemes.com/free-guide/fse-pet-shop/','fse-pet-shop'));
		define('FSE_PET_SHOP_PRO_THEME_DOC',__('https://pattern.cretathemes.com/pro-guide/fse-pet-shop-pro/','fse-pet-shop'));
		define('FSE_PET_SHOP_SUPPORT',__('https://wordpress.org/support/theme/fse-pet-shop/','fse-pet-shop'));
		define('FSE_PET_SHOP_REVIEW',__('https://wordpress.org/support/theme/fse-pet-shop/reviews/#new-post','fse-pet-shop'));
		define('FSE_PET_SHOP_PRO_THEME_BUNDLE',__('https://www.cretathemes.com/products/wordpress-theme-bundle','fse-pet-shop'));
		define('FSE_PET_SHOP_PRO_ALL_THEMES',__('https://www.cretathemes.com/collections/wordpress-block-themes','fse-pet-shop'));

	}
endif;

add_action( 'after_setup_theme', 'fse_pet_shop_support' );

if ( ! function_exists( 'fse_pet_shop_styles' ) ) :
	function fse_pet_shop_styles() {
		// Register theme stylesheet.
		$fse_pet_shop_theme_version = wp_get_theme()->get( 'Version' );

		$fse_pet_shop_version_string = is_string( $fse_pet_shop_theme_version ) ? $fse_pet_shop_theme_version : false;
		wp_enqueue_style(
			'fse-pet-shop-style',
			get_template_directory_uri() . '/style.css',
			array(),
			$fse_pet_shop_version_string
		);

		wp_enqueue_style( 'dashicons' );

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

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

		wp_style_add_data( 'fse-pet-shop-style', 'rtl', 'replace' );

		

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

		// Enqueue Swiper JS
		wp_enqueue_script(
		    'swiper-bundle-scripts',
		    get_template_directory_uri() . '/assets/js/swiper-bundle.js',
		    array('jquery'),
		    $fse_pet_shop_version_string,
		    true
		);

		// Enqueue Custom Script (depends on Swiper too)
		wp_enqueue_script(
		    'fse-pet-shop-custom-script',
		    get_template_directory_uri() . '/assets/js/custom-script.js',
		    array('jquery', 'swiper-bundle-scripts'),
		    $fse_pet_shop_version_string,
		    true
		);
	}
endif;

add_action( 'wp_enqueue_scripts', 'fse_pet_shop_styles' );


// Force YITH Wishlist icon to show on WooCommerce product blocks
add_filter( 'render_block', function( $fse_pet_shop_content, $fse_pet_shop_block ) {
    if ( isset( $fse_pet_shop_block['blockName'] ) && $fse_pet_shop_block['blockName'] === 'woocommerce/product-image' ) {

        // Add wishlist button only if plugin is active
        if ( function_exists( 'YITH_WCWL' ) ) {
            $fse_pet_shop_wishlist_button = do_shortcode('[yith_wcwl_add_to_wishlist]');
            // Add button overlay
            $fse_pet_shop_content .= '<div class="yith-wishlist-block-overlay">' . $fse_pet_shop_wishlist_button . '</div>';
        }
    }
    return $fse_pet_shop_content;
}, 10, 2 );

/* Enqueue admin-notice-script js */
add_action('admin_enqueue_scripts', function ($hook) {
    if ($hook !== 'appearance_page_fse-pet-shop') 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_cretatestimonial_nonce'), // Match this with PHP nonce check
        'redirectUrl' => admin_url('themes.php?page=fse-pet-shop-guide-page'),
    ]);
});

add_action('wp_ajax_check_creta_testimonial_activation', function () {
    include_once ABSPATH . 'wp-admin/includes/plugin.php';
    $fse_pet_shop_plugin_file = 'creta-testimonial-showcase/creta-testimonial-showcase.php';

    if (is_plugin_active($fse_pet_shop_plugin_file)) {
        wp_send_json_success(['active' => true]);
    } else {
        wp_send_json_success(['active' => false]);
    }
});


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

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

// Block Filters
require get_template_directory() . '/inc/block-filters.php';

// Svg icons
require get_template_directory() . '/inc/icon-function.php';

// TGM
require_once get_template_directory() . '/inc/tgm/tgm.php';

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

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

// Add Getstart admin notice
function fse_pet_shop_admin_notice() { 
    global $pagenow;
    $theme_args      = wp_get_theme();
    $meta            = get_option( 'fse_pet_shop_admin_notice' );
    $name            = $theme_args->__get( 'Name' );
    $current_screen  = get_current_screen();

    if( !$meta ){
	    if( is_network_admin() ){
	        return;
	    }

	    if( ! current_user_can( 'manage_options' ) ){
	        return;
	    } if($current_screen->base != 'appearance_page_fse-pet-shop-guide-page' && $current_screen->base != 'toplevel_page_cretats-theme-showcase' ) { ?>

	    <div class="notice notice-success dash-notice">
	        <h1><?php esc_html_e('Hey, Thank you for installing FSE Pet Shop Theme!', 'fse-pet-shop'); ?></h1>
	        <p> <a href="javascript:void(0);" id="install-activate-button" class="button admin-button info-button get-start-btn">
				   <?php echo __('Navigate Getstart', 'fse-pet-shop'); ?>
				</a>

				<script type="text/javascript">
				document.getElementById('install-activate-button').addEventListener('click', function () {
				    const fse_pet_shop_button = this;
				    const fse_pet_shop_redirectUrl = '<?php echo esc_url(admin_url("themes.php?page=fse-pet-shop-guide-page")); ?>';
				    // First, check if plugin is already active
				    jQuery.post(ajaxurl, { action: 'check_creta_testimonial_activation' }, function (response) {
				        if (response.success && response.data.active) {
				            // Plugin already active — just redirect
				            window.location.href = fse_pet_shop_redirectUrl;
				        } else {
				            // Show Installing & Activating only if not already active
				            fse_pet_shop_button.textContent = 'Navigate Getstart';

				            jQuery.post(ajaxurl, {
				                action: 'install_and_activate_creta_testimonial_plugin',
				                nonce: '<?php echo wp_create_nonce("install_activate_nonce"); ?>'
				            }, function (response) {
				                if (response.success) {
				                    window.location.href = fse_pet_shop_redirectUrl;
				                } else {
				                    alert('Failed to activate the plugin.');
				                    fse_pet_shop_button.textContent = 'Try Again';
				                }
				            });
				        }
				    });
				});
				</script>


	        	<a class="button button-primary site-edit" href="<?php echo esc_url( admin_url( 'site-editor.php' ) ); ?>"><?php esc_html_e('Site Editor', 'fse-pet-shop'); ?></a> 
				<a class="button button-primary buy-now-btn" href="<?php echo esc_url( FSE_PET_SHOP_BUY_NOW ); ?>" target="_blank"><?php esc_html_e('Buy Pro', 'fse-pet-shop'); ?></a>
				<a class="button button-primary bundle-btn" href="<?php echo esc_url( FSE_PET_SHOP_PRO_THEME_BUNDLE ); ?>" target="_blank"><?php esc_html_e('Get Bundle', 'fse-pet-shop'); ?></a>
	        </p>
	        <p class="dismiss-link"><strong><a href="?fse_pet_shop_admin_notice=1"><?php esc_html_e( 'Dismiss', 'fse-pet-shop' ); ?></a></strong></p>
	    </div>
	    <?php

	}?>
	    <?php

	}
}

add_action( 'admin_notices', 'fse_pet_shop_admin_notice' );

if( ! function_exists( 'fse_pet_shop_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/
function fse_pet_shop_update_admin_notice(){
    if ( isset( $_GET['fse_pet_shop_admin_notice'] ) && $_GET['fse_pet_shop_admin_notice'] = '1' ) {
        update_option( 'fse_pet_shop_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'fse_pet_shop_update_admin_notice' );

//After Switch theme function
add_action('after_switch_theme', 'fse_pet_shop_getstart_setup_options');
function fse_pet_shop_getstart_setup_options () {
    update_option('fse_pet_shop_admin_notice', FALSE );
}

add_action('admin_bar_menu', 'your_plugin_adminbar_link', 100);
function your_plugin_adminbar_link($wp_admin_bar) {
    $wp_admin_bar->add_node([
        'id'    => 'yourplugin_upgrade',
        'title' => ' Upgrade to Pro',
        'href'  => 'https://www.cretathemes.com/products/pet-shop-wordpress-theme',
        'meta'  => array(
            'target' => '_blank',
        )
    ]);
}