<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Textloopcircle extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'textloopcircle',
			'title'          => 'Circle Text Loop',
			'icon'           => 'eicon-animation-text',
			'category'       => 'sifency-typography-widgets',
			'keywords'       => array( 'circle', 'text', 'loop', 'animation' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-text-circle-style', 'sifency-icon' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section( 'section_content', array( 'label' => 'Content' ) );
		$this->add_control(
			'circle_text',
			array(
				'label'   => 'Text',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => '• Your Text Here • Your Text Here ',
			)
		);
		$this->add_control(
			'center_icon',
			array(
				'label'   => 'Center Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-right',
					'library' => 'sifency-icons',
				),
			)
		);
		$this->add_control(
			'link',
			array(
				'label'   => 'Link',
				'type'    => Controls_Manager::URL,
				'default' => array( 'is_external' => 'true' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => 'Style',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'size',
			array(
				'label'     => 'Size',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 50,
						'max' => 500,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-circle-text' => 'width: {{SIZE}}px; height: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'text_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-circle-text__path' => 'fill: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'text_color_hover',
			array(
				'label'     => 'Hover Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-circle-text__path:hover' => 'fill: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-circle-text__path',
			)
		);
		$this->end_controls_section();

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

		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector'        => '.sifency-circle-text__icon i, .sifency-circle-text__icon svg',
				'show_dimensions' => true,
				'hover_selector'  => '.sifency-circle-text:hover .sifency-circle-text__icon i, .sifency-circle-text:hover .sifency-circle-text__icon svg',
			)
		);
		$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();
		$unique_id         = 'circle-path-' . $this->get_id();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'link', $settings['link'] );
			$tag = 'a';
		} else {
			$tag = 'div';
		}
		$wrapper_classes = array( '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
	}
}
