<?php
/**
 * Template part for displaying profile widget
 *
 * @package errigal
 * @since 1.0.3
 *
 * @param array $args {
 *     Optional. Associative array of arguments for profile widget.
 *
 *     @type string $profile_page_id Optional. Post ID for profile page. Default empty string.
 *     @type string $image_url       Optional. Image URL. Default empty string.
 *     @type string $image_alt       Optional. Image alternate text. Default empty string.
 *     @type string $name            Optional. Profile name. Default empty string.
 *     @type string $description     Optional. Profile description. Default empty string.
 *     @type string $facebook_url    Optional. Facebook URL. Default empty string.
 *     @type string $x_url           Optional. X URL. Default empty string.
 *     @type string $twitter_url     Optional. Twitter URL. Default empty string.
 *     @type string $instagram_url   Optional. Instagram URL. Default empty string.
 *     @type string $linkedin_url    Optional. LinkedIn URL. Default empty string.
 *     @type string $youtube_url     Optional. YouTube URL. Default empty string.
 *     @type string $github_url      Optional. GitHub URL. Default empty string.
 *     @type string $line_url        Optional. LINE URL. Default empty string.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_WP_Hook as WP_Hook,
};
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};
use Errigal\Social\Errigal_Social_Link as Social_Link;
use Errigal\Widget\Errigal_Profile_Widget as Profile_Widget;

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Profile_Widget::PROFILE_PAGE_URL   => '',
		Profile_Widget::PROFILE_PAGE_TITLE => '',
		Profile_Widget::IMAGE_URL          => '',
		Profile_Widget::IMAGE_ALT          => '',
		Profile_Widget::NAME               => '',
		Profile_Widget::DESCRIPTION        => '',
		Profile_Widget::URL_FACEBOOK       => '',
		Profile_Widget::URL_X              => '',
		Profile_Widget::URL_TWITTER        => '',
		Profile_Widget::URL_INSTAGRAM      => '',
		Profile_Widget::URL_LINKEDIN       => '',
		Profile_Widget::URL_YOUTUBE        => '',
		Profile_Widget::URL_GITHUB         => '',
		Profile_Widget::URL_LINE           => '',
	)
);

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

		$html_builder->create_figure(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'errigal-profile-widget__profile-image-container',
				),
			)
		)->render_element(
			function ( $html_builder ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				$args = $html_builder->get_args();
				/**
				* Renders profile image.
				*
				* @since 1.2.0
				*
				* @return void
				*/
				$render_profile_image = function () use ( $args ) {
					if ( ! is_string( $args[ Profile_Widget::IMAGE_ALT ] ) ) {
						return;
					}

					if ( ! Validator::is_non_empty_string( $args[ Profile_Widget::IMAGE_URL ] ) ) {
						Icon::render_icon( Icon::DEFAULT_PROFILE_IMAGE, Icon::SIZE_3XL );

						return;
					}

					get_template_part(
						'template-parts/component/widget/image',
						null,
						array(
							Profile_Widget::IMAGE_CLASS => 'errigal-profile-widget__profile-image',
							Profile_Widget::IMAGE_URL   => esc_url( $args[ Profile_Widget::IMAGE_URL ] ),
							Profile_Widget::IMAGE_ALT   => esc_attr( $args[ Profile_Widget::IMAGE_ALT ] ),
						)
					);
				};

				if ( Validator::is_non_empty_string( $html_builder->get_arg( Profile_Widget::PROFILE_PAGE_URL ) ) ) {
					$html_builder->create_a(
						array(
							Html_Option::ATTRS => array(
								Html_Attr::HREF       => $html_builder->get_arg( Profile_Widget::PROFILE_PAGE_URL ),
								Html_Attr::ARIA_LABEL => apply_filters(
									WP_Hook::THE_TITLE,
									$html_builder->get_arg( Profile_Widget::PROFILE_PAGE_TITLE )
								),
							),
						)
					)->render_element( $render_profile_image );
				} else {
					$render_profile_image();
				}
			}
		);

		if ( Validator::is_non_empty_string( $html_builder->get_arg( Profile_Widget::NAME ) ) ) {
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'errigal-profile-widget__profile-name',
					),
				)
			)->render_element(
				esc_html(
					$html_builder->get_arg( Profile_Widget::NAME )
				)
			);
		}

		if ( Validator::is_non_empty_string( $html_builder->get_arg( Profile_Widget::DESCRIPTION ) ) ) {
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'errigal-profile-widget__description',
					),
				)
			)->render_element(
				$html_builder->get_arg( Profile_Widget::DESCRIPTION )
			);
		}

		$social_links = Social_Link::get_social_links(
			array(
				Social_Link::URL_FACEBOOK  => $html_builder->get_arg( Profile_Widget::URL_FACEBOOK ),
				Social_Link::URL_X         => $html_builder->get_arg( Profile_Widget::URL_X ),
				Social_Link::URL_TWITTER   => $html_builder->get_arg( Profile_Widget::URL_TWITTER ),
				Social_Link::URL_INSTAGRAM => $html_builder->get_arg( Profile_Widget::URL_INSTAGRAM ),
				Social_Link::URL_LINKEDIN  => $html_builder->get_arg( Profile_Widget::URL_LINKEDIN ),
				Social_Link::URL_YOUTUBE   => $html_builder->get_arg( Profile_Widget::URL_YOUTUBE ),
				Social_Link::URL_GITHUB    => $html_builder->get_arg( Profile_Widget::URL_GITHUB ),
				Social_Link::URL_LINE      => $html_builder->get_arg( Profile_Widget::URL_LINE ),
			)
		);

		if ( Validator::is_non_empty_string( $social_links ) ) {
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'errigal-profile-widget__social-icons-container',
					),
				)
			)->render_element( $social_links );
		}
	}
);
