<?php
/**
 * Sidebar Base Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Sidebar;

defined( 'ABSPATH' ) || exit;

use Errigal\Shared\Errigal_Validator as Validator;

/**
 * This class is in charge of providing shared sidebar functions.
 *
 * @since 1.1.0
 */
abstract class Errigal_Sidebar_Base {
	/**
	 * Sidebar arg: name.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const SIDEBAR_NAME = 'name';

	/**
	 * Sidebar arg: description.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const SIDEBAR_DESCRIPTION = 'description';

	/**
	 * Sidebar arg: before_widget.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const SIDEBAR_BEFORE_WIDGET = 'before_widget';

	/**
	 * Sidebar arg: after_widget.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const SIDEBAR_AFTER_WIDGET = 'after_widget';

	/**
	 * Sidebar arg: before_title.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const SIDEBAR_BEFORE_TITLE = 'before_title';

	/**
	 * Sidebar arg: after_title.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const SIDEBAR_AFTER_TITLE = 'after_title';

	/**
	 * Constructor arg: Callback for widgets_init.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	protected const CALLBACK_WIDGETS_INIT = 'widgets_init_callback';

	/**
	 * Sidebar: ID.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const SIDEBAR_ID = 'id';

	/**
	 * Constructor.
	 *
	 * @since 1.1.0
	 *
	 * @param array $args {
	 *     Associative array of arguments for sidebar. Default empty array.
	 *
	 *     @type callable $widgets_init_callback Callback for widgets_init. Default null.
	 * }
	 */
	public function __construct( $args ) {
		if ( ! $this->register_sidebar() || ! is_array( $args ) ) {
			return;
		}

		$args = wp_parse_args(
			$args,
			array(
				self::CALLBACK_WIDGETS_INIT => null,
			)
		);

		$widgets_init_callback = $args[ self::CALLBACK_WIDGETS_INIT ];

		if ( ! is_callable( $widgets_init_callback ) ) {
			return;
		}

		add_action( 'widgets_init', $widgets_init_callback );
	}

	/**
	 * Gets sidebar ID.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	abstract protected static function get_sidebar_id();

	/**
	 * Gets sidebar args.
	 *
	 * @since 1.1.0
	 *
	 * @link https://developer.wordpress.org/reference/functions/register_sidebar/
	 *
	 * @return array
	 */
	abstract protected function get_sidebar_args();

	/**
	 * Renders sidebar.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	public static function render_sidebar() {
		dynamic_sidebar( static::get_sidebar_id() );
	}

	/**
	 * Determines whether sidebar can be displayed.
	 *
	 * @since 1.1.0
	 *
	 * @return bool
	 */
	public static function can_show() {
		return is_active_sidebar( static::get_sidebar_id() );
	}

	/**
	 * Registers sidebar.
	 *
	 * @since 1.1.0
	 *
	 * @return bool
	 */
	private function register_sidebar() {
		$sidebar_args = static::get_sidebar_args();

		if ( ! Validator::is_associative_array( $sidebar_args ) ) {
			return false;
		}

		$sidebar_args = wp_parse_args(
			$sidebar_args,
			array(
				self::SIDEBAR_ID => sanitize_key( static::get_sidebar_id() ),
			)
		);

		return (bool) register_sidebar( $sidebar_args );
	}
}
