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

if ( ! function_exists( 'godhuli_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 godhuli_setup() {
		/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 */
		load_theme_textdomain( 'godhuli', get_template_directory() . '/languages' );

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

		/*
		 * Let WordPress manage the document title.
		 */
		add_theme_support( 'title-tag' );

		/*
		 * Enable support for Post Thumbnails on posts and pages.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */
		add_theme_support( 'post-thumbnails' );

		// Enable custom image size.
		add_image_size( 'godhuli_post_thumb', 1200, 576, true );

		// Enable custom image size for slider.
		add_image_size( 'godhuli_post_slide', 1920, 600, true );

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(
			array(
				'primary' => esc_html__( 'Primary Menu', 'godhuli' ),
			)
		);

		/*
		 * Switch default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		 */
		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
			)
		);

		// Set up the WordPress core custom background feature.
		add_theme_support(
			'custom-background',
			array(
				'default-color'      => 'ffffff',
				'default-image'      => '',
				'default-repeat'     => 'no-repeat',
				'default-position-x' => 'center',
				'default-position-y' => 'center',
				'default-size'       => 'cover',
			)
		);

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/custom-logo/
		 */
		add_theme_support(
			'custom-logo',
			array(
				'height'      => 50,
				'width'       => 250,
				'flex-width'  => true,
				'flex-height' => true,
			)
		);

		// Adding support for core block visual styles.
		add_theme_support( 'wp-block-styles' );

		// Add support for full and wide align images.
		add_theme_support( 'align-wide' );

		// Load regular editor styles into the new block-based editor.
		add_theme_support( 'editor-styles' );

		// Add editor style.
		add_editor_style( array( 'css/godhuli-tokens.css', 'css/editor-style.css', godhuli_fonts_url() ) );

		// Add support for custom color scheme.
		add_theme_support(
			'editor-color-palette',
			array(
				array(
					'name'  => __( 'Strong Blue', 'godhuli' ),
					'slug'  => 'strong-blue',
					'color' => '#0073aa',
				),
				array(
					'name'  => __( 'Lighter Blue', 'godhuli' ),
					'slug'  => 'lighter-blue',
					'color' => '#229fd8',
				),
				array(
					'name'  => __( 'Very Light Gray', 'godhuli' ),
					'slug'  => 'very-light-gray',
					'color' => '#eee',
				),
				array(
					'name'  => __( 'Very Dark Gray', 'godhuli' ),
					'slug'  => 'very-dark-gray',
					'color' => '#444',
				),
			)
		);

		// Add support for responsive embeds.
		add_theme_support( 'responsive-embeds' );
	}
endif;
add_action( 'after_setup_theme', 'godhuli_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function godhuli_content_width() {
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$GLOBALS['content_width'] = apply_filters( 'godhuli_content_width', 1140 );
}
add_action( 'after_setup_theme', 'godhuli_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function godhuli_widgets_init() {
	register_sidebar(
		array(
			'name'          => esc_html__( 'Right Sidebar', 'godhuli' ),
			'id'            => 'godhuli-default-sidebar',
			'description'   => esc_html__( 'Add widgets here.', 'godhuli' ),
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<div class="widget-title">',
			'after_title'   => '</div>',
		)
	);
}
add_action( 'widgets_init', 'godhuli_widgets_init' );


/**
 * Register Google Fonts using the modern API v2 format.
 *
 * @return string Google Fonts URL.
 */
function godhuli_fonts_url() {
	$fonts_url = '';

	/*
	 * Translators: If there are characters in your language that are not
	 * supported by Poppins, translate this to 'off'. Do not translate
	 * into your own language.
	 */
	$poppins = esc_html_x( 'on', 'Poppins font: on or off', 'godhuli' );

	/*
	 * Translators: If there are characters in your language that are not
	 * supported by Lora, translate this to 'off'. Do not translate
	 * into your own language.
	 */
	$lora = esc_html_x( 'on', 'Lora font: on or off', 'godhuli' );

	$font_families = array();

	if ( 'off' !== $poppins ) {
		$font_families[] = 'Poppins:wght@300;400;500;600;700;800';
	}

	if ( 'off' !== $lora ) {
		$font_families[] = 'Lora:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500';
	}

	if ( ! empty( $font_families ) ) {
		$query_args = array(
			'family'  => implode( '&family=', $font_families ),
			'display' => 'swap',
		);
		$fonts_url  = add_query_arg( $query_args, 'https://fonts.googleapis.com/css2' );
	}

	return $fonts_url;
}


