<?php
/**
 * EasySite Pro Theme Functions
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Product tier: set to true for Ultimate Bundle, false for Basic theme.
 * Distributors can override this in wp-config.php before theme loads.
 */
if ( ! defined( 'EASYSITE_BUNDLE' ) ) {
	define( 'EASYSITE_BUNDLE', false );
}

define( 'EASYSITE_PRO_VERSION', '1.7.0' );
define( 'EASYSITE_PRO_DIR', get_template_directory() );
define( 'EASYSITE_PRO_URI', get_template_directory_uri() );

/**
 * Check if Ultimate Bundle features are available.
 *
 * @return bool
 */
function easysite_is_bundle() {
	return (bool) apply_filters( 'easysite_is_bundle', EASYSITE_BUNDLE );
}

/**
 * Theme setup.
 */
function easysite_pro_setup() {
	load_theme_textdomain( 'easysite-pro', EASYSITE_PRO_DIR . '/languages' );

	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'custom-logo', array(
		'height'      => 120,
		'width'       => 400,
		'flex-height' => true,
		'flex-width'  => true,
	) );
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
		'style',
		'script',
	) );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'woocommerce' );
	add_theme_support( 'wc-product-gallery-zoom' );
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );

	register_nav_menus( array(
		'primary' => __( 'Primary Menu', 'easysite-pro' ),
		'footer'  => __( 'Footer Menu', 'easysite-pro' ),
	) );

	set_post_thumbnail_size( 1200, 675, true );
	add_image_size( 'easysite-hero', 1920, 1080, true );
	add_image_size( 'easysite-card', 600, 400, true );
	add_image_size( 'easysite-team', 400, 400, true );
}
add_action( 'after_setup_theme', 'easysite_pro_setup' );

/**
 * Register widget areas.
 */
function easysite_pro_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Main Sidebar', 'easysite-pro' ),
		'id'            => 'sidebar-main',
		'description'   => __( 'Widgets shown on blog and default pages.', 'easysite-pro' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer Widgets', 'easysite-pro' ),
		'id'            => 'sidebar-footer',
		'description'   => __( 'Widgets shown in the footer area.', 'easysite-pro' ),
		'before_widget' => '<div id="%1$s" class="footer-widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h4 class="footer-widget-title">',
		'after_title'   => '</h4>',
	) );
}
add_action( 'widgets_init', 'easysite_pro_widgets_init' );

/**
 * Include theme modules.
 */
$easysite_includes = array(
	'helpers.php',
	'branding.php',
	'gradients.php',
	'customizer.php',
	'onboarding.php',
	'page-builder.php',
	'section-design.php',
	'visual-editor.php',
	've-extras.php',
	'site-settings.php',
	'templates.php',
	'seo.php',
	'performance.php',
	'sections.php',
	'forms.php',
	'woocommerce.php',
	'ai-assistant.php',
	'licensing.php',
);

if ( easysite_is_bundle() ) {
	$easysite_includes = array_merge( $easysite_includes, array(
		'client-mode.php',
		'client-help.php',
		'section-library.php',
		'site-kit.php',
		'demo-kits.php',
		'section-presets.php',
		'shop-starter.php',
		'mini-cart.php',
		'commerce.php',
		'updater.php',
		'testimonials.php',
		'header-footer-builder.php',
		'mega-menu.php',
		'popup-builder.php',
		'white-label.php',
	) );
}

foreach ( $easysite_includes as $file ) {
	$path = EASYSITE_PRO_DIR . '/inc/' . $file;
	if ( file_exists( $path ) ) {
		require_once $path;
	}
}

require_once EASYSITE_PRO_DIR . '/inc/fallback-menu.php';

/**
 * Enqueue frontend assets.
 */
function easysite_pro_enqueue_assets() {
	wp_enqueue_style(
		'easysite-pro-main',
		EASYSITE_PRO_URI . '/assets/css/main.css',
		array(),
		EASYSITE_PRO_VERSION
	);

	wp_enqueue_script(
		'easysite-pro-main',
		EASYSITE_PRO_URI . '/assets/js/main.js',
		array(),
		EASYSITE_PRO_VERSION,
		true
	);

	if ( easysite_is_bundle() ) {
		wp_enqueue_style(
			'easysite-pro-builder',
			EASYSITE_PRO_URI . '/assets/css/builder.css',
			array( 'easysite-pro-main' ),
			EASYSITE_PRO_VERSION
		);

		wp_enqueue_script(
			'easysite-pro-popup',
			EASYSITE_PRO_URI . '/assets/js/popup.js',
			array( 'easysite-pro-main' ),
			EASYSITE_PRO_VERSION,
			true
		);

		wp_enqueue_script(
			'easysite-pro-mega-menu',
			EASYSITE_PRO_URI . '/assets/js/mega-menu.js',
			array( 'easysite-pro-main' ),
			EASYSITE_PRO_VERSION,
			true
		);
	}

	wp_localize_script( 'easysite-pro-main', 'easysitePro', array(
		'ajaxUrl' => admin_url( 'admin-ajax.php' ),
		'nonce'   => wp_create_nonce( 'easysite_pro_nonce' ),
	) );

	easysite_output_branding_css();
}
add_action( 'wp_enqueue_scripts', 'easysite_pro_enqueue_assets' );

