<?php
/**
 * Widget Name: Post Author
 * Description: Display author avatar, name, bio, website, and archive link.
 *
 * @package captain-widgets-kit
 */

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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Box_Shadow;

/**
 * Class Captwiki_Post_Author
 */
class Captwiki_Post_Author extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-post-author';
	}

	/**
	 * Get widget title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Post Author', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-person captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'post author', 'author box', 'avatar', 'bio', 'profile', 'captain' );
	}

	/**
	 * Get style dependencies.
	 *
	 * @return array
	 */
	public function get_style_depends(): array {
		return array( 'captwiki_post_author' );
	}

	/**
	 * Mark widget as dynamic content.
	 *
	 * @return bool
	 */
	public function is_dynamic_content(): bool {
		return true;
	}

	/**
	 * Check Elementor optimized markup wrapper support.
	 *
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Register widget controls.
	 *
	 * @return void
	 */
	protected function register_controls(): void {

		$this->start_controls_section(
			'section_author_content',
			array(
				'label' => esc_html__( 'Post Author Settings', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => esc_html__( 'Layout', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'inline',
				'options' => array(
					'inline' => esc_html__( 'Inline', 'captain-widgets-kit' ),
					'card'   => esc_html__( 'Author Card', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'show_avatar',
			array(
				'label'        => esc_html__( 'Show Avatar', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'avatar_size',
			array(
				'label'     => esc_html__( 'Avatar Size', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 24,
						'max' => 180,
					),
				),
				'default'   => array( 'size' => 56 ),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-post-author__avatar img' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
				'condition' => array( 'show_avatar' => 'yes' ),
			)
		);

		$this->add_control(
			'before_name',
			array(
				'label'       => esc_html__( 'Before Name', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'By', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. Written by', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'link_name',
			array(
				'label'        => esc_html__( 'Link Name to Author Archive', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_bio',
			array(
				'label'        => esc_html__( 'Show Bio', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_website',
			array(
				'label'        => esc_html__( 'Show Website Link', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'website_text',
			array(
				'label'     => esc_html__( 'Website Text', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Website', 'captain-widgets-kit' ),
				'condition' => array( 'show_website' => 'yes' ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_author_box_style',
			array(
				'label' => esc_html__( 'Author Box', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'default'   => 'left',
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-post-author' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'author_background',
				'selector' => '{{WRAPPER}} .captwiki-post-author',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'author_border',
				'selector' => '{{WRAPPER}} .captwiki-post-author',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'author_shadow',
				'selector' => '{{WRAPPER}} .captwiki-post-author',
			)
		);

		$this->add_responsive_control(
			'author_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-post-author' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_author_text_style',
			array(
				'label' => esc_html__( 'Author Text', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'name_color',
			array(
				'label'     => esc_html__( 'Name Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-post-author__name, {{WRAPPER}} .captwiki-post-author__name a' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'name_typography',
				'selector' => '{{WRAPPER}} .captwiki-post-author__name',
			)
		);

		$this->add_control(
			'bio_color',
			array(
				'label'     => esc_html__( 'Bio Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .captwiki-post-author__bio' => 'color: {{VALUE}};',
				),
				'condition' => array( 'show_bio' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Get author ID from the current post.
	 *
	 * @return int
	 */
	private function get_author_id(): int {
		$post_id = get_the_ID();

		if ( $post_id ) {
			return (int) get_post_field( 'post_author', $post_id );
		}

		return (int) get_current_user_id();
	}

	/**
	 * Render widget output.
	 *
	 * @return void
	 */
	protected function render(): void {
		$settings  = $this->get_settings_for_display();
		$author_id = $this->get_author_id();

		if ( ! $author_id ) {
			return;
		}

		$name        = get_the_author_meta( 'display_name', $author_id );
		$bio         = get_the_author_meta( 'description', $author_id );
		$website     = get_the_author_meta( 'user_url', $author_id );
		$archive_url = get_author_posts_url( $author_id );
		$layout      = 'card' === $settings['layout'] ? 'card' : 'inline';
		$avatar_size = ! empty( $settings['avatar_size']['size'] ) ? absint( $settings['avatar_size']['size'] ) : 56;

		echo '<div class="captwiki-post-author captwiki-post-author--' . esc_attr( $layout ) . '">';

		if ( 'yes' === $settings['show_avatar'] ) {
			echo '<div class="captwiki-post-author__avatar">' . get_avatar( $author_id, $avatar_size ) . '</div>';
		}

		echo '<div class="captwiki-post-author__content">';
		echo '<div class="captwiki-post-author__name">';

		if ( ! empty( $settings['before_name'] ) ) {
			echo '<span class="captwiki-post-author__prefix">' . esc_html( $settings['before_name'] ) . '</span> ';
		}

		if ( 'yes' === $settings['link_name'] ) {
			echo '<a href="' . esc_url( $archive_url ) . '">' . esc_html( $name ) . '</a>';
		} else {
			echo esc_html( $name );
		}

		echo '</div>';

		if ( 'yes' === $settings['show_bio'] && ! empty( $bio ) ) {
			echo '<div class="captwiki-post-author__bio">' . esc_html( $bio ) . '</div>';
		}

		if ( 'yes' === $settings['show_website'] && ! empty( $website ) ) {
			echo '<a class="captwiki-post-author__website" href="' . esc_url( $website ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $settings['website_text'] ) . '</a>';
		}

		echo '</div>';
		echo '</div>';
	}
}
