<?php
/**
 * Widget Name: Post Title
 * Description: Display the current post, page, or archive title with Captain controls.
 *
 * @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_Text_Shadow;

/**
 * Class Captwiki_Post_Title
 */
class Captwiki_Post_Title extends Widget_Base {

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

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

	/**
	 * Get widget icon.
	 *
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-post-title 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 title', 'page title', 'archive title', 'dynamic title', 'captain' );
	}

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

	/**
	 * 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_title_content',
			array(
				'label' => esc_html__( 'Post Title Settings', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'title_source',
			array(
				'label'   => esc_html__( 'Title Source', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'single',
				'options' => array(
					'single'  => esc_html__( 'Current Post/Page', 'captain-widgets-kit' ),
					'archive' => esc_html__( 'Archive Title', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'before_text',
			array(
				'label'       => esc_html__( 'Before Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => esc_html__( 'e.g. Reading:', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'after_text',
			array(
				'label'       => esc_html__( 'After Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => esc_html__( 'e.g. - Latest Update', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'title_tag',
			array(
				'label'   => esc_html__( 'HTML Tag', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'h2',
				'options' => array(
					'h1'   => 'H1',
					'h2'   => 'H2',
					'h3'   => 'H3',
					'h4'   => 'H4',
					'h5'   => 'H5',
					'h6'   => 'H6',
					'div'  => 'div',
					'span' => 'span',
					'p'    => 'p',
				),
			)
		);

		$this->add_control(
			'link_title',
			array(
				'label'        => esc_html__( 'Link to Post', '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'      => '',
				'condition'    => array( 'title_source' => 'single' ),
			)
		);

		$this->add_control(
			'trim_words',
			array(
				'label'       => esc_html__( 'Limit Words', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 60,
				'step'        => 1,
				'default'     => 0,
				'description' => esc_html__( 'Use 0 to show the full title.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'fallback_text',
			array(
				'label'       => esc_html__( 'Fallback Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Untitled', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'Shown when no title is available', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_title_style',
			array(
				'label' => esc_html__( 'Title Style', '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-title' => 'text-align: {{VALUE}};',
				),
			)
		);

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

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

		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'title_shadow',
				'selector' => '{{WRAPPER}} .captwiki-post-title__heading',
			)
		);

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

		$this->end_controls_section();
	}

	/**
	 * Get the current post or archive title.
	 *
	 * @param array $settings Widget settings.
	 * @return string
	 */
	private function get_current_title( array $settings ): string {
		if ( 'archive' === $settings['title_source'] && ( is_archive() || is_home() || is_search() ) ) {
			$title = get_the_archive_title();
		} else {
			$title = get_the_title();
		}

		$title = wp_strip_all_tags( (string) $title );

		if ( '' === $title ) {
			$title = (string) $settings['fallback_text'];
		}

		if ( ! empty( $settings['trim_words'] ) ) {
			$title = wp_trim_words( $title, absint( $settings['trim_words'] ), '&hellip;' );
		}

		return trim( $title );
	}

	/**
	 * Render widget output.
	 *
	 * @return void
	 */
	protected function render(): void {
		$settings = $this->get_settings_for_display();
		$tag      = in_array( $settings['title_tag'], array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p' ), true ) ? $settings['title_tag'] : 'h2';
		$title    = $this->get_current_title( $settings );
		$output   = trim( $settings['before_text'] . ' ' . $title . ' ' . $settings['after_text'] );

		if ( '' === $output ) {
			return;
		}

		$this->add_render_attribute( 'wrapper', 'class', 'captwiki-post-title' );
		$this->add_render_attribute( 'heading', 'class', 'captwiki-post-title__heading' );

		echo '<div ' . wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ) . '>';
		echo '<' . esc_attr( $tag ) . ' ' . wp_kses_post( $this->get_render_attribute_string( 'heading' ) ) . '>';

		if ( 'yes' === $settings['link_title'] && 'single' === $settings['title_source'] ) {
			echo '<a href="' . esc_url( get_permalink() ) . '">';
			echo esc_html( $output );
			echo '</a>';
		} else {
			echo esc_html( $output );
		}

		echo '</' . esc_attr( $tag ) . '>';
		echo '</div>';
	}
}
