<?php

/**
 * Scripts and styles management
 *
 * @package Kubelio\Core
 * @since 1.0.0
 */

namespace Kubelio\Core;

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

/**
 * Scripts Class
 */
class Scripts {

	private $theme_version;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->theme_version = KUBELIO_THEME_VERSION;
		add_action('wp_enqueue_scripts', array($this, 'core_block_scripts'));
		add_action('wp_enqueue_scripts', array($this, 'utility_scripts'), 5);
		add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'), 100);
		add_action('after_setup_theme', array($this, 'enqueue_editor_style'), 99);
		add_filter('should_load_separate_core_block_assets', '__return_true');
	}

	/**
	 * Enable core block any where on classic theme
	 */
	public function core_block_scripts() {
		wp_enqueue_style('wp-block-columns');
		wp_enqueue_style('wp-block-spacer');
		wp_enqueue_style('wp-block-separator');
		wp_enqueue_style('wp-block-social-links');
		wp_enqueue_style('wp-block-group');

		if (is_singular() && comments_open()) {
			wp_enqueue_style('wp-block-comments');
		}
	}

	/**
	 * Utilities Script
	 */
	public function utility_scripts() {
		wp_enqueue_style('kubelio-utilities', get_template_directory_uri() . '/assets/css/utilities.min.css', array(), $this->theme_version);
	}

	/**
	 * Enqueue theme scripts and styles.
	 */
	public function enqueue_scripts() {

		// Layout.
		wp_enqueue_style('kubelio-layout', get_template_directory_uri() . '/assets/css/layouts.min.css', array(), $this->theme_version);

		// Header.
		wp_enqueue_script('kubelio-layout', get_template_directory_uri() . '/assets/js/header.min.js', array(), $this->theme_version, true);

		// Widget.
		wp_enqueue_style('kubelio-widget', get_template_directory_uri() . '/assets/css/widget.min.css', array(), $this->theme_version);

		// Navigation.
		wp_enqueue_style('kubelio-navigation', get_template_directory_uri() . '/assets/css/navigation.min.css', array(), $this->theme_version);
		wp_enqueue_script('kubelio-navigation', get_template_directory_uri() . '/assets/js/navigation.min.js', array(), $this->theme_version, true);

		// Swiper.
		wp_enqueue_style('swiper', get_template_directory_uri() . '/assets/swiper/v12/swiper-bundle.min.css', array(), '12.0.3');
		wp_enqueue_script('swiper', get_template_directory_uri() . '/assets/swiper/v12/swiper-bundle.min.js', array(), '12.0.3', true);

		wp_enqueue_style('kubelio-swiper', get_template_directory_uri() . '/assets/css/kubelio-swiper.min.css', array(), $this->theme_version);
		wp_enqueue_script('kubelio-swiper', get_template_directory_uri() . '/assets/js/kubelio-swiper.min.js', array('swiper'), $this->theme_version, true);

		// Animations.
		wp_enqueue_style('kubelio-animations', get_template_directory_uri() . '/assets/css/animations.min.css', array(), $this->theme_version);

		// Slide panel.
		wp_enqueue_style('kubelio-slidepanel', get_template_directory_uri() . '/assets/css/slidepanel.min.css', array('kubelio-animations'), $this->theme_version);
		wp_enqueue_script('kubelio-slidepanel', get_template_directory_uri() . '/assets/js/slidepanel.min.js', array(), $this->theme_version, true);

		// Modal.
		wp_enqueue_style('kubelio-modal', get_template_directory_uri() . '/assets/css/modal.min.css', array('kubelio-animations'), $this->theme_version);
		wp_enqueue_script('kubelio-modal', get_template_directory_uri() . '/assets/js/modal.min.js', array(), $this->theme_version, true);

		// Post share modal (single posts only).
		if (is_singular()) {
			wp_enqueue_script('kubelio-share', get_template_directory_uri() . '/assets/js/share.min.js', array(), $this->theme_version, true);
		}

		if (get_theme_mod('currency_switcher', false)) {
			wp_enqueue_style('kubelio-dropdown', get_template_directory_uri() . '/assets/css/dropdown.min.css', array(), $this->theme_version);
			wp_enqueue_script('kubelio-dropdown', get_template_directory_uri() . '/assets/js/dropdown.min.js', array(), $this->theme_version, true);
		}

		if (defined('WPCF7_VERSION')) {
			wp_enqueue_style('kubelio-contact-form-7', get_template_directory_uri() . '/assets/css/plugins/contact-form-7.min.css', array(), $this->theme_version);
		}

		if (defined('TUTOR_VERSION')) {
			wp_enqueue_style('kubelio-tutor-lms', get_template_directory_uri() . '/assets/css/plugins/tutor-lms.min.css', array(), $this->theme_version);
		}

		// Comment.
		if (is_singular() && comments_open()) {
			wp_enqueue_script('comment-reply');
			wp_enqueue_style('kubelio-comment', get_template_directory_uri() . '/assets/css/comment.min.css', array(), $this->theme_version);
		}

		// Main style.
		wp_enqueue_style('kubelio', get_template_directory_uri() . '/assets/css/style.min.css', array(), $this->theme_version);
		wp_enqueue_script('kubelio', get_template_directory_uri() . '/assets/js/main.min.js', array(), $this->theme_version, true);
	}

	/**
	 * Add editor styles
	 */
	public function enqueue_editor_style() {
		add_theme_support('editor-styles');
		add_editor_style('assets/css/utilities.min.css');
		add_editor_style('assets/css/style.min.css');
		add_editor_style('assets/css/theme.min.css');
		add_editor_style('assets/css/kubelio-swiper.min.css');
		add_editor_style('assets/css/editor-style.min.css');
	}
}
