<?php

namespace EasyCriticalCSS;

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

class Settings {
	/**
	 * Gets a global setting from the database.
	 *
	 * @param string $setting_name Name of the setting.
	 * @param mixed  $default_value Default value if the setting is not found.
	 *
	 * @return mixed Setting value.
	 */
	public static function get_global_setting( $setting_name, $default_value = '' ) {
		return get_option( "easy_cc_$setting_name", $default_value );
	}

	/**
	 * Gets an individual setting from the ECC table.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 * @param string $setting_name Name of the setting.
	 * @param mixed  $default_value Default value if the setting is not found.
	 *
	 * @return mixed Setting value.
	 */
	public static function get_individual_setting( $identifier, $setting_name, $default_value = '' ) {
		$settings = Individual_Settings::get_settings( $identifier );

		return isset( $settings[ $setting_name ] ) ? $settings[ $setting_name ] : $default_value;
	}

	public static function activate_critical_compatibility( $setting ) {
		// If WP Rocket is installed, Critical is paused
		if ( Compatibility_WP_Rocket::is_rocket_critical_activated() ) {
			return false;
		}

		// If Trellis is installed, Critical is paused
		if ( Compatibility_Trellis::is_trellis_critical_activated() ) {
			return false;
		}

		// If Perfmatters conflicting CSS settings are active, Critical is paused
		if ( Compatibility_Perfmatters::has_conflicting_css_settings() ) {
			return false;
		}

		// If Jetpack Boost is installed, Critical is paused
		if ( Compatibility_Jetpack_Boost::is_jetpack_boost_installed() ) {
			return false;
		}

		return (bool) $setting;
	}

	/**
	 * Gets the individual Activate Critical CSS setting.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return bool
	 */
	public static function get_individual_activate_critical( $identifier ) {
		return self::activate_critical_compatibility( self::get_individual_setting( $identifier, 'activate_critical_css', true ) );
	}

	/**
	 * Gets the global Critical CSS Mode.
	 *
	 * @return string
	 */
	public static function get_global_mode() {
		$mode = (string) self::get_global_setting( 'critical_css_mode', 'auto' );

		return $mode;
	}

	/**
	 * Gets the individual Critical CSS Mode.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return string
	 */
	public static function get_individual_mode( $identifier ) {
		$global  = self::get_global_mode();
		$setting = (string) self::get_individual_setting( $identifier, 'critical_css_mode', $global );

		// 'default' must be global.
		return ( $setting === 'default' ) ? $global : $setting;
	}

	/**
	 * Gets the global Secondary CSS Bahavior.
	 *
	 * @return string
	 */
	public static function get_global_secondary_behavior() {
		return (string) self::get_global_setting( 'secondary_css_behavior', 'secondary' );
	}

	/**
	 * Gets the individual Secondary CSS Bahavior.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return string
	 */
	public static function get_individual_secondary_behavior( $identifier ) {
		$global  = self::get_global_secondary_behavior();
		$setting = (string) self::get_individual_setting( $identifier, 'secondary_css_behavior', $global );

		// 'default' must be global.
		return ( $setting === 'default' ) ? $global : $setting;
	}

	/**
	 * Gets the global Forced Critical CSS Selectors.
	 *
	 * @return array
	 */
	public static function get_global_forced_critical() {
		$selectors = self::get_global_setting( 'forced_critical_selectors', '' );

		return Helpers::parse_newline_list( $selectors );
	}

	/**
	 * Gets the individual Forced Critical CSS Selectors.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return array
	 */
	public static function get_individual_forced_critical( $identifier ) {
		$global    = self::get_global_forced_critical();
		$selectors = (string) self::get_individual_setting( $identifier, 'forced_critical_selectors', implode( "\n", $global ) );

		return Helpers::parse_newline_list( $selectors );
	}

	/**
	 * Gets the global Add Common Secondary Selectors.
	 *
	 * @return bool
	 */
	public static function get_global_add_common_secondary() {
		return (bool) self::get_global_setting( 'add_common_secondary', true );
	}

	/**
	 * Gets the common Secondary CSS Selectors.
	 *
	 * @return array
	 */
	public static function add_common_secondary_selectors( $selectors ) {
		// Return what we have if Common Selector setting is off.
		if ( self::get_global_add_common_secondary() ) {
			$common_secondary_selectors = [
				'.active',
				'.lazyloaded',
			];

			/**
			 * Filters the list of common Secondary CSS Selectors.
			 *
			 * @since 1.4.2
			 *
			 * @param array $common_secondary_selectors List of common Secondary CSS Selectors.
			 */
			$common_secondary_selectors = apply_filters( 'easy_cc_common_secondary_selectors', $common_secondary_selectors );
			foreach ( $common_secondary_selectors as $selector ) {
				$selectors .= "\n" . $selector;
			}
		}

		return $selectors;
	}