/**
 * Enqueue admin assets.
 */
function easysite_pro_admin_assets( $hook ) {
	wp_enqueue_style(
		'easysite-pro-admin',
		EASYSITE_PRO_URI . '/assets/css/admin.css',
		array(),
		EASYSITE_PRO_VERSION
	);

	wp_enqueue_script(
		'easysite-pro-admin',
		EASYSITE_PRO_URI . '/assets/js/admin.js',
		array( 'jquery' ),
		EASYSITE_PRO_VERSION,
		true
	);

	$admin_scripts = array();

	if ( easysite_is_bundle() ) {
		$admin_scripts['easysite-pro-builder']   = 'builder.js';
		$admin_scripts['easysite-pro-ai-helper'] = 'ai-helper.js';
	}

	if ( in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
		wp_enqueue_script( 'jquery-ui-sortable' );
	}

	foreach ( $admin_scripts as $handle => $file ) {
		wp_enqueue_script(
			$handle,
			EASYSITE_PRO_URI . '/assets/js/' . $file,
			array( 'jquery', 'easysite-pro-admin' ),
			EASYSITE_PRO_VERSION,
			true
		);
	}

	wp_localize_script( 'easysite-pro-admin', 'easysiteAdmin', array(
		'bundle'  => easysite_is_bundle(),
		'ajaxUrl' => admin_url( 'admin-ajax.php' ),
		'nonce'   => wp_create_nonce( 'easysite_admin_nonce' ),
		'i18n'    => array(
			'saving'   => __( 'Saving…', 'easysite-pro' ),
			'saved'    => __( 'Saved!', 'easysite-pro' ),
			'error'    => __( 'Something went wrong. Please try again.', 'easysite-pro' ),
		),
	) );
}
add_action( 'admin_enqueue_scripts', 'easysite_pro_admin_assets' );

/**
 * Body classes for layout and presets.
 */
function easysite_pro_body_classes( $classes ) {
	$layout       = get_theme_mod( 'easysite_layout_style', 'full-width' );
	$header_style = get_theme_mod( 'easysite_header_style', 'logo-left' );
	$preset       = get_theme_mod( 'easysite_design_preset', 'modern' );
	$sticky       = get_theme_mod( 'easysite_sticky_header', true );

	$classes[] = 'easysite-layout-' . sanitize_html_class( $layout );
	$classes[] = 'easysite-header-' . sanitize_html_class( $header_style );
	$classes[] = 'easysite-preset-' . sanitize_html_class( $preset );

	if ( $sticky ) {
		$classes[] = 'easysite-sticky-header';
	}

	if ( easysite_is_bundle() ) {
		$classes[] = 'easysite-bundle';
	}

	$btn_style = get_theme_mod( 'easysite_button_style', 'pill' );
	$spacing   = get_theme_mod( 'easysite_section_spacing', 'normal' );
	$classes[] = 'easysite-btn-style-' . sanitize_html_class( $btn_style );
	$classes[] = 'easysite-spacing-' . sanitize_html_class( $spacing );

	if ( easysite_get_mod( 'easysite_animations', true ) ) {
		$classes[] = 'easysite-animations-on';
	}

	if ( easysite_get_mod( 'easysite_use_gradients', true ) ) {
		$classes[] = 'easysite-gradients-on';
	}

	if ( easysite_get_mod( 'easysite_gradient_buttons', true ) ) {
		$classes[] = 'easysite-gradient-buttons';
	}

	if ( easysite_get_mod( 'easysite_gradient_hero', true ) ) {
		$classes[] = 'easysite-gradient-hero';
	}

	if ( is_front_page() && easysite_get_mod( 'easysite_header_transparent', false ) ) {
		$classes[] = 'easysite-header-transparent';
	}

	return $classes;
}
add_filter( 'body_class', 'easysite_pro_body_classes' );
