<?php
/**
 * Widget Name: Text Block
 * Description: Customizable Text Block with 8 dynamic styles (2026 edition).
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

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

/**
 * Class Captwiki_Text_Block
 *
 * @since 1.0.0
 */
class Captwiki_Text_Block extends Widget_Base {

	/**
	 * Get Widget Name.
	 *
	 * @since 1.0.0
	 */
	public function get_name() {
		return 'captwiki-text-block';
	}

	/**
	 * Get Widget Title.
	 *
	 * @since 1.0.0
	 */
	public function get_title() {
		return esc_html__( 'Text Block', 'captain-widgets-kit' );
	}

	/**
	 * Get Widget Icon.
	 *
	 * @since 1.0.0
	 */
	public function get_icon() {
		return 'eicon-atomic-label captwiki-el-logo';
	}

	/**
	 * Get Widget Categories.
	 *
	 * @since 1.0.0
	 */
	public function get_categories() {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get Widget Keywords.
	 *
	 * @since 1.0.0
	 */
	public function get_keywords() {
		return array( 'text', 'block', 'content', 'seo', 'article', 'highlight' );
	}

	/**
	 * Disable Elementor's default inner wrapper for custom HTML control.
	 *
	 * @since 1.0.0
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Register Controls.
	 *
	 * @since 1.0.0
	 */
	protected function register_controls() {

		/* ── Content Section ─────────────────────────────────── */
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Content', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'style_type',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style1',
				'options' => array(
					/* ── Original 4 styles ── */
					'style1' => esc_html__( 'Style 1 — Left Accent Border', 'captain-widgets-kit' ),
					'style2' => esc_html__( 'Style 2 — Center Card', 'captain-widgets-kit' ),
					'style3' => esc_html__( 'Style 3 — Warm Italic Quote', 'captain-widgets-kit' ),
					'style4' => esc_html__( 'Style 4 — Dark Gradient', 'captain-widgets-kit' ),
					/* ── New 2026 styles ── */
					'style5' => esc_html__( 'Style 5 — Glassmorphism', 'captain-widgets-kit' ),
					'style6' => esc_html__( 'Style 6 — Editorial Serif', 'captain-widgets-kit' ),
					'style7' => esc_html__( 'Style 7 — Neon Ink (Dark)', 'captain-widgets-kit' ),
					'style8' => esc_html__( 'Style 8 — Minimal Luxury', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'text_content',
			array(
				'label'   => esc_html__( 'Text Content', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => esc_html__( 'This is an advanced text block with multiple styles.', 'captain-widgets-kit' ),
			)
		);

		/* ── SEO / Accessibility Options ─────────────────────── */
		$this->add_control(
			'seo_heading',
			array(
				'label'     => esc_html__( 'SEO Options', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'semantic_tag',
			array(
				'label'   => esc_html__( 'Semantic Wrapper Tag', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'div',
				'options' => array(
					'div'     => 'div (default)',
					'section' => 'section',
					'article' => 'article',
					'aside'   => 'aside',
					'p'       => 'p',
				),
			)
		);

		$this->add_control(
			'aria_label',
			array(
				'label'       => esc_html__( 'ARIA Label (Accessibility)', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'e.g. Introduction text', 'captain-widgets-kit' ),
				'description' => esc_html__( 'Adds aria-label for screen readers. Leave empty to skip.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();

		/* ── Style Section ───────────────────────────────────── */
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Style', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .captwiki-adv-text',
			)
		);

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

		$this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			array(
				'name'     => 'background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-adv-text',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'border',
				'selector' => '{{WRAPPER}} .captwiki-adv-text',
			)
		);

		$this->add_responsive_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-adv-text' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

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

		$this->add_control(
			'accent_color',
			array(
				'label'       => esc_html__( 'Accent Color', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::COLOR,
				'description' => esc_html__( 'Used by Style 5–8 for borders, glows and decorative lines.', 'captain-widgets-kit' ),
				'selectors'   => array(
					'{{WRAPPER}} .captwiki-adv-text' => '--ctb-accent: {{VALUE}};',
				),
				'separator'   => 'before',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render Widget Output.
	 *
	 * @since 1.0.0
	 */
	protected function render() {
		$settings     = $this->get_settings_for_display();
		$style_type   = ! empty( $settings['style_type'] ) ? $settings['style_type'] : 'style1';
		$text_content = ! empty( $settings['text_content'] ) ? $settings['text_content'] : '';
		$tag          = ! empty( $settings['semantic_tag'] ) ? $settings['semantic_tag'] : 'div';
		$aria_label   = ! empty( $settings['aria_label'] ) ? $settings['aria_label'] : '';

		/* Allowed tags for the semantic wrapper. */
		$allowed_tags = array( 'div', 'section', 'article', 'aside', 'p' );
		if ( ! in_array( $tag, $allowed_tags, true ) ) {
			$tag = 'div';
		}

		$aria_attr = $aria_label ? ' aria-label="' . esc_attr( $aria_label ) . '"' : '';

		echo '<' . esc_attr( $tag ) . ' class="captwiki-adv-text ' . esc_attr( $style_type ) . '"' . $aria_attr . '>';
		echo wp_kses_post( $text_content );
		echo '</' . esc_attr( $tag ) . '>';
	}
}
