<?php
namespace Prodigy\Includes\Helpers;

use Prodigy\Includes\Content\Prodigy_Settings_Parser;
use Prodigy\Includes\Prodigy_User;

/**
 * Prodigy Platform Settings
 *
 * @version    3.2.0
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Platform_Settings {

	public const LOGIN_MODE_OPTIONAL = 'optional';


	/** @var array  */
	public static array $price_option = array(
		'price_range' => true,
		'min_price'   => false,
	);

		/**
		 * @return bool
		 */
	public static function is_enable_logo_app(): bool {
		return get_option( Prodigy_Settings_Parser::APP_LOGO_MANAGEMENT );
	}

	/**
	 * @return bool
	 */
	public static function is_enable_subscription_app(): bool {
		return (bool) get_option( Prodigy_Settings_Parser::APP_SUBSCRIPTION_FEATURE );
	}

	/**
	 * @return bool
	 */
	public static function is_enable_brands_app(): bool {
		return (bool) get_option( Prodigy_Settings_Parser::APP_BRANDS );
	}

	/**
	 * @return bool
	 */
	public static function is_enable_personalization_app(): bool {
		return (bool) get_option( Prodigy_Settings_Parser::APP_PERSONALISATION_FEATURE );
	}

	/**
	 * @return bool
	 */
	public static function is_enable_gift_cards_app(): bool {
		return (bool) get_option( Prodigy_Settings_Parser::APP_GIFT_CARDS_APP );
	}

	/**
	 * @return false|mixed|null
	 */
	public static function is_enable_google_analytics_app(): bool {
		return get_option( 'pg_enable_google_analytics' ) && get_option( Prodigy_Settings_Parser::APP_GOOGLE_ANALYTICS );
	}

	/**
	 * @return bool
	 */
	public static function is_product_page_price_range_settings(): bool {
		$price_option = get_option( Prodigy_Settings_Parser::PRODUCT_PAGE_PRICE_SETTINGS );

		return self::$price_option[ $price_option ] ?? true;
	}

	/**
	 * @return false|mixed|null
	 */
	public static function get_google_analytics_app_code() {
		return get_option( Prodigy_Settings_Parser::APP_GOOGLE_ANALYTICS . '_key' );
	}

	/**
	 * @return bool
	 */
	public static function is_redemption_store_status(): bool {
		return ! get_option( Prodigy_Settings_Parser::APP_REDEMPTION_STORE );
	}

	/**
	 * @return string
	 */
	public static function get_thank_you_page_title(): string {
		return (string) ( get_option( Prodigy_Settings_Parser::THANK_YOU_PAGE_TITLE ) ?: 'Thank you for your order' );
	}

	/**
	 * @return string Raw title from API settings, empty string if not set.
	 */
	public static function get_thank_you_page_title_from_api(): string {
		return (string) get_option( Prodigy_Settings_Parser::THANK_YOU_PAGE_TITLE );
	}

	/**
	 * @return string
	 */
	public static function get_thank_you_page_body(): string {
		return (string) ( get_option( Prodigy_Settings_Parser::THANK_YOU_PAGE_BODY ) ?: 'Order [order number] was successfully placed.' );
	}

	/**
	 * @return bool
	 */
	public static function is_thank_you_page_back_btn_enabled(): bool {
		$value = get_option( Prodigy_Settings_Parser::THANK_YOU_PAGE_ENABLE_BACK_BTN, null );
		if ( $value === null || $value === '' ) {
			return true;
		}
		return (bool) $value;
	}

	/**
	 * @return string
	 */
	public static function get_thank_you_page_button_text(): string {
		return (string) ( get_option( Prodigy_Settings_Parser::THANK_YOU_PAGE_BUTTON_TEXT ) ?: 'BACK TO SHOP LIST' );
	}

	/**
	 * @return array
	 */
	public static function get_error_settings( string $error_type ): array {
		$error_settings = array(
			'access_rights'  => array(
				'number'         => Prodigy_Settings_Parser::ERROR_ACCESS_RIGHTS_NUMBER,
				'number_default' => '403',
				'title'          => Prodigy_Settings_Parser::ERROR_ACCESS_RIGHTS_TITLE,
				'title_default'  => "You don't have permission",
				'body'           => Prodigy_Settings_Parser::ERROR_ACCESS_RIGHTS_BODY,
			),
			'page_not_found' => array(
				'number'         => Prodigy_Settings_Parser::ERROR_PAGE_NOT_FOUND_NUMBER,
				'number_default' => '404',
				'title'          => Prodigy_Settings_Parser::ERROR_PAGE_NOT_FOUND_TITLE,
				'title_default'  => 'OOPS! Could not find it…',
				'body'           => Prodigy_Settings_Parser::ERROR_PAGE_NOT_FOUND_BODY,
			),
		);

		$options = $error_settings[ $error_type ] ?? $error_settings['access_rights'];

		return array(
			'error_number' => (string) ( get_option( $options['number'] ) ?: $options['number_default'] ),
			'error_title'  => (string) ( get_option( $options['title'] ) ?: $options['title_default'] ),
			'error_body'   => (string) ( get_option( $options['body'] ) ?: '' ),
		);
	}

	/**
	 * @return bool
	 */
	public static function is_enable_login_option(): bool {
		$login_option = get_option( Prodigy_Settings_Parser::REQUIRE_LOGIN_OPTION, self::LOGIN_MODE_OPTIONAL );

		if (
			$login_option === Prodigy_User::REQUIRE_LOGIN_DISABLED_VALUE ||
			$login_option === Prodigy_User::REQUIRE_LOGIN_OPTIONAL_VALUE
		) {
			return false;
		}

		if ( $login_option === Prodigy_User::REQUIRE_LOGIN_VALUE ) {
			return true;
		}

		return false;
	}
}