	/**
	 * Gets the global Forced Secondary CSS Selectors.
	 *
	 * @return array
	 */
	public static function get_global_forced_secondary() {
		$selectors = self::get_global_setting( 'forced_secondary_selectors', '' );
		$selectors = self::add_common_secondary_selectors( $selectors );

		return Helpers::parse_newline_list( $selectors );
	}

	/**
	 * Gets the individual Forced Secondary CSS Selectors.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return array
	 */
	public static function get_individual_forced_secondary( $identifier ) {
		$global    = self::get_global_forced_secondary();
		$selectors = (string) self::get_individual_setting( $identifier, 'forced_secondary_selectors', implode( "\n", $global ) );
		$selectors = self::add_common_secondary_selectors( $selectors );

		return Helpers::parse_newline_list( $selectors );
	}

	/**
	 * Gets the global Default Critical CSS.
	 *
	 * @return string
	 */
	public static function get_global_default_critical_css() {
		return (string) self::get_global_setting( 'default_critical_css', '' );
	}

	/**
	 * Gets the individual Default Critical CSS.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return string
	 */
	public static function get_individual_default_critical_css( $identifier ) {
		$global = self::get_global_default_critical_css();

		return (string) self::get_individual_setting( $identifier, 'critical_css', $global );
	}

	/**
	 * Gets the individual Manual Critical CSS.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return string
	 */
	public static function get_individual_manual_secondary_css( $identifier ) {
		return (string) self::get_individual_setting( $identifier, 'manual_secondary_css', '' );
	}

	/**
	 * Gets the global Excluded CSS files.
	 *
	 * @return array
	 */
	public static function get_global_excluded_css_files() {
		$files = self::get_global_setting( 'exclude_css_files', '' );

		return Helpers::parse_newline_list( $files );
	}

	/**
	 * Gets the individual Excluded CSS files.
	 *
	 * @param string|int $identifier URL, Post ID, or URL hash.
	 *
	 * @return array
	 */
	public static function get_individual_excluded_css_files( $identifier ) {
		$global = self::get_global_excluded_css_files();
		$files  = (string) self::get_individual_setting( $identifier, 'exclude_css_files', implode( "\n", $global ) );

		return Helpers::parse_newline_list( $files );
	}

	/**
	 * Gets the global Serve Critical CSS from Files setting.
	 *
	 * @return bool
	 */
	public static function get_global_serve_css_from_files() {
		return (bool) self::get_global_setting( 'serve_css_from_files', true );
	}

	/**
	 * Gets the global Ignore Cross-Domain CSS setting.
	 *
	 * @return bool
	 */
	public static function get_global_ignore_cross_domain_css() {
		return (bool) self::get_global_setting( 'ignore_cross_domain_css', 1 );
	}

	/**
	 * Gets the global Auto Generate Critical CSS Triggers setting.
	 *
	 * @return array
	 */
	public static function get_global_regenerate_triggers() {
		$default  = [
			'post_updated'      => 1,
			'archive_updated'   => 1,
			'plugin_change'     => 1,
			'theme_change'      => 1,
			'upgrader_complete' => 1,
		];
		$triggers = self::get_global_setting( 'regenerate_triggers', $default );

		return array_keys( is_array( $triggers ) ? $triggers : $default );
	}

	/**
	 * Gets the global Expired Critical CSS Bahavior.
	 *
	 * @return string
	 */
	public static function get_global_expired_css_behavior() {
		return (string) self::get_global_setting( 'expired_css_behavior', 'use_expired' );
	}

	/**
	 * Gets the global Debug Mode setting.
	 *
	 * @return bool
	 */
	public static function get_global_debug_mode() {
		return (bool) self::get_global_setting( 'debug_mode', false );
	}

	/**
	 * Gets the global Cloudflare Email setting.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public static function get_global_cloudflare_email() {
		if ( defined( 'CLOUDFLARE_EMAIL' ) ) {
			return (string) CLOUDFLARE_EMAIL;
		}
		return (string) self::get_global_setting( 'cloudflare_email', '' );
	}

	/**
	 * Gets the global Cloudflare API Key setting.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public static function get_global_cloudflare_api_key() {
		if ( defined( 'CLOUDFLARE_API_KEY' ) ) {
			return (string) CLOUDFLARE_API_KEY;
		}
		return (string) self::get_global_setting( 'cloudflare_api_key', '' );
	}

	/**
	 * Gets the global Cloudflare Zone ID setting.
	 *
	 * @since 1.4.0
	 * @return string
	 */
	public static function get_global_cloudflare_zone_id() {
		if ( defined( 'CLOUDFLARE_ZONE_ID' ) ) {
			return (string) CLOUDFLARE_ZONE_ID;
		}
		return (string) self::get_global_setting( 'cloudflare_zone_id', '' );
	}
}
