<?php
/**
 * Vet Care Center functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Vet Care Center
 */

if ( ! defined( 'VET_CARE_CENTER_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( 'VET_CARE_CENTER_VERSION', wp_get_theme()->get( 'Version' ) );
}

if ( ! function_exists( 'vet_care_center_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 for post thumbnails.
	 */
	function vet_care_center_setup() {

		load_theme_textdomain( 'vet-care-center', get_template_directory() . '/languages' );

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

		add_theme_support( 'align-wide' );

		add_theme_support( 'woocommerce' );

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

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

		// Experimental support for adding blocks inside nav menus
		add_theme_support( 'block-nav-menus' );

		// Add support for experimental link color control.
		add_theme_support( 'experimental-link-color' );
	}
endif;
add_action( 'after_setup_theme', 'vet_care_center_setup' );

function vet_care_center_scripts() {
    $VET_CARE_CENTER_VERSION_string = '1.0.0'; // Add this line

    wp_enqueue_style('vet-care-center-style', get_stylesheet_uri(), array() );
    wp_enqueue_script('jquery-wow', get_template_directory_uri() . '/assets/js/wow.js', array('jquery'));
    wp_enqueue_style('animate-css', get_template_directory_uri() . '/assets/css/animate.css');

    //font-awesome
    wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/font-awesome/css/all.css', array(), '6.7.0' );
    wp_enqueue_style( 'owl.carousel-style', get_template_directory_uri().'/assets/css/owl.carousel.css', array(), $VET_CARE_CENTER_VERSION_string );
    wp_enqueue_script( 'owl.carousel-js', get_template_directory_uri(). '/assets/js/owl.carousel.js', array('jquery') ,$VET_CARE_CENTER_VERSION_string,true);
    
    wp_enqueue_script('vet-care-center-custom-scripts', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'), '', true);
    wp_style_add_data( 'vet-care-center-style', 'rtl', 'replace' );

}
add_action( 'wp_enqueue_scripts', 'vet_care_center_scripts' );


/**
 * Enqueue block editor styles
 */
function vet_care_center_enqueue_block_editor_styles() {
    wp_enqueue_style(
        'vet-care-center-block-editor-style', // Handle
        get_theme_file_uri( '/assets/css/block-editor.css' ), // File URI
        array(), // Dependencies
        filemtime( get_theme_file_path( '/assets/css/block-editor.css' ) ) // Version with file modification time for cache busting
    );
}
add_action( 'enqueue_block_editor_assets', 'vet_care_center_enqueue_block_editor_styles' );


function vet_care_center_init_setup() {

	/**
     * Section Pro
     */
    require get_template_directory() . '/inc/section-pro/customizer.php';

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

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


//////////////////////////////////////////////   Function for Translation Error   //////////////////////////////////////////////////////
function vet_care_center_enqueue_function() {

    /**
    * GET START.
    */
    require get_template_directory() . '/getstarted/vet_care_center_about_page.php';

    define('VET_CARE_CENTER_FREE_SUPPORT',__('https://www.revolutionwp.com/products/vet-care-center','vet-care-center'));
    define('VET_CARE_CENTER_PRO_SUPPORT',__('https://www.revolutionwp.com/support/revolution-wp/','vet-care-center'));
    define('VET_CARE_CENTER_REVIEW',__('https://wordpress.org/support/theme/vet-care-center/reviews/','vet-care-center'));
    define('VET_CARE_CENTER_BUY_NOW',__('https://www.revolutionwp.com/products/veterinary-clinic-wordpress-theme','vet-care-center'));
    define('VET_CARE_CENTER_LIVE_DEMO',__('https://demo.revolutionwp.com/vet-care-center-pro/','vet-care-center'));
    define('VET_CARE_CENTER_PRO_DOC',__('https://demo.revolutionwp.com/wpdocs/vet-care-center-pro/','vet-care-center'));
    define('VET_CARE_CENTER_LITE_DOC',__('https://demo.revolutionwp.com/wpdocs/vet-care-center-free/','vet-care-center'));
    
}
add_action( 'after_setup_theme', 'vet_care_center_enqueue_function' );

// Add admin notice
function vet_care_center_admin_notice() { 
    global $pagenow;
    $vet_care_center_theme_args      = wp_get_theme();
    $vet_care_center_meta            = get_option( 'vet_care_center_admin_notice' );
    $name            = $vet_care_center_theme_args->__get( 'Name' );
    $vet_care_center_current_screen  = get_current_screen();

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

        if( ! current_user_can( 'manage_options' ) ){
            return;
        } 
        
        if( $vet_care_center_current_screen->base !== 'appearance_page_vet_care_center_guide') { ?>

            <div class="notice notice-success vet-care-center-welcome-notice">
                <p class="vet-care-center-dismiss-link">
                    <strong>
                        <a href="<?php echo esc_url( add_query_arg( 'vet_care_center_admin_notice', '1' ) ); ?>">
                            <?php esc_html_e( 'Dismiss', 'vet-care-center' ); ?>
                        </a>
                    </strong>
                </p>

                <div class="vet-care-center-welcome-notice-wrap">
                    <h2 class="vet-care-center-notice-title">
                        <span class="dashicons dashicons-admin-home"></span> 
                        <?php 
                            $vet_care_center_theme_name = wp_get_theme()->get( 'Name' );
                            /* translators: %s!: Theme Name. */
                            echo esc_html( sprintf( __( 'Welcome to the free theme: %s!', 'vet-care-center' ), $vet_care_center_theme_name ) );
                        ?>
                    </h2>
                    <p class="vet-care-center-notice-desc">
                        <?php esc_html_e( 'Get started by exploring the features of your new theme. Customize your design, add your content, and create a site that fits your vision.', 'vet-care-center' ); ?>
                    </p>

                    <div class="vet-care-center-welcome-info">
                        <div class="vet-care-center-welcome-thumb">
                            <img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/screenshot.png' ); ?>" alt="<?php esc_attr_e( 'Theme Screenshot', 'vet-care-center' ); ?>">
                        </div>

                        <div class="vet-care-center-welcome-getting-started">
                            <h3><span class="dashicons dashicons-art"></span> <?php esc_html_e( 'Customize Your Theme', 'vet-care-center' ); ?></h3>
                            <p><?php esc_html_e( 'Want to make it truly yours? Explore the Getting Started Guide to personalize your site to suit your needs.', 'vet-care-center' ); ?></p>
                            <p><a class="info-link button" href="<?php echo esc_url( admin_url( 'themes.php?page=vet-care-center-getstart-page' ) ); ?>"><?php esc_html_e( 'View Getting Started Guide', 'vet-care-center' ); ?></a></p>
                        </div>
                    </div>
                </div>
            </div>

            <?php
        }

    }
}

add_action( 'admin_notices', 'vet_care_center_admin_notice' );

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

add_action('after_switch_theme', 'vet_care_center_setup_options');
function vet_care_center_setup_options () {
    update_option('vet_care_center_admin_notice', FALSE );
}

// Enqueue custom admin notice CSS
function vet_care_center_enqueue_admin_notice_css( $hook ) {
    // Load CSS only on wp-admin pages (not the front end)
    wp_enqueue_style(
        'vet-care-center-admin-notice-style',
        get_template_directory_uri() . '/assets/css/admin-notice.css',
        array(),
        wp_get_theme()->get( 'Version' )
    );
}
add_action( 'admin_enqueue_scripts', 'vet_care_center_enqueue_admin_notice_css' );

// changelog
function vet_care_center_get_changelog_from_readme() {
    $file_path = get_template_directory() . '/readme.txt';

    if (!file_exists($file_path)) {
        return [];
    }

    $content = file_get_contents($file_path);

    // Check if changelog exists
    if (strpos($content, '== Changelog ==') === false) {
        return [];
    }

    // Extract only changelog section
    $changelog = explode('== Changelog ==', $content)[1];

    // Match versions like = 1.0 =
    preg_match_all('/=\s*([\d\.]+)\s*=\s*(.*?)(?=\n=\s*[\d\.]+\s*=|\Z)/s', $changelog, $matches, PREG_SET_ORDER);

    return $matches;
}