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

namespace Errigal\Sidebar;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Sidebar_Hook as Sidebar_Hook,
};
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};
use Errigal\Widget\Errigal_Profile_Widget as Profile_Widget;

/**
 * This class is in charge of providing sidebar settings and functions.
 *
 * @since 1.1.0
 */
class Errigal_Sidebar extends Errigal_Sidebar_Base {
	/**
	 * Constructor.
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		parent::__construct(
			array(
				self::CALLBACK_WIDGETS_INIT => array( $this, 'register_widgets' ),
			)
		);
	}

	/**
	 * Determines whether sidebar can be displayed.
	 *
	 * @since 1.1.0
	 *
	 * @return bool
	 */
	public static function can_show() {
		/**
		 * Filters can show.
		 *
		 * @since 1.1.0
		 *
		 * @param bool $can_show Whether sidebar can be displayed.
		 */
		$can_show = apply_filters(
			Sidebar_Hook::CAN_SHOW_SIDEBAR,
			parent::can_show()
		);

		return Validator::is_true( $can_show );
	}

	/**
	 * Registers widgets.
	 *
	 * @since 1.1.0
	 *
	 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
	 *
	 * @return void
	 */
	public function register_widgets() {
		register_widget( Profile_Widget::class );
	}

	/**
	 * Gets sidebar ID.
	 *
	 * @since 1.1.0
	 *
	 * @return string
	 */
	protected static function get_sidebar_id() {
		return 'sidebar-1';
	}

	/**
	 * Gets sidebar args.
	 *
	 * @since 1.1.0
	 *
	 * @link https://developer.wordpress.org/reference/functions/register_sidebar/
	 *
	 * @return array
	 */
	protected function get_sidebar_args() {
		$html_builder = Html_Builder::create();
		$widget       = $html_builder->create_section(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::ID                  => '%1$s',
					Html_Attr::CLASS_NAME_TEMPLATE => array(
						'widget',
						'%2$s',
					),
				),
			)
		);
		$title        = $html_builder->create_h2(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'widget-title',
				),
			)
		);

		return array(
			self::SIDEBAR_NAME          => esc_html__( 'Sidebar', 'errigal' ),
			self::SIDEBAR_DESCRIPTION   => esc_html__( 'Add widgets here.', 'errigal' ),
			self::SIDEBAR_BEFORE_WIDGET => $widget->get_open_tag(),
			self::SIDEBAR_AFTER_WIDGET  => $widget->get_close_tag(),
			self::SIDEBAR_BEFORE_TITLE  => $title->get_open_tag(),
			self::SIDEBAR_AFTER_TITLE   => $title->get_close_tag(),
		);
	}
}
