<?php
/**
 * Admin interface helper functions.
 *
 * @package Awesomemotive\Rewardswp\Helpers
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Helpers;

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

use Awesomemotive\Rewardswp\Views\Admin\Components\AmountComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\BadgeComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\ButtonComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\ChoicesComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\ColorPickerComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\DrawerRequirementComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\RadioComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\SectionComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\ToggleComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\SelectComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\InputComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\NumberComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\ModalComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\UpgradeOverlayComponentView;
use Awesomemotive\Rewardswp\Views\Admin\Components\EmptyStateComponentView;
use Exception;

function get_rewardswp_admin_url( string $page ): string {
	return add_query_arg(
		[
			'page' => $page,
		],
		admin_url( 'admin.php' )
	);
}

/**
 * Check if the current request is on a RewardsWP admin page.
 *
 * @since 1.0.0
 *
 * @param string|null $page Optional page to compare.
 *
 * @return bool True if in the RewardsWP admin area, false otherwise.
 */
function is_rewardswp_admin_page( ?string $page = null ): bool {
	if ( ! is_wp_admin_page( $page ) ) {
		return false;
	}

	$screen = get_current_screen();

	$is_rewardswp_page = ( $screen && false !== strpos( $screen->id, 'rewardswp' ) );

	if ( is_null( $page ) ) {
		return $is_rewardswp_page;
	}

	// Compare if it is on a specific page.
	return sanitize_text_field( filter_input( INPUT_GET, 'page' ) ) === $page && $is_rewardswp_page;
}

/**
 * Check if the current request is on a WordPress admin page.
 *
 * @since 1.0.0
 *
 * @param string|null $page Optional page to compare.
 *
 * @return bool True if in the WordPress admin area, false otherwise.
 */
function is_wp_admin_page( ?string $page = null ): bool {
	// Check if we are in the WordPress admin and not in an AJAX call.
	$is_admin = is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX );

	if ( is_null( $page ) ) {
		return $is_admin;
	}

	// Compare if it is on a specific page.
	return sanitize_text_field( filter_input( INPUT_GET, 'page' ) ) === $page && $is_admin;
}

/**
 * Check if the current request is on the "Add New User" page.
 *
 * @since 1.0.0
 *
 * @return bool True if on user-new.php, false otherwise.
 */
function is_user_new_page(): bool {
	global $pagenow;

	return 'user-new.php' === $pagenow;
}

/**
 * Return a reusable component by its name.
 *
 * @since 1.0.0
 *
 * @param string $component_name
 * @param array  $args
 *
 * @return string
 */
function get_view_component( string $component_name, array $args = [] ): string {
	try {
		switch ( $component_name ) {
			case 'section':
				return am_rewardswp()->get_container()->make( SectionComponentView::class )->get_contents( $args );
			case 'badge':
				return am_rewardswp()->get_container()->make( BadgeComponentView::class )->get_contents( $args );
			case 'button':
				return am_rewardswp()->get_container()->make( ButtonComponentView::class )->get_contents( $args );
			case 'toggle':
				return am_rewardswp()->get_container()->make( ToggleComponentView::class )->get_contents( $args );
			case 'select':
				return am_rewardswp()->get_container()->make( SelectComponentView::class )->get_contents( $args );
			case 'radio':
				return am_rewardswp()->get_container()->make( RadioComponentView::class )->get_contents( $args );
			case 'amount':
				return am_rewardswp()->get_container()->make( AmountComponentView::class )->get_contents( $args );
			case 'choices':
				return am_rewardswp()->get_container()->make( ChoicesComponentView::class )->get_contents( $args );
			case 'input':
				return am_rewardswp()->get_container()->make( InputComponentView::class )->get_contents( $args );
			case 'color-picker':
				return am_rewardswp()->get_container()->make( ColorPickerComponentView::class )->get_contents( $args );
			case 'number':
				return am_rewardswp()->get_container()->make( NumberComponentView::class )->get_contents( $args );
			case 'modal':
				return am_rewardswp()->get_container()->make( ModalComponentView::class )->get_contents( $args );
			case 'upgrade-overlay':
				return am_rewardswp()->get_container()->make( UpgradeOverlayComponentView::class )->get_contents( $args );
			case 'drawer-requirement':
				return am_rewardswp()->get_container()->make( DrawerRequirementComponentView::class )->get_contents( $args );
			case 'empty-state':
				return am_rewardswp()->get_container()->make( EmptyStateComponentView::class )->get_contents( $args );
			default:
				throw new Exception(
					sprintf(
						/* translators: %s is the component name */
						esc_html__( "Component %s doesn't exist.", 'rewardswp' ),
						$component_name
					)
				);
		}
	} catch ( Exception $exception ) {
		return $exception->getMessage();
	}
}