/**
 * Handles JavaScript detection.
 * Adds a `js` class to the root `<html>` element when JavaScript is detected.
 */
function godhuli_javascript_detection() {
	echo "<script>(function(html){html.className = html.className.replace(/\\bno-js\\b/,'js')})(document.documentElement);</script>\n"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
}
add_action( 'wp_head', 'godhuli_javascript_detection', 0 );


/**
 * Enqueue scripts and styles.
 */
function godhuli_scripts() {
	$theme_version = wp_get_theme()->get( 'Version' );

	wp_enqueue_style( 'godhuli-fonts', godhuli_fonts_url(), array(), null ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion

	// Design tokens — must load first so CSS variables are available everywhere.
	wp_enqueue_style( 'godhuli-tokens', get_template_directory_uri() . '/css/godhuli-tokens.css', array(), $theme_version, 'all' );

	wp_enqueue_style( 'owl-carousel', get_template_directory_uri() . '/css/owl-carousel.css', array( 'godhuli-tokens' ), '2.3.4', 'all' );

	wp_enqueue_style( 'material-designicons', get_template_directory_uri() . '/css/materialdesignicons.min.css', array(), '7.2.96', 'all' );

	wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '5.3.0', 'all' );

	wp_enqueue_style( 'godhuli-block', get_template_directory_uri() . '/css/blocks.css', array( 'godhuli-tokens' ), $theme_version, 'all' );

	wp_enqueue_style( 'colorbox', get_template_directory_uri() . '/css/colorbox.css', array(), '1.6.4', 'all' );

	wp_enqueue_style( 'select2', get_template_directory_uri() . '/css/select2.min.css', array(), '4.0.13', 'all' );

	wp_enqueue_style( 'godhuli-main-styles', get_template_directory_uri() . '/css/main.css', array( 'bootstrap', 'godhuli-tokens' ), $theme_version, 'all' );

	wp_enqueue_style( 'godhuli-style', get_stylesheet_uri(), array( 'godhuli-main-styles', 'godhuli-tokens' ), $theme_version );

	wp_add_inline_style( 'godhuli-style', godhuli_theme_dynamic_style() );

	wp_enqueue_script( 'popper', get_template_directory_uri() . '/js/umd/popper.min.js', array( 'jquery' ), '2.11.8', true );

	wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery', 'popper' ), '5.3.0', true );

	wp_enqueue_script( 'owl-carousel', get_template_directory_uri() . '/js/owl.carousel.min.js', array( 'jquery' ), '2.3.4', true );

	wp_enqueue_script( 'colorbox', get_template_directory_uri() . '/js/jquery.colorbox.min.js', array( 'jquery' ), '1.6.4', true );

	wp_enqueue_script( 'godhuli-app-js', get_template_directory_uri() . '/js/jquery.app.js', array( 'jquery' ), $theme_version, true );

	wp_enqueue_script( 'responsive-nav', get_template_directory_uri() . '/js/responsive-nav.js', array( 'jquery' ), $theme_version, true );

	wp_enqueue_script( 'godhuli-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), $theme_version, true );

	wp_enqueue_script( 'select2', get_template_directory_uri() . '/js/select2.min.js', array( 'jquery' ), '4.0.13', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'godhuli_scripts' );


/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

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

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
	require get_template_directory() . '/inc/jetpack.php';
}

/**
 * customizer functions
 */
require get_template_directory() . '/inc/customizer-functions.php';

/**
 * bootstrap navwalker
 */
require get_template_directory() . '/inc/class-godhuli-bootstrap-navwalker.php';

/**
 * Additional functions
 */
require get_template_directory() . '/inc/godhuli-additional-functions.php';

/**
 * Include Author Widgets
 */
require get_template_directory() . '/inc/widgets/class-godhuli-aboutme-widget.php';

/**
 * Include Recent Post with thumbnail Widgets
 */
require get_template_directory() . '/inc/widgets/class-godhuli-recentpost-widget.php';

/**
 * Comment Walker
 */
require get_template_directory() . '/inc/class-godhuli-comment.php';

/**
 * Dynamic style
 */
require get_template_directory() . '/inc/dynamic-style.php';
