<?php
/**
 * Customise Control Pro Features Class
 *
 * @package errigal
 * @since 1.0.0
 */

namespace Errigal\Customiser;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Component_Class as Component_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
};
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Theme_Info as Theme_Info,
	Errigal_Validator as Validator,
};
use WP_Customize_Control;

	/**
	 * Customise Pro Features Control Class.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/classes/wp_customize_control/
	 */
final class Errigal_Customise_Control_Pro_Features extends WP_Customize_Control {
	/**
	 * Priority pro features.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	private const PRIORITY_PRO_FEATURES = 'priority_pro_features';

	/**
	 * Pro features.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	private const PRO_FEATURES = 'pro_features';

	/**
	 * Control type.
	 *
	 * @since 1.0.0
	 *
	 * @var string
	 */
	public $type = 'errigal-pro-features';

	/**
	 * Renders control's content.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/classes/wp_customize_control/render_content/
	 *
	 * @return void
	 */
	public function render_content() {
		Html_Builder::create()->create_div(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'pro-features',
				),
			)
		)->render_element(
			function () {
				$this->render_pro_features_title();
				$this->render_pro_features_list();
				$this->render_discover_button();
			}
		);
	}

	/**
	 * Renders pro features title.
	 *
	 * @since 1.0.5
	 *
	 * @return void
	 */
	public function render_pro_features_title() {
		Html_Builder::create()->create_div(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'customize-control-errigal-pro-features-title',
				),
			)
		)->render_element(
			esc_html__( 'errigal PRO coming soon', 'errigal' )
		);
	}

	/**
	 * Renders pro features list.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function render_pro_features_list() {
		$priority_pro_features = $this->get_priority_pro_features();
		$pro_features          = $this->get_pro_features();

		if (
			! Validator::is_string_list( $priority_pro_features ) &&
			! Validator::is_string_list( $pro_features )
		) {
			return '';
		}

		Html_Builder::create(
			array(
				self::PRIORITY_PRO_FEATURES => $priority_pro_features,
				self::PRO_FEATURES          => $pro_features,
			)
		)->create_ul()->render_element(
			function ( $html_builder ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				foreach ( $html_builder->get_arg( self::PRIORITY_PRO_FEATURES ) as $priority_pro_feature ) {
					if ( ! Validator::is_non_empty_string( $priority_pro_feature ) ) {
						continue;
					}

					$html_builder->create_li()->render_element(
						function ( $html_builder ) use ( $priority_pro_feature ) {
							if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
								return;
							}

							$html_builder->create_strong()->render_element(
								esc_html( $priority_pro_feature )
							);
						}
					);
				}

				foreach ( $html_builder->get_arg( self::PRO_FEATURES ) as $pro_feature ) {
					if ( ! Validator::is_non_empty_string( $pro_feature ) ) {
						continue;
					}

					$html_builder->create_li()->render_element(
						esc_html( $pro_feature )
					);
				}
			}
		);
	}

	/**
	 * Renders discover button.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public function render_discover_button() {
		$should_temporarily_hide_button = true;

		if ( Validator::is_true( $should_temporarily_hide_button ) ) {
			return '';
		}

		$author_uri = Theme_Info::get_theme_author_uri( Theme_Info::IS_PARENT );
		$theme_name = Theme_Info::get_theme_name( Theme_Info::IS_PARENT );

		if (
			! Validator::is_non_empty_string( $author_uri ) ||
			! Validator::is_non_empty_string( $theme_name )
		) {
			return '';
		}

		Html_Builder::create()->create_a(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => Component_Class::BUTTON,
					Html_Attr::HREF       => sprintf(
						'%sthemes/%s-pro/',
						esc_url( $author_uri ),
						sanitize_title( $theme_name )
					),
					Html_Attr::TARGET     => Html_Attr::VALUE_BLANK,
					Html_Attr::REL        => array(
						Html_Attr::VALUE_NOOPENER,
						Html_Attr::VALUE_NOFOLLOW,
					),
				),
			)
		)->render_element(
			esc_html__( 'Discover errigal PRO', 'errigal' )
		);
	}

	/**
	 * Gets priority pro features.
	 *
	 * @since 1.0.0
	 *
	 * @return string[]
	 */
	private function get_priority_pro_features() {
		return array(
			esc_html__( 'Hero banner carousel', 'errigal' ),
			esc_html__( 'Table of contents', 'errigal' ),
			esc_html__( 'Cookie notification', 'errigal' ),
			esc_html__( 'Share buttons', 'errigal' ),
			esc_html__( 'Related posts', 'errigal' ),
		);
	}

	/**
	 * Gets pro features.
	 *
	 * @since 1.0.0
	 *
	 * @return string[]
	 */
	private function get_pro_features() {
		return array(
			esc_html__( 'Sidebar layout options', 'errigal' ),
			esc_html__( 'Multiple blog header styles', 'errigal' ),
			esc_html__( 'Additional colour schemes', 'errigal' ),
			esc_html__( 'and more...', 'errigal' ),
		);
	}
}
