<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Textloopcircle extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'textloopcircle';
    }

    public function get_title() {
        return esc_html__('Circle Text Loop', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-animation-text';
    }

    public function get_categories() {
        return ['sifency-addons-widgets'];
    }

    public function get_style_depends() {
	    return [ 'sifency-frontend-style' , 'sifency-icon', 'sifency-text-circle-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-frontend-script' ];
    }
    protected function register_controls() {
        // --- Content Section ---
        $this->start_controls_section( 'section_content', [ 'label' => 'Content' ] );
        $this->add_control(
            'circle_text',
            [ 'label' => 'Text', 'type' => Controls_Manager::TEXTAREA, 'default' => '• Your Text Here • Your Text Here ' ]
        );
        $this->add_control( 'center_icon', [ 'label' => 'Center Icon', 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'icon-right', 'library' => 'sifency-icons' ] ] );
        $this->add_control( 'link', [ 'label' => 'Link', 'type' => Controls_Manager::URL, 'default' => [ 'is_external' => 'true' ] ] );
        $this->end_controls_section();

        // --- Style Section ---
        $this->start_controls_section( 'section_style', [ 'label' => 'Style', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_responsive_control( 'size', [ 'label' => 'Size', 'type' => Controls_Manager::SLIDER, 'range' => [ 'px' => [ 'min' => 50, 'max' => 500 ] ], 'selectors' => [ '{{WRAPPER}} .sifency-circle-text' => 'width: {{SIZE}}px; height: {{SIZE}}px;' ] ]);
        $this->add_control( 'text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-circle-text__path' => 'fill: {{VALUE}};' ] ]);
        $this->add_control( 'text_color_hover', [ 'label' => 'Hover Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-circle-text__path:hover' => 'fill: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'typography', 'selector' => '{{WRAPPER}} .sifency-circle-text__path' ] );
        $this->add_control( 'icon_color', [ 'label' => 'Icon Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-circle-text__icon' => 'color: {{VALUE}};' ], 'separator' => 'before' ]);
        $this->add_responsive_control( 'icon_size', [ 'label' => 'Icon Size', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-circle-text__icon i' => 'font-size: {{SIZE}}px;' ] ]);
        $this->end_controls_section();

        // --- Animation Section ---
        $this->start_controls_section( 'section_animation', [ 'label' => 'Animation', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_control(
            'rotation_speed',
            [ 'label' => 'Speed (seconds per rotation)', 'type' => Controls_Manager::SLIDER, 'range' => [ 's' => [ 'min' => 1, 'max' => 100 ] ], 'default' => [ 'size' => 20 ], 'selectors' => [ '{{WRAPPER}} .sifency-circle-text__svg' => 'animation-duration: {{SIZE}}s;' ] ]
        );
        $this->add_control(
            'rotation_direction',
            [ 'label' => 'Direction', 'type' => Controls_Manager::SELECT, 'default' => 'normal', 'options' => [ 'normal' => 'Clockwise', 'reverse' => 'Counter-Clockwise' ], 'selectors' => [ '{{WRAPPER}} .sifency-circle-text__svg' => 'animation-direction: {{VALUE}};' ] ]
        );
        $this->add_control(
            'pause_on_hover',
            [ 'label' => 'Pause on Hover', 'type' => Controls_Manager::SWITCHER, 'prefix_class' => 'sifency-circle--hover-' ]
        );
        $this->end_controls_section();

        $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();
        $unique_id = 'circle-path-' . $this->get_id();
        $animation_manager = new Sifency_Animation_Manager($settings);
        if ( ! empty($settings['link']['url']) ) {
            $this->add_link_attributes('link', $settings['link']);
            $tag = 'a';
        } else {
            $tag = 'div';
        }
        $wrapper_classes = ['sifency-circle-text'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <<?php echo esc_html($tag); ?> <?php echo wp_kses_post ($this->get_render_attribute_string('link') . $wrapper_attrs); ?>>
            <svg class="sifency-circle-text__svg" viewBox="0 0 100 100">
                <path id="<?php echo esc_attr($unique_id); ?>" d="M 5,50 a 45,45 0 1,1 0,1 z" fill="none"/>

                <text>
                    <textPath class="sifency-circle-text__path sifency-content-style" href="#<?php echo esc_attr($unique_id); ?>" startOffset="50%" text-anchor="middle">
                        <?php echo esc_html($settings['circle_text']); ?>
                    </textPath>
                </text>
            </svg>
            <?php if ( ! empty($settings['center_icon']['value']) ) : ?>
                <div class="sifency-circle-text__icon">
                    <?php \Elementor\Icons_Manager::render_icon($settings['center_icon']); ?>
                </div>
            <?php endif; ?>
        </<?php echo esc_html($tag); ?>>
        <?php
    }
}