<?php
/**
 * Template part for displaying notice item
 *
 * @package errigal
 * @since 1.1.0
 *
 * @param array $args {
 *     Optional. Associative array of arguments for admin notice item.
 *
 *     @type string $tagline                Optional. Tagline. Default empty string.
 *     @type string $icon                   Optional. Icon. Default empty string.
 *     @type string $notification_type      Optional. Notification type. Default empty string.
 *     @type string $notification_content   Optional. Notification content. Default empty string.
 *     @type string $item_title             Optional. Item title. Default empty string.
 *     @type string $item_description       Optional. Item description. Default empty string.
 *     @type string $item_instructions      Optional. Item instructions. Default empty string.
 *     @type string $cta_customiser_url     Optional. CTA: Customiser URL. Default empty string.
 *     @type string $cta_customiser_text    Optional. CTA: Customiser Text. Default empty string.
 *     @type string $cta_documentation_url  Optional. CTA: Documentation URL. Default empty string.
 *     @type string $cta_documentation_text Optional. CTA: Documentation Text. Default empty string.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Admin\Errigal_Notice as Notice;
use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_Class,
	Errigal_Admin_Class as Admin_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
};
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Notice::TAGLINE                => '',
		Notice::ICON                   => '',
		Notice::NOTIFICATION_TYPE      => '',
		Notice::NOTIFICATION_CONTENT   => '',
		Notice::ITEM_TITLE             => '',
		Notice::ITEM_DESCRIPTION       => '',
		Notice::ITEM_INSTRUCTIONS      => '',
		Notice::CTA_CUSTOMISER_URL     => '',
		Notice::CTA_CUSTOMISER_TEXT    => '',
		Notice::CTA_DOCUMENTATION_URL  => '',
		Notice::CTA_DOCUMENTATION_TEXT => '',
	)
);

Html_Builder::create( $args )->create_div(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => 'errigal-notice__item',
		),
	)
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		$html_builder->create_div(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'errigal-notice__item-inner-container',
				),
			)
		)->render_element(
			function ( $html_builder ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				$tagline = $html_builder->get_arg( Notice::TAGLINE );

				if ( Validator::is_non_empty_string( $tagline ) ) {
					$html_builder->create_p(
						array(
							Html_Option::ATTRS => array(
								Html_Attr::CLASS_NAME => 'errigal-notice__item__tagline',
							),
						)
					)->render_element(
						esc_html( $tagline )
					);
				}

				$html_builder->create_div(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => 'errigal-notice__item__content',
						),
					)
				)->render_element(
					function ( $html_builder ) {
						if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
							return;
						}

						$icon = $html_builder->get_arg( Notice::ICON );

						if ( Icon::is_supported_icon( $icon ) ) {
							Icon::render_icon( $icon, Icon::SIZE_XL );
						}

						$html_builder->create_div(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => 'errigal-notice__item__content__container',
								),
							)
						)->render_element(
							function ( $html_builder ) {
								if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
									return;
								}

								if (
									Notice::is_supported_notification_type( $html_builder->get_arg( Notice::NOTIFICATION_TYPE ) ) &&
									Validator::is_non_empty_string( $html_builder->get_arg( Notice::NOTIFICATION_CONTENT ) )
								) {
									$html_builder->create_div(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => 'errigal-notice__item__notification',
											),
										)
									)->render_element(
										function ( $html_builder ) {
											if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
												return;
											}

											$html_builder->create_div(
												array(
													Html_Option::ATTRS => array(
														Html_Attr::CLASS_NAME => 'errigal-notice__item__notification__type',
													),
												)
											)->render_element(
												function ( $html_builder ) {
													if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
														return;
													}

													switch ( $html_builder->get_arg( Notice::NOTIFICATION_TYPE ) ) {
														case Notice::NOTIFICATION_TYPE_UPDATED:
															Icon::render_icon( Icon::NOTIFICATION_BELL );
															esc_html_e( 'Updated:', 'errigal' );
													}
												}
											);
											$html_builder->create_div(
												array(
													Html_Option::ATTRS => array(
														Html_Attr::CLASS_NAME => 'errigal-notice__item__notification__content',
													),
												)
											)->render_element(
												esc_html(
													$html_builder->get_arg( Notice::NOTIFICATION_CONTENT )
												)
											);
										}
									);
								}

								$item_title = $html_builder->get_arg( Notice::ITEM_TITLE );

								if ( Validator::is_non_empty_string( $item_title ) ) {
									$html_builder->create_h4(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => 'errigal-notice__item__content__title',
											),
										)
									)->render_element(
										esc_html( $item_title )
									);
								}

								$item_description = $html_builder->get_arg( Notice::ITEM_DESCRIPTION );

								if ( Validator::is_non_empty_string( $item_description ) ) {
									$html_builder->create_p(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => 'errigal-notice__item__content__description',
											),
										)
									)->render_element(
										esc_html( $item_description )
									);
								}

								$item_instructions = $html_builder->get_arg( Notice::ITEM_INSTRUCTIONS );

								if ( Validator::is_non_empty_string( $item_instructions ) ) {
									$html_builder->create_p(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => 'errigal-notice__item__content__instructions',
											),
										)
									)->render_element(
										esc_html( $item_instructions )
									);
								}

								if (
								(
									Validator::is_non_empty_string( $html_builder->get_arg( Notice::CTA_CUSTOMISER_URL ) ) &&
									Validator::is_non_empty_string( $html_builder->get_arg( Notice::CTA_CUSTOMISER_TEXT ) )
								) ||
								(
									Validator::is_non_empty_string( $html_builder->get_arg( Notice::CTA_DOCUMENTATION_URL ) ) &&
									Validator::is_non_empty_string( $html_builder->get_arg( Notice::CTA_DOCUMENTATION_TEXT ) )
								)
								) {
									$html_builder->create_div(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => 'errigal-notice__item__content__cta-container',
											),
										)
									)->render_element(
										function ( $html_builder ) {
											if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
												return;
											}

											$cta_customiser_url  = $html_builder->get_arg( Notice::CTA_CUSTOMISER_URL );
											$cta_customiser_text = $html_builder->get_arg( Notice::CTA_CUSTOMISER_TEXT );

											if (
												current_user_can( 'customize' ) &&
												Validator::is_non_empty_string( $cta_customiser_url ) &&
												Validator::is_non_empty_string( $cta_customiser_text )
											) {
												$html_builder->create_a(
													array(
														Html_Option::ATTRS => array(
															Html_Attr::CLASS_NAME => array(
																Admin_Class::NOTICE_ITEM_CONTENT_CTA,
																'errigal-admin__button',
															),
															Html_Attr::HREF => $cta_customiser_url,
														),
													)
												)->render_element(
													esc_html( $cta_customiser_text )
												);
											}

											$cta_documentation_url  = $html_builder->get_arg( Notice::CTA_DOCUMENTATION_URL );
											$cta_documentation_text = $html_builder->get_arg( Notice::CTA_DOCUMENTATION_TEXT );

											if (
												Validator::is_non_empty_string( $cta_documentation_url ) &&
												Validator::is_non_empty_string( $cta_documentation_text )
											) {
												$html_builder->create_a(
													array(
														Html_Option::ATTRS => array(
															Html_Attr::CLASS_NAME => array(
																Admin_Class::NOTICE_ITEM_CONTENT_CTA,
																'errigal-admin__link-external',
															),
															Html_Attr::HREF => $cta_documentation_url,
															Html_Attr::TARGET => Html_Attr::VALUE_BLANK,
															Html_Attr::REL => array(
																Html_Attr::VALUE_NOOPENER,
																Html_Attr::VALUE_NOFOLLOW,
															),
														),
													)
												)->render_element(
													function ( $html_builder ) use ( $cta_documentation_text ) {
														if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
															return;
														}

														echo esc_html( $cta_documentation_text );
														Icon::render_icon( Icon::OPEN_NEW_WINDOW );
														$html_builder->create_span(
															array(
																Html_Option::ATTRS => array(
																	Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
																),
															)
														)->render_element(
															/* translators: Ensure the screen reader text starts with a full stop (.) in the language for proper reading pause. */
															esc_html__( '. Opens in a new window', 'errigal' )
														);
													}
												);
											}
										}
									);
								}
							}
						);
					}
				);
			}
		);
	}
);
