<?php

namespace SugarCalendarBookings\Support\Helpers;

use Exception;

/**
 * Class with all the misc helper functions that don't belong elsewhere.
 *
 * @since 1.0.0
 */
class Helpers {

	/**
	 * Get the default user agent.
	 *
	 * @since 1.0.0
	 *
	 * @param string $type The type of plugin (lite or pro).
	 *
	 * @return string
	 */
	public static function get_default_user_agent( $type = 'lite' ) {

		return 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) . '; SugarCalendarBookings/' . $type . '-' . SCBOOKINGS_PLUGIN_VERSION;
	}

	/**
	 * Get UTM URL.
	 *
	 * @since 1.0.0
	 *
	 * @param string       $url Base url.
	 * @param array|string $utm Array of UTM params, or if string provided - utm_content URL parameter.
	 *
	 * @return string
	 */
	public static function get_utm_url( $url, $utm ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh

		// Defaults.
		$source   = 'WordPress';
		$medium   = 'plugin-settings';
		$campaign = sugar_calendar_bookings()->is_pro() ? 'plugin' : 'liteplugin';
		$content  = 'general';
		$locale   = get_user_locale();

		if ( is_array( $utm ) ) {
			if ( isset( $utm['source'] ) ) {
				$source = $utm['source'];
			}
			if ( isset( $utm['medium'] ) ) {
				$medium = $utm['medium'];
			}
			if ( isset( $utm['campaign'] ) ) {
				$campaign = $utm['campaign'];
			}
			if ( isset( $utm['content'] ) ) {
				$content = $utm['content'];
			}
			if ( isset( $utm['locale'] ) ) {
				$locale = $utm['locale'];
			}
		} elseif ( is_string( $utm ) ) {
			$content = $utm;
		}

		$query_args = [
			'utm_source'   => esc_attr( rawurlencode( $source ) ),
			'utm_medium'   => esc_attr( rawurlencode( $medium ) ),
			'utm_campaign' => esc_attr( rawurlencode( $campaign ) ),
			'utm_locale'   => esc_attr( sanitize_key( $locale ) ),
		];

		if ( ! empty( $content ) ) {
			$query_args['utm_content'] = esc_attr( rawurlencode( $content ) );
		}

		return add_query_arg( $query_args, $url );
	}

	/**
	 * Import Plugin_Upgrader class from core.
	 *
	 * @since 1.0.0
	 */
	public static function include_plugin_upgrader() {

		/** \WP_Upgrader class */
		require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

		/** \Plugin_Upgrader class */
		require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
	}

	/**
	 * Generate a random hex color code.
	 *
	 * @since 1.0.0
	 *
	 * @return string Random hex color code with leading #.
	 */
	public static function generate_random_hex_color() {

		return sprintf( '#%06X', wp_rand( 0, 0xFFFFFF ) );
	}

	/**
	 * Get the support URL based on plugin edition.
	 *
	 * @since 1.3.1
	 *
	 * @param array<string, string> $args Optional query args to append to the URL.
	 *
	 * @return string
	 */
	public static function get_support_url( array $args = [] ): string {

		if ( sugar_calendar_bookings()->is_pro() ) {
			$url = 'https://sugarcalendar.com/account/support/';

			return ! empty( $args ) ? add_query_arg( $args, $url ) : $url;
		}

		return 'https://wordpress.org/support/plugin/sugar-calendar-bookings-scheduling-appointments-lite/';
	}

	/**
	 * Check whether an array is associative or indexed.
	 *
	 * @since 1.0.0
	 *
	 * @param array $value Whether the array is associative.
	 *
	 * @return bool
	 */
	public static function is_associative( $value ) {

		if ( ! is_array( $value ) || empty( $value ) ) {
			return false;
		}

		return array_keys( $value ) !== range( 0, count( $value ) - 1 );
	}

	/**
	 * Include a template file or return it as string.
	 *
	 * @since 1.0.0
	 *
	 * @param string $template The template file.
	 * @param array  $vars     Template variables array.
	 * @param bool   $return   Whether to return contents as string.
	 *
	 * @throws Exception Throw an exception if template is not found.
	 *
	 * @return void|string
	 */
	public static function template( $template, array $vars = [], $return = false ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.returnFound

		$template_path = SCBOOKINGS_PLUGIN_PATH . 'templates/' . ltrim( $template, '/' );

		if ( ! file_exists( $template_path ) ) {
			throw new Exception( 'Template not found: ' . esc_html( $template_path ) );
		}

		// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
		extract( $vars, EXTR_SKIP );

		if ( $return ) {
			ob_start();
			include $template_path;

			return ob_get_clean();
		} else {
			include $template_path;
		}
	}
}
