<?php
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;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Utils;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Blockquote extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Core_Text_Style;
    use Sifency_Icon_Style_Controls;

    public function get_name() {
        return 'sifency-addons-block-quote';
    }
    
    public function get_title() {
        return esc_html__('Sifency Blockquote', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-blockquote';
    }
    
    public function get_categories() {
        return ['sifency-addons-widgets'];
    }

    public function get_keywords() {
        return ['quote', 'testimonial', 'citation', 'blockquote', 'author'];
    }

    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-typo-style' , 'sifency-icon' ];
    }
    
    protected function register_controls() {
        // --- Content Section ---
        $this->start_controls_section(
            'content_section',
            [ 'label' => __('Content', 'sifency-addons') ]
        );
        $this->add_control(
            'quote_content',
            [
                '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' => [ 'active' => true ],
            ]
        );
        $this->add_control(
            'author_name',
            [
                'label' => __('Author Name', 'sifency-addons'),
                'type' => Controls_Manager::TEXT,
                'default' => __('John Doe', 'sifency-addons'),
                'separator' => 'before',
                'dynamic' => [ 'active' => true ],
            ]
        );
        $this->add_control(
            'author_image',
            [ 'label' => __('Author Image', 'sifency-addons'), 'type' => Controls_Manager::MEDIA ]
        );
        $this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[ 'name' => 'author_image', 'default' => 'thumbnail' ]
		);
        $this->add_control(
            'link',
            [ 'label' => __('Link', 'sifency-addons'), 'type' => Controls_Manager::URL ]
        );
        $this->end_controls_section();

        // --- Layout Section ---
        $this->start_controls_section(
            'layout_section',
            [ 'label' => __('Layout', 'sifency-addons') ]
        );
        $this->add_responsive_control(
            'blockquote_align',
            [
                'label' => esc_html__( 'Alignment', 'sifency-addons' ),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
					'left' => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
					'center' => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-h-align-center' ],
					'right' => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
				],
                'default' => 'left',
                'selectors' => [ '{{WRAPPER}} .sifency-blockquote' => 'text-align: {{VALUE}};' ],
            ]
        );
        $this->add_control(
			'quote_icon',
			[
				'label' => __( 'Quote Icon', 'sifency-addons' ),
				'type' => Controls_Manager::ICONS,
				'default' => [
					'value' => 'icon-quote-right-1',
					'library' => 'sifency-icons',
				],
			]
		);
        $this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Author Name',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Author Bio',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_icon_style_controls();
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                '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_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 = ['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' ), [] );
                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'], [ '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' ), [] ); ?>>
                    <?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 wp_kses_post ( \Elementor\Group_Control_Image_Size::get_attachment_image_html( $settings, 'author_image' ) ); 
                                ?>
                            </div>
                        <?php endif; ?>

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

                    </div>
                <?php endif; ?>

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

        <?php
    }
}