<?php
/**
 * Admin Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Admin;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Customiser_Object as Customiser_Object,
	Errigal_WP_Page as WP_Page,
};
use Errigal\Shared\Errigal_Validator as Validator;

/**
 * This class is in charge of providing admin settings and functions.
 *
 * @since 1.1.0
 */
class Errigal_Admin {
	/**
	 * Post: Nonce.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const POST_NONCE = 'nonce';

	/**
	 * Get current page.
	 *
	 * @since 1.1.0
	 *
	 * @global string $pagenow Filename of current screen.
	 *
	 * @return string
	 */
	public static function get_current_page() {
		global $pagenow;

		if (
			! isset( $pagenow ) ||
			! Validator::is_non_empty_string( $pagenow )
		) {
			return '';
		}

		return $pagenow;
	}

	/**
	 * Gets new post URL.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	public static function get_new_post_url() {
		return esc_url( admin_url( WP_Page::POST_NEW ) );
	}

	/**
	 * Gets customiser URL.
	 *
	 * @since 1.1.0
	 *
	 * @param string $customiser_object Customiser object.
	 * @param string $customiser_id     Customiser ID.
	 * @return string
	 */
	public static function get_customiser_url( $customiser_object, $customiser_id ) {
		if (
			! self::is_supported_customiser_object( $customiser_object ) ||
			! Validator::is_non_empty_string( $customiser_id )
		) {
			return '';
		}

		return esc_url(
			admin_url(
				sprintf(
					'customize.php?autofocus[%s]=%s',
					$customiser_object,
					$customiser_id
				)
			)
		);
	}

	/**
	 * Gets user meta.
	 *
	 * @since 1.1.0
	 *
	 * @param string $key Metadata key.
	 * @return string
	 */
	public static function get_user_meta( $key ) {
		if ( ! Validator::is_non_empty_string( $key ) ) {
			return '';
		}

		$current_user_id = get_current_user_id();

		if ( ! self::is_valid_current_user_id( $current_user_id ) ) {
			return '';
		}

		return get_user_meta(
			$current_user_id,
			sanitize_key( $key ),
			true
		);
	}

	/**
	 * Deletes user meta.
	 *
	 * @since 1.1.0
	 *
	 * @param string $key Metadata key.
	 * @return bool
	 */
	public static function delete_user_meta( $key ) {
		if ( ! Validator::is_non_empty_string( $key ) ) {
			return false;
		}

		$current_user_id = get_current_user_id();

		if ( ! self::is_valid_current_user_id( $current_user_id ) ) {
			return false;
		}

		return delete_user_meta(
			$current_user_id,
			sanitize_key( $key )
		);
	}

	/**
	 * Updates user meta.
	 *
	 * @since 1.1.0
	 *
	 * @param string $key   Metadata key.
	 * @param string $value Metadata value.
	 * @return int|bool
	 */
	public static function update_user_meta( $key, $value ) {
		if (
			! Validator::is_non_empty_string( $key ) ||
			! Validator::is_non_empty_string( $value )
		) {
			return false;
		}

		$current_user_id = get_current_user_id();

		if ( ! self::is_valid_current_user_id( $current_user_id ) ) {
			return false;
		}

		return update_user_meta(
			$current_user_id,
			sanitize_key( $key ),
			sanitize_text_field( $value )
		);
	}

	/**
	 * Creates nonce.
	 *
	 * @since 1.1.0
	 *
	 * @param string $action Scalar value to add context to nonce.
	 * @return string
	 */
	public static function create_nonce( $action ) {
		if ( ! Validator::is_non_empty_string( $action ) ) {
			return '';
		}

		return esc_js( wp_create_nonce( sanitize_key( $action ) ) );
	}

	/**
	 * Determines whether nonce action is valid.
	 *
	 * @since 1.1.0
	 *
	 * @param string $action Scalar value to add context to nonce.
	 * @return bool
	 */
	public static function is_valid_nonce_action( $action ) {
		return isset( $_POST ) &&
			isset( $_POST[ self::POST_NONCE ] ) &&
			! Validator::is_false(
				wp_verify_nonce(
					sanitize_text_field( wp_unslash( $_POST[ self::POST_NONCE ] ) ),
					sanitize_key( $action )
				)
			);
	}

	/**
	 * Determines whether admin bar is showing.
	 *
	 * @since 1.1.0
	 *
	 * @return bool
	 */
	public static function is_admin_bar_showing() {
		return is_user_logged_in() || is_admin_bar_showing();
	}

	/**
	 * Determines whether customiser object is supported.
	 *
	 * @since 1.1.0
	 *
	 * @param string $customiser_object Customiser object.
	 * @return bool
	 */
	private static function is_supported_customiser_object( $customiser_object ) {
		return Validator::has_string_list_item(
			Customiser_Object::SUPPORTED_OBJECTS,
			$customiser_object
		);
	}

	/**
	 * Determines whether current user ID is valid.
	 *
	 * @since 1.2.0
	 *
	 * @param int $current_user_id Current user id.
	 * @return bool
	 */
	private static function is_valid_current_user_id( $current_user_id ) {
		return Validator::is_positive_int( $current_user_id );
	}
}
