<?php
if (! defined('SAASLAUNCHER_VERSION')) {
	// Replace the version number of the theme on each release.
	define('SAASLAUNCHER_VERSION', wp_get_theme()->get('Version'));
}
define('SAASLAUNCHER_DEBUG', defined('WP_DEBUG') && WP_DEBUG === true);
define('SAASLAUNCHER_DIR', trailingslashit(get_template_directory()));
define('SAASLAUNCHER_URL', trailingslashit(get_template_directory_uri()));

if (! function_exists('saaslauncher_support')) :
	function saaslauncher_support()
	{
		add_theme_support('automatic-feed-links');
		add_theme_support('wp-block-styles');
		add_theme_support('post-thumbnails');
		add_editor_style('style.css');
		remove_theme_support('core-block-patterns');
		load_theme_textdomain('saaslauncher', get_template_directory());
	}
endif;
add_action('after_setup_theme', 'saaslauncher_support');

/*
----------------------------------------------------------------------------------
Enqueue Styles
-----------------------------------------------------------------------------------*/
if (! function_exists('saaslauncher_styles')) :
	function saaslauncher_styles()
	{
		wp_enqueue_style('saaslauncher-style', get_stylesheet_uri(), array(), SAASLAUNCHER_VERSION);
		wp_enqueue_style('saaslauncher-blocks-style', get_template_directory_uri() . '/assets/css/blocks.css');
		wp_enqueue_style('saaslauncher-aos-style', get_template_directory_uri() . '/assets/css/aos.css');
		if (is_rtl()) {
			wp_enqueue_style(
				'saaslauncher-rtl-css',
				get_template_directory_uri() . '/assets/css/rtl.css',
				array(),
				SAASLAUNCHER_VERSION
			);
		}
		wp_enqueue_script('saaslauncher-aos-scripts', get_template_directory_uri() . '/assets/js/aos.js', array('jquery'), SAASLAUNCHER_VERSION, true);
		wp_enqueue_script('saaslauncher-scripts', get_template_directory_uri() . '/assets/js/saaslauncher-scripts.js', array('jquery'), SAASLAUNCHER_VERSION, true);

		// Stacked cards drag-to-dismiss animation for This Just In section
		wp_add_inline_script('saaslauncher-scripts', '
			document.addEventListener("DOMContentLoaded", function () {
				var section = document.querySelector(".this-just-in-section");
				if (!section) return;

				var initialised = [];

				// Target the real WordPress button block by its class
				// The wrapper div is .wp-block-button.cards-reset-btn
				// The actual clickable link inside is .wp-block-button__link
				var resetBtnWrapper = document.querySelector(".wp-block-button.cards-reset-btn");
				var resetBtn = resetBtnWrapper ? resetBtnWrapper.querySelector(".wp-block-button__link") : null;

				function getAllCards() {
					return Array.prototype.slice.call(
						section.querySelectorAll(".wp-block-post-template > li")
					);
				}

				function getCards() {
					return getAllCards().filter(function(c) {
						return !c.classList.contains("swiped-right") && !c.classList.contains("swiped-left");
					});
				}

				function checkEmpty() {
					if (!resetBtnWrapper) return;
					if (getCards().length === 0) {
						resetBtnWrapper.style.display = "block";
					} else {
						resetBtnWrapper.style.display = "none";
					}
				}

				function restack() {
					var cards = getCards();
					cards.forEach(function(card, i) {
						card.style.transition = "";
						card.style.zIndex = cards.length - i;
						card.style.top = (i * 14) + "px";
						card.style.transform = "translateX(-50%) scale(" + (1 - i * 0.03) + ")";
						card.style.pointerEvents = (i === 0) ? "all" : "none";

						if (initialised.indexOf(card) === -1) {
							attachDrag(card);
							initialised.push(card);
						}
					});
					checkEmpty();
				}

				function attachDrag(card) {
					var startX = 0, currentX = 0, dragging = false;

					function start(e) {
						var cards = getCards();
						if (cards.length === 0 || cards[0] !== card) return;
						dragging = true;
						startX = e.touches ? e.touches[0].clientX : e.clientX;
						card.style.transition = "none";
						card.style.cursor = "grabbing";
					}

					function move(e) {
						if (!dragging) return;
						currentX = (e.touches ? e.touches[0].clientX : e.clientX) - startX;
						var rotate = currentX * 0.07;
						card.style.transform = "translateX(calc(-50% + " + currentX + "px)) rotate(" + rotate + "deg)";
					}

					function end() {
						if (!dragging) return;
						dragging = false;
						card.style.cursor = "grab";
						card.style.transition = "";

						if (currentX > 100) {
							card.classList.add("swiped-right");
							setTimeout(restack, 450);
						} else if (currentX < -100) {
							card.classList.add("swiped-left");
							setTimeout(restack, 450);
						} else {
							restack();
						}
						currentX = 0;
					}

					card.addEventListener("mousedown", start);
					card.addEventListener("touchstart", start, { passive: true });
					document.addEventListener("mousemove", move);
					document.addEventListener("touchmove", move, { passive: true });
					document.addEventListener("mouseup", end);
					document.addEventListener("touchend", end);
				}

				// Reset button brings all cards back
				if (resetBtn) {
					resetBtn.addEventListener("click", function (e) {
						e.preventDefault();
						getAllCards().forEach(function(card) {
							card.classList.remove("swiped-right", "swiped-left");
							card.style.transition = "";
							card.style.opacity = "";
						});
						setTimeout(restack, 50);
					});
				}

				restack();
			});
		');
	}
endif;

add_action('wp_enqueue_scripts', 'saaslauncher_styles');

/**
 * Enqueue scripts for admin area
 */
function saaslauncher_admin_style()
{
	if (function_exists('get_current_screen')) {
		$saaslauncher_notice_current_screen = get_current_screen();
	}
	if ((! empty($_GET['page']) && 'about-saaslauncher' === $_GET['page']) || $saaslauncher_notice_current_screen->id === 'themes' || $saaslauncher_notice_current_screen->id === 'dashboard' || $saaslauncher_notice_current_screen->id === 'plugins') {
		wp_enqueue_style('saaslauncher-admin-style', get_template_directory_uri() . '/inc/admin/css/admin-style.css', array(), SAASLAUNCHER_VERSION, 'all');
		wp_enqueue_script('saaslauncher-admin-scripts', get_template_directory_uri() . '/inc/admin/js/saaslauncher-admin-scripts.js', array('jquery'), SAASLAUNCHER_VERSION, true);
		wp_localize_script(
			'saaslauncher-admin-scripts',
			'saaslauncher_admin_localize',
			array(
				'ajax_url'     => admin_url('admin-ajax.php'),
				'nonce'        => wp_create_nonce('saaslauncher_admin_nonce'),
				'welcomeNonce' => wp_create_nonce('saaslauncher_welcome_nonce'),
				'redirect_url' => admin_url('themes.php?page=about-saaslauncher'),
				'scrollURL'    => admin_url('plugins.php?cozy-addons-scroll=true'),
				'demoURL'      => admin_url('themes.php?page=advanced-import'),
			)
		);
	}
}
add_action('admin_enqueue_scripts', 'saaslauncher_admin_style');

/**
 * Enqueue assets scripts for both backend and frontend
 */
function saaslauncher_block_assets()
{
	wp_enqueue_style('saaslauncher-blocks-style', get_template_directory_uri() . '/assets/css/blocks.css');
}
add_action('enqueue_block_assets', 'saaslauncher_block_assets');

/**
 * Load core file.
 */
require_once get_template_directory() . '/inc/core/init.php';

/**
 * Load welcome page file.
 */
require_once get_template_directory() . '/inc/admin/welcome-notice.php';

if (! function_exists('saaslauncher_excerpt_more_postfix')) {
	function saaslauncher_excerpt_more_postfix($more)
	{
		if (is_admin()) {
			return $more;
		}
		return '...';
	}
	add_filter('excerpt_more', 'saaslauncher_excerpt_more_postfix');
}

function saaslauncher_add_woocommerce_support()
{
	add_theme_support('woocommerce');
}
add_action('after_setup_theme', 'saaslauncher_add_woocommerce_support');