<?php

namespace SugarCalendarBookings\Support\Admin;

use SugarCalendarBookings\Admin\Area;
use SugarCalendarBookings\Support\Admin\PageLayouts\LayoutInterface;
use SugarCalendarBookings\Support\Helpers\WP;

/**
 * Page abstract.
 *
 * @since 1.0.0
 */
abstract class AbstractPage {

	/**
	 * Page layout.
	 *
	 * @since 1.0.0
	 *
	 * @var LayoutInterface
	 */
	protected $layout;

	/**
	 * Page title.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	abstract public static function get_title();

	/**
	 * Page slug.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	abstract public static function get_slug();

	/**
	 * View slug.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_view_slug() {

		return '';
	}

	/**
	 * Menu slug for navigation highlighting.
	 *
	 * By default, returns the view slug. Override this method in child classes
	 * to control which tab is highlighted in navigation.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_menu_slug() {

		return static::get_view_slug();
	}

	/**
	 * Page URL.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_url() {

		$query_args = [
			'page' => Area::SLUG . '-' . static::get_slug(),
		];

		if ( ! empty( static::get_view_slug() ) ) {
			$query_args['view'] = static::get_view_slug();
		}

		return add_query_arg( $query_args, WP::admin_url( 'admin.php' ) );
	}

	/**
	 * Page ID.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function get_id() {

		$page_id = static::get_slug();

		if ( ! empty( static::get_view_slug() ) ) {
			$page_id .= '-' . static::get_view_slug();
		}

		return $page_id;
	}

	/**
	 * Check if the page should be loaded.
	 *
	 * Override this method in child classes to add conditional loading logic.
	 *
	 * @since 1.3.0
	 *
	 * @return bool True if the page should be loaded, false otherwise.
	 */
	public static function should_load() {

		return true;
	}

	/**
	 * Get the capability required to access this page.
	 *
	 * @since 1.4.0
	 *
	 * @return string
	 */
	public static function get_capability() {

		return 'manage_options';
	}

	/**
	 * Get page script data.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_script_data() {

		return [];
	}

	/**
	 * Register page hooks.
	 *
	 * @since 1.0.0
	 */
	public function hooks() { }

	/**
	 * Display page.
	 *
	 * @since 1.0.0
	 */
	public function display() {

		if ( empty( $this->layout ) ) {
			return;
		}

		$this->layout->display( $this );
	}

	/**
	 * Whether to show the subheader.
	 *
	 * @since 1.2.0
	 *
	 * @return bool
	 */
	public function should_show_subheader() {

		return true;
	}

	/**
	 * Display page content.
	 *
	 * @since 1.0.0
	 */
	public function display_content() { }

	/**
	 * Post data processing rules.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	public function get_post_data_processing_rules() {

		return [];
	}

	/**
	 * Handle POST requests.
	 *
	 * @since 1.0.0
	 *
	 * @param array $post_data Post data.
	 */
	public function handle_post( $post_data = [] ) { }

	/**
	 * Set page layout.
	 *
	 * @since 1.0.0
	 *
	 * @param LayoutInterface $layout Page layout.
	 */
	public function set_layout( LayoutInterface $layout ) {

		$this->layout = $layout;
	}
}
