<?php
/**
 * Post Author
 *
 * @author  Jegstudio
 * @since   1.0.0
 * @package gutenverse-news
 */

namespace GUTENVERSE\NEWS\Block;

use GUTENVERSE\NEWS\Social_Contacts;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


/**
 * Post_Author
 *
 * @package gutenverse-news
 * @author Jegstudio
 */
class Post_Author extends Post_Guten {
	/**
	 * Method get_content
	 *
	 * @return string
	 */
	public function get_content() {
		$post_id   = ! empty( $this->context['postId'] ) ? esc_html( $this->context['postId'] ) : get_the_ID();
		$author_id = is_author() ? get_queried_object_id() : get_post_field( 'post_author', $post_id );
		$author    = array(
			'id'   => $author_id,
			'name' => get_the_author_meta( 'display_name', $author_id ),
			'url'  => get_author_posts_url( $author_id ),
			'desc' => get_the_author_meta( 'description', $author_id ),
		);

		$block = '<div class="gvnews-author-content">
					<' . $this->attributes['titleTag'] . ' class="gvnews-author-name">
						<a href="' . esc_url( $author['url'] ) . '" aria-label="' . esc_attr( $author['name'] ) . '">' . esc_html( $author['name'] ) . '</a>
					</' . $this->attributes['titleTag'] . '>
					<p class="gvnews-author-desc">' . esc_html( $author['desc'] ) . '</p>
					<div class="gvnews-author-socials">' .
			$this->generate_social_element( $author_id ) .
			'</div>
				</div>';

		if ( 'left' === $this->attributes['avatarPosition'] || 'top' === $this->attributes['avatarPosition'] ) {
			$block = $this->render_avatar( $author ) . $block;
		} else {
			$block = $block . $this->render_avatar( $author );
		}

		return $block;
	}

	/**
	 * Render view in editor
	 */
	public function render_gutenberg() {
		return null;
	}

	/**
	 * Render view in frontend
	 */
	public function render_frontend() {
		$element_id      = $this->get_element_id();
		$display_classes = $this->set_display_classes();
		$custom_classes  = $this->get_custom_classes();
		$avatar_position = ' avatar-' . $this->attributes['avatarPosition'] . ' ';

		return '<div class="' .
			$element_id .
			$display_classes .
			// $animation_class .
			$custom_classes .
			'gvnews-post-author guten-element
							' . $avatar_position . '" 
				>' . $this->render_content() . '</div>';
	}

	/**
	 * Method generate_social_element
	 *
	 * @param string $author_id id of author.
	 *
	 * @return string
	 */
	protected function generate_social_element( $author_id ) {
		$social_elements = '';
		$social_icons    = Social_Contacts::social_icon_list();
		foreach ( $social_icons as $social => $icon ) {
			$url = get_the_author_meta( $social, $author_id );

			if ( '' !== $url ) {
				$social_elements .=
					'<a href="' . $url . '" class="' . $social . '" aria-label="' . esc_attr( $social ) . '">
					<div class="gutenverse-icon-svg">
						' . $icon . '
					</div>
					</a>';
			}
		}

		return $social_elements;
	}

	/**
	 * Render avatar
	 *
	 * @param array $author author data.
	 *
	 * @return string
	 */
	private function render_avatar( $author ) {
		return '<div class="gvnews-author-image">' .
			get_avatar( $author['id'], 80, null, $author['name'] ) .
			'</div>';
	}
}
