<?php
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_Image_Size;
use \Elementor\Utils;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Blockquote extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-addons-block-quote',
			'title'         => 'Sifency Blockquote',
			'icon'          => 'eicon-blockquote',
			'category'      => 'sifency-addons-widgets',
			'keywords'      => array( 'quote', 'blockquote', 'testimonial', 'text' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-typo-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section(
			'content_section',
			array( 'label' => __( 'Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'quote_content',
			array(
				'label'   => __( 'Quote Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'sifency-addons' ),
				'dynamic' => array( 'active' => true ),
			)
		);
		$this->add_control(
			'author_name',
			array(
				'label'     => __( 'Author Name', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => __( 'John Doe', 'sifency-addons' ),
				'separator' => 'before',
				'dynamic'   => array( 'active' => true ),
			)
		);
		$this->add_control(
			'author_image',
			array(
				'label' => __( 'Author Image', 'sifency-addons' ),
				'type'  => Controls_Manager::MEDIA,
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'author_image',
				'default' => 'thumbnail',
			)
		);
		$this->add_control(
			'link',
			array(
				'label' => __( 'Link', 'sifency-addons' ),
				'type'  => Controls_Manager::URL,
			)
		);
		$this->end_controls_section();

		// --- Layout Section ---
		$this->start_controls_section(
			'layout_section',
			array( 'label' => __( 'Layout', 'sifency-addons' ) )
		);
		$this->add_responsive_control(
			'blockquote_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'left',
				'selectors' => array( '{{WRAPPER}} .sifency-blockquote' => 'text-align: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'quote_icon',
			array(
				'label'   => __( 'Quote Icon', 'sifency-addons' ),
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-quote-right-1',
					'library' => 'sifency-icons',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Quote Text',
				'selector'   => '.sifency-content-style',
				'color_only' => false,
				'condition'  => array( 'quote_content!' => '' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'heading_style_',
				'label'      => 'Author Name',
				'selector'   => '.sifency-heading-style',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'alignment', 'bg', 'radius', 'stroke', 'shadow' ),
				'condition'  => array( 'author_name!' => '' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls( array( 'condition' => array( 'quote_icon[value]!' => '' ) ) );
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'     => '.sifency-blockquote__author-image img',
				'condition'    => array( 'author_image!' => '' ),
				'show_overlay' => false,
				'exclude'      => array(
					'opacity',
					'filters',
					'object_fit',
					'transition',
					'tabs',
				),
			),
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render() {

		$settings = $this->get_settings_for_display();

		if ( empty( $settings['quote_content'] ) ) {
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		// Inline editing attributes
		$this->add_inline_editing_attributes( 'quote_content', 'basic' );
		$this->add_inline_editing_attributes( 'author_name', 'none' );

		$has_link = ! empty( $settings['link']['url'] );
		$tag      = $has_link ? 'a' : 'div';

		if ( $has_link ) {
			$this->add_link_attributes( 'wrapper_link', $settings['link'] );
		}

		$wrapper_classes = array( 'sifency-blockquote' );
		if ( $has_link ) {
			$wrapper_classes[] = 'sifency-blockquote--linked';
		}

		// Animation attributes
		$wrapper_animation_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>

		<<?php echo tag_escape( $tag ); ?>
			<?php
				// wrapper_link attributes + animation attributes
				echo wp_kses( $this->get_render_attribute_string( 'wrapper_link' ), array() );
				echo ' ' . wp_kses_post( $wrapper_animation_attrs );
			?>
		>
			<div class="sifency-blockquote__inner">

				<?php if ( ! empty( $settings['quote_icon']['value'] ) ) : ?>
					<span class="sifency-blockquote__icon sifency-icon-style">
						<?php \Elementor\Icons_Manager::render_icon( $settings['quote_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</span>
				<?php endif; ?>

				<blockquote class="sifency-blockquote__content sifency-content-style"
					<?php echo wp_kses( $this->get_render_attribute_string( 'quote_content' ), array() ); ?>>
					<?php echo wp_kses_post( $settings['quote_content'] ); ?>
				</blockquote>

				<?php if ( ! empty( $settings['author_name'] ) ) : ?>
					<div class="sifency-blockquote__footer">

						<?php if ( ! empty( $settings['author_image']['url'] ) ) : ?>
							<div class="sifency-blockquote__author-image">
								<?php
								echo \Elementor\Group_Control_Image_Size::get_attachment_image_html( $settings, 'author_image' );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
								?>
							</div>
						<?php endif; ?>

						<cite class="sifency-blockquote__author-name sifency-heading-style"
							<?php echo wp_kses( $this->get_render_attribute_string( 'author_name' ), array() ); ?>>
							<?php echo esc_html( $settings['author_name'] ); ?>
						</cite>

					</div>
				<?php endif; ?>

			</div>
		</<?php echo tag_escape( $tag ); ?>>

		<?php
	}
}
