<?php
/**
 * Widget Name: Post Content
 * Description: Display post content or excerpt 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_Border;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Box_Shadow;

/**
 * Class Captwiki_Post_Content
 */
class Captwiki_Post_Content extends Widget_Base {

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

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

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

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

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

		$this->add_control(
			'content_type',
			array(
				'label'   => esc_html__( 'Content Type', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'full',
				'options' => array(
					'full'    => esc_html__( 'Full Content', 'captain-widgets-kit' ),
					'excerpt' => esc_html__( 'Excerpt', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'excerpt_length',
			array(
				'label'     => esc_html__( 'Excerpt Words', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 5,
				'max'       => 120,
				'step'      => 1,
				'default'   => 25,
				'condition' => array( 'content_type' => 'excerpt' ),
			)
		);

		$this->add_control(
			'show_read_more',
			array(
				'label'        => esc_html__( 'Show Read More', '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( 'content_type' => 'excerpt' ),
			)
		);

		$this->add_control(
			'read_more_text',
			array(
				'label'     => esc_html__( 'Read More Text', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Read More', 'captain-widgets-kit' ),
				'dynamic'   => array( 'active' => true ),
				'condition' => array(
					'content_type'   => 'excerpt',
					'show_read_more' => 'yes',
				),
			)
		);

		$this->add_control(
			'fallback_text',
			array(
				'label'       => esc_html__( 'Fallback Content', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'No content available.', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'Shown when post content is empty', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->end_controls_section();

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

		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Text 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-content' => 'text-align: {{VALUE}};',
				),
			)
		);

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

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

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

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

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

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

		$this->end_controls_section();

		$this->start_controls_section(
			'section_read_more_style',
			array(
				'label'     => esc_html__( 'Read More', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'content_type'   => 'excerpt',
					'show_read_more' => 'yes',
				),
			)
		);

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

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

		$this->end_controls_section();
	}

	/**
	 * Get post content or excerpt output.
	 *
	 * @param array $settings Widget settings.
	 * @return string
	 */
	private function get_content_output( array $settings ): string {
		$post_id = get_the_ID();

		if ( ! $post_id ) {
			return wp_kses_post( $settings['fallback_text'] );
		}

		if ( 'excerpt' === $settings['content_type'] ) {
			$excerpt = has_excerpt( $post_id ) ? get_the_excerpt( $post_id ) : get_post_field( 'post_content', $post_id );
			$excerpt = wp_strip_all_tags( strip_shortcodes( (string) $excerpt ) );
			$excerpt = wp_trim_words( $excerpt, absint( $settings['excerpt_length'] ), '&hellip;' );

			return '' !== $excerpt ? esc_html( $excerpt ) : wp_kses_post( $settings['fallback_text'] );
		}

		$content = get_post_field( 'post_content', $post_id );
		$content = apply_filters( 'the_content', $content );

		return '' !== trim( wp_strip_all_tags( $content ) ) ? $content : wpautop( wp_kses_post( $settings['fallback_text'] ) );
	}

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

		echo '<div class="captwiki-post-content">';
		echo wp_kses_post( $content );

		if ( 'excerpt' === $settings['content_type'] && 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_text'] ) ) {
			echo '<p class="captwiki-post-content__read-more-wrap"><a class="captwiki-post-content__read-more" href="' . esc_url( get_permalink() ) . '">' . esc_html( $settings['read_more_text'] ) . '</a></p>';
		}

		echo '</div>';
	}
}
