<?php
/**
 * Base Admin screen used for extending.
 *
 * @link       https://personalizewp.com
 * @since      1.0.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Admin
 */

namespace PersonalizeWP\Admin;

use PersonalizeWP\Traits\SingletonTrait;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Class Base Admin
 */
abstract class Admin_Base_Page {

	use SingletonTrait;

	/**
	 * Stores any errors during form processing.
	 *
	 * @var array
	 */
	protected $validation_errors = [];

	/**
	 * Page title for the Admin page
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public $page_title;

	/**
	 * Holds instance of plugin object
	 *
	 * @var PersonalizeWP
	 */
	protected $plugin;

	/**
	 * Used to store the current page from the URL, shared between process/route actions
	 *
	 * @var string
	 */
	protected $current_page;

	/**
	 * Used to store the current action from the URL, shared between process/route actions
	 *
	 * @var string
	 */
	protected $current_action;

	/**
	 * Used to store the current processed form data, shared between process/route actions
	 *
	 * @var array
	 */
	protected $form_post = [];

	/**
	 * Constructor
	 *
	 * @since 3.2.0
	 */
	protected function __construct() {

		$this->plugin = \personalizewp();

		$this->current_action = isset( $_REQUEST[ Admin::URL_ACTION ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ Admin::URL_ACTION ] ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: Form verified within each process
		$this->current_page   = isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: Form verified within each process
	}

	/**
	 * Process - typically used for processing POST data.
	 */
	public function process() {}

	/**
	 * Route to the correct action within this page.
	 * Must be overwritten by the extending class.
	 */
	abstract public function route();

	/**
	 * Display the shared Header for all PWP pages.
	 *
	 * @since 1.0.0
	 *
	 * @param string $page_title Page title
	 *
	 * @return void
	 */
	public function display_header( $page_title = '' ) {
		if ( empty( $page_title ) ) {
			$page_title = $this->page_title;
		}
		$page     = $this->current_page;
		$base_url = plugins_url( '', __FILE__ );

		require_once plugin_dir_path( __DIR__ ) . 'admin/partials/_other/header.php';
	}


	/**
	 * Display the shared footer for all PWP pages.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function display_footer() {
		require_once plugin_dir_path( __DIR__ ) . 'admin/partials/_other/footer.php';
	}

	/**
	 * Display immediate error.
	 *
	 * @param string $error Message
	 * @param bool   $stop  Stop further dislay, defaults true
	 */
	public function showError( $error, $stop = true ) {
		require_once plugin_dir_path( __DIR__ ) . 'admin/partials/error.php';

		if ( $stop ) {
			die();
		}
	}

	/**
	 * Add validation error
	 *
	 * @param string $field Field name
	 * @param string $error Message
	 *
	 * @return void
	 */
	protected function addValidationError( $field, $error ) {
		if ( empty( $this->validation_errors[ $field ] ) ) {
			$this->validation_errors[ $field ] = [];
		}

		$this->validation_errors[ $field ][] = $error;
	}

	/**
	 * Retrieve validation error from named field
	 *
	 * @param  string $field Field name, if empty return all errors
	 *
	 * @return array
	 */
	protected function getError( $field = null ) {
		if ( empty( $field ) ) {
			return $this->validation_errors;
		}

		return empty( $this->validation_errors[ $field ] ) ? null : $this->validation_errors[ $field ];
	}
}
