<?php
/**
 * General functions.
 *
 * @package Sunset
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

define('SUNSET_MOBILE_BREAKPOINT', 600);
define('SUNSET_TABLET_BREAKPOINT', 1023);
define('SUNSET_MIN_LAYOUT_WIDTH', 320);

if ( ! function_exists( 'sunset_scripts' ) ) {
	add_action( 'wp_enqueue_scripts', 'sunset_scripts' );
	/**
	 * Enqueue scripts and styles
	 */
	function sunset_scripts() {
		if (is_admin()) return true;
		
		global $wp_query; 
		
		$dir_uri = get_template_directory_uri();
		$dir_path = get_template_directory();
		$is_select2_enable = sunset_get_option( 'sunset_style_select2_enable' );
		$is_lightbox_enable = sunset_get_option( 'sunset_style_lightbox_enable' );
		
		wp_enqueue_style( 'sunset-style-normalize', $dir_uri . "/assets/css/normalize.css", false, '8.0.1', 'all' );
		wp_enqueue_style( 'sunset-font-awesome', $dir_uri . "/assets/css/fa/all.css", false, '5.15.4', 'all' );
		wp_enqueue_style( 'sunset-simplebar', $dir_uri . "/assets/css/simplebar.css", false, '5.3.6', 'all' );

		if ($is_select2_enable) {
			wp_enqueue_style( 'sunset-select2-css', $dir_uri . "/assets/css/select2.css", array(), '4.1.0' );
		}
		if ($is_lightbox_enable) {
			wp_enqueue_style( 'sunset-lightbox-css', $dir_uri . "/assets/css/lightbox.css", array(), '2.11.3' );
		}
		
		wp_enqueue_style( 'sunset-style', $dir_uri . "/assets/css/styles.css", array(), SUNSET_VERSION, 'all' );

		if ( is_child_theme() ) {
			wp_enqueue_style( 'sunset-child', get_stylesheet_uri(), array( 'sunset-style' ), filemtime( get_stylesheet_directory() . '/style.css' ), 'all' );
		}

		if ($is_select2_enable) {
			wp_enqueue_script( 'sunset-select2', $dir_uri . "/assets/js/select2.js", array('jquery'), '4.1.0', true );
		}
		if ($is_lightbox_enable) {
			wp_enqueue_script( 'sunset-lightbox', $dir_uri . "/assets/js/lightbox.js", array('jquery'), '2.11.3', true );
		}

		wp_enqueue_script( 'sunset-simplebar', $dir_uri . "/assets/js/simplebar.js", array('jquery'), '5.3.6', true );
		wp_enqueue_script( 'sunset-dragscroll', $dir_uri . "/assets/js/dragscroll.js", array('jquery'), '0.0.8', true );
		wp_enqueue_script( 'sunset-sticky', $dir_uri . "/assets/js/sticky.jquery.js", array('jquery'), '2.1.4', true );
		wp_enqueue_script( 'sunset-critical', $dir_uri . "/assets/js/critical.js", array('jquery'), SUNSET_VERSION, false );
		wp_enqueue_script( 'sunset-main', $dir_uri . "/assets/js/main.js", array('jquery'), SUNSET_VERSION, true );

		wp_localize_script(
			'sunset-main',
			'sunset_params',
			apply_filters(
				'sunset_localize_js_args',
				array(
					'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
					'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
					'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
					'max_page' => $wp_query->max_num_pages,
					'select2_enable' => $is_select2_enable,
					'lightbox_enable' => $is_lightbox_enable,
					'mobile_breakpoint' => SUNSET_MOBILE_BREAKPOINT,
					'animation_speed' => apply_filters('sunset_js_animation_speed', 300),
					'current_page_layout' => sunset_get_layout_type(),
				)
			)
		);

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

if ( ! function_exists( 'sunset_widgets_init' ) ) {
	add_action( 'widgets_init', 'sunset_widgets_init' );
	/**
	 * Register widgetized area and update sidebar with default widgets
	 */
	function sunset_widgets_init() {

		$max_footer_columns = sunset_get_option('sunset_footer_type') === 'footer_1' ? sunset_get_option('sunset_footer_columns') : 4;
		$widgets = array(
			'sidebar-blog' => __( 'Blog Sidebar', 'my-sunset' ),
			'sidebar-post' => __( 'Post Sidebar', 'my-sunset' ),
		);

		for ($col_number = 1; $col_number <= $max_footer_columns; $col_number += 1) {
			$widgets['footer-'.$col_number] = __( 'Footer Widget', 'my-sunset' ).' '.$col_number;
		}

		foreach ( $widgets as $id => $name ) {
			if ( strrpos($id, "footer") === false ) {
				register_sidebar(
					array(
						'name'          => $name,
						'id'            => $id,
						'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
						'after_widget'  => '</aside>',
						'before_title'  => apply_filters( 'sunset_start_widget_title', '<h2 class="widget-title">' ),
						'after_title'   => apply_filters( 'sunset_end_widget_title', '</h2>' ),
					)
				);
			} else {
				register_sidebar(
					array(
						'name'          => $name,
						'id'            => $id,
						'before_widget' => '<aside id="%1$s" class="footer-widget %2$s"><div class="footer-widget__content">',
						'after_widget'  => '</div></aside>',
						'before_title'  => apply_filters( 'sunset_start_widget_title', '<h3 class="footer-widget__title">' ),
						'after_title'   => apply_filters( 'sunset_end_widget_title', '</h3>' ),
					)
				);
			}
		}

		register_sidebar(
			array(
				'name'          => __('Copyright', 'my-sunset' ),
				'id'            => 'copyright',
				'before_widget' => '<aside id="%1$s" class="copyright-widget %2$s">',
				'after_widget'  => '</aside>',
				'before_title'  => apply_filters( 'sunset_start_widget_title', '<h2 class="copyright-widget__title">' ),
				'after_title'   => apply_filters( 'sunset_end_widget_title', '</h2>' ),
			)
		);
		
	}
}