<?php
/**
 * Rewards admin controller.
 *
 * @package Awesomemotive\Rewardswp\Controllers\Admin
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Controllers\Admin;

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

use Awesomemotive\Rewardswp\Factories\ViewsFactory;
use Awesomemotive\Rewardswp\Interfaces\ControllerInterface;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Views\Admin\ListTables\RewardsListTableView;
use function Awesomemotive\Rewardswp\Helpers\is_rewardswp_admin_page;

class RewardsAdminController implements ControllerInterface {
	/**
	 * Admin submenu position.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	const MENU_POSITION = 40;

	/**
	 * Admin submenu slug.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	const MENU_SLUG = 'rewardswp-rewards';

	/**
	 * Views factory instance.
	 *
	 * @since 1.0.0
	 *
	 * @var ViewsFactory
	 */
	private ViewsFactory $views_factory;

	/**
	 * Construct.
	 *
	 * @since 1.0.0
	 *
	 * @param ViewsFactory $views_factory
	 */
	public function __construct( ViewsFactory $views_factory ) {
		$this->views_factory = $views_factory;
	}

	/**
	 * Get page title.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function get_page_title(): string {
		return esc_html__( 'Rewards', 'rewardswp' );
	}

	/**
	 * Init.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'admin_menu', [ $this, 'add_admin_menu' ], self::MENU_POSITION );
		add_action( 'admin_head', [ $this, 'add_screen_options' ] );
		add_filter( 'set-screen-option', [ $this, 'action_rewards_set_screen_option' ], 10, 3 );
	}

	/**
	 * Register the menu.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function add_admin_menu() {
		add_submenu_page(
			MainAdminController::MAIN_MENU_SLUG,
			$this->get_page_title(),
			$this->get_page_title(),
			'manage_options',
			self::MENU_SLUG,
			[ $this, 'render_page' ],
			self::MENU_POSITION
		);
	}

	/**
	 * Register screen options.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function add_screen_options(): void {
		if ( ! is_rewardswp_admin_page( self::MENU_SLUG ) ) {
			return;
		}

		add_screen_option(
			'per_page',
			[
				'label'   => esc_html__( 'Number of rewards per page:', 'rewardswp' ),
				'option'  => 'rewards_per_page',
				'default' => 20,
			]
		);

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce not required for this.
		if ( empty( $_REQUEST['action'] ) || ( 'view_reward' !== $_REQUEST['action'] ) ) {
			/*
			 * Instantiate the list table to make the columns array available to screen options.
			 *
			 * If the 'view_referral' action is set, don't instantiate. Instantiating in sub-views
			 * creates conflicts in the screen option column controls if another list table is being
			 * displayed.
			 */
			$this->views_factory->get_view( RewardsListTableView::class );
		}
	}

	/**
	 * Per page screen option value for the Rewards list table
	 *
	 * @since  1.0.0
	 *
	 * @param  bool|int $status
	 * @param  string   $option
	 * @param  mixed    $value
	 *
	 * @return mixed
	 */
	public function action_rewards_set_screen_option( $status, string $option, $value ) {

		if ( 'rewards_per_page' === $option ) {
			update_user_meta( get_current_user_id(), $option, $value );

			return $value;
		}

		return $status;
	}

	/**
	 * Render the page.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function render_page(): void {
		$list_table = $this->views_factory->get_view( RewardsListTableView::class );

		if ( is_subclass_of( $list_table, ViewInterface::class ) ) {
			$list_table->render();
		}
	}
}
