<?php
/**
 * BPO Services functions and definitions
 *
 * @package BPO Services
 */

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

	global $content_width;

	if ( ! isset( $content_width ) ) {
		$content_width = 640;
	}

	// Theme version
	if ( ! defined( 'BPO_SERVICES_VERSION' ) ) {
		define( 'BPO_SERVICES_VERSION', wp_get_theme()->get( 'Version' ) );
	}

	// Load text domain
	load_theme_textdomain( 'bpo-services', get_template_directory() . '/languages' );

	// Theme supports
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );

	add_theme_support( 'custom-logo', array(
		'height'      => 240,
		'width'       => 240,
		'flex-height' => true,
	) );

	add_theme_support( 'custom-background', array(
		'default-color' => 'ffffff',
	) );

	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'responsive-embeds' );

	// WooCommerce
	add_theme_support( 'woocommerce' );

	// Editor styles
	add_editor_style( array( 'assets/css/editor-style.css' ) );

	require get_parent_theme_file_path( '/inc/dashboard/dashboard.php' );

	require get_parent_theme_file_path( '/inc/admin/theme-upsell.php' );

    require get_parent_theme_file_path( '/inc/customizer/customizer.php' );
}
endif;
add_action( 'after_setup_theme', 'bpo_services_setup' );

/**
 * Enqueue scripts and styles
 */
function bpo_services_scripts() {

	$bpo_services_version = BPO_SERVICES_VERSION;

	// Main stylesheet
	wp_enqueue_style(
		'bpo-services-basic-style',
		get_stylesheet_uri(),
		array(),
		$bpo_services_version
	);

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

	// Font Awesome
	wp_enqueue_style(
		'fontawesome',
		get_template_directory_uri() . '/assets/font-awesome/css/all.css',
		array(),
		'7.0.0'
	);

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

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

	// Owl Carousel JS
	wp_enqueue_script(
		'owl-carousel-js',
		get_template_directory_uri() . '/assets/js/owl-carousel.js',
		array( 'jquery' ),
		$bpo_services_version,
		true
	);

	// Main Script
	wp_enqueue_script(
		'bpo-services-main-script',
		get_template_directory_uri() . '/assets/js/script.js',
		array( 'jquery', 'wow-js' ),
		$bpo_services_version,
		true
	);

	// RTL support
	wp_style_add_data( 'bpo-services-basic-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'bpo_services_scripts' );

function bpo_services_enqueue_admin_script($hook) {
    // Enqueue admin JS for notices
    wp_enqueue_script('bpo-services-welcome-notice', get_template_directory_uri() . '/inc/dashboard/bpo-services-welcome-notice.js', array('jquery'), '', true);
    
    // Localize script to pass data to JavaScript
    wp_localize_script('bpo-services-welcome-notice', 'bpo_services_localize', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('bpo_services_welcome_nonce'),
        'dismiss_nonce' => wp_create_nonce('bpo_services_welcome_nonce'), // Nonce for dismissal
        'redirect_url' => admin_url('themes.php?page=bpo-services-guide-page')
    ));
}
add_action('admin_enqueue_scripts', 'bpo_services_enqueue_admin_script');

function bpo_services_admin_theme_style() {
   wp_enqueue_style('bpo-services-custom-admin-style', esc_url(get_template_directory_uri()) . '/inc/dashboard/dashboard.css');
   wp_enqueue_style('bpo-services-admin-upsell-style', esc_url(get_template_directory_uri()) . '/assets/css/admin-style.css');
}
add_action('admin_enqueue_scripts', 'bpo_services_admin_theme_style');

add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );


// Block Patterns.
require get_template_directory() . '/block-patterns.php';
require get_template_directory() . '/custom-setting.php';
require_once get_template_directory() . '/inc/dashboard/welcome-notice.php';

/**
 * Redirect to Pro upsell page after theme activation
 */

add_action('after_switch_theme', 'bpo_services_redirect_after_activation');
function bpo_services_redirect_after_activation() {
    if ( ! get_option( 'bpo_services_activation_redirect', false ) ) {
        update_option( 'bpo_services_activation_redirect', true );
        wp_safe_redirect( admin_url( 'themes.php?page=bpo-services-pro' ) );
        exit;
    }
}

add_action( 'admin_bar_menu', 'bpo_services_add_upgrade_button', 100 );

function bpo_services_add_upgrade_button( $bpo_services_wp_admin_bar ) {

    $bpo_services_theme_name = wp_get_theme()->get( 'Name' );

    $bpo_services_args = array(
        'id'    => 'bpo_services_upgrade',
        'title' => '<span style="color:#fff;font-weight:600;">
            🚀 Upgrade to ' . esc_html( $bpo_services_theme_name ) . ' Pro - 20% OFF 
            <span style="background:#ff5722;color:#fff;padding:2px 8px;border-radius:3px;margin-left:6px;">
                Buy Now
            </span>
        </span>',
        'href'  => esc_url( BPO_SERVICES_BUY_PRO ),
        'meta'  => array(
            'class'  => 'bpo-services-upgrade-btn',
            'title'  => 'Upgrade to Pro',
            'target' => '_blank'
        )
    );

    $bpo_services_wp_admin_bar->add_node( $bpo_services_args );
}