<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Radial_Progress extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-radial-progress',
			'title'          => 'Sifency Radial Progress',
			'icon'           => 'eicon-counter-circle',
			'category'       => array( 'sifency-business-widgets' ),
			'category'       => 'sifency-business-widgets',
			'script_depends' => array( 'sifency-radial-progressbar-script' ),
			'style_depends'  => array( 'sifency-radial-progressbar-style', 'sifency-typo-style' ),
			'keywords'       => array( 'radial', 'progress', 'circle', 'percentage', 'skill' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Radial Items', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'standard',
				'options' => array(
					'standard' => '1. Standard',
					'filled'   => '2. Filled Circle',
					'gradient' => '3. Gradient Glow',
					'semi'     => '4. Semi-Circle',
					'dashed'   => '5. Dashed',
				),
			)
		);
		$this->add_responsive_control(
			'columns',
			array(
				'label'     => esc_html__( 'Columns', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '4',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-rp-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);',
				),
			)
		);
		$this->add_responsive_control(
			'grid_gap',
			array(
				'label'     => esc_html__( 'Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 30 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-rp-grid' => 'gap: {{SIZE}}px;',
				),
			)
		);

		// --- REPEATER START ---
		$repeater = new Repeater();
		$repeater->add_control(
			'percent',
			array(
				'label'   => esc_html__( 'Percentage (%)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 0,
				'max'     => 100,
				'default' => 75,
			)
		);
		$repeater->add_control(
			'inner_content_type',
			array(
				'label'   => 'Inner Content',
				'type'    => Controls_Manager::SELECT,
				'default' => 'percent',
				'options' => array(
					'percent' => 'Percent',
					'icon'    => 'Icon',
					'text'    => 'Text',
				),
			)
		);
		$repeater->add_control(
			'custom_text',
			array(
				'label'     => 'Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Goal',
				'condition' => array( 'inner_content_type' => 'text' ),
			)
		);
		$repeater->add_control(
			'icon',
			array(
				'label'     => 'Icon',
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'inner_content_type' => 'icon' ),
			)
		);

		$repeater->add_control(
			'title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Project',
			)
		);
		$repeater->add_control(
			'subtitle',
			array(
				'label'   => 'Subtitle',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Description here',
			)
		);

		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'percent' => 25,
						'title'   => 'Design',
					),
					array(
						'percent' => 50,
						'title'   => 'Development',
					),
					array(
						'percent' => 85,
						'title'   => 'SEO',
					),
				),
				'title_field' => '{{{ title }}} ({{{ percent }}}%)',
			)
		);
		$this->add_control(
			'duration',
			array(
				'label'   => 'Animation Speed (ms)',
				'type'    => Controls_Manager::NUMBER,
				'default' => 1500,
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- STYLE: CIRCLE ---
		$this->start_controls_section(
			'style_circle',
			array(
				'label' => 'Circle Style',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'circle_size',
			array(
				'label'     => 'Size (px)',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 100,
						'max' => 500,
					),
				),
				'default'   => array( 'size' => 200 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-rp-svg'       => 'width: {{SIZE}}px; height: {{SIZE}}px;',
					'{{WRAPPER}} .sifency-rp-container' => 'width: {{SIZE}}px;',
				),
			)
		);

		$this->add_control(
			'stroke_width',
			array(
				'label'   => 'Stroke Width',
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min' => 1,
						'max' => 50,
					),
				),
				'default' => array( 'size' => 10 ),
			)
		);

		$this->add_control(
			'track_color',
			array(
				'label'     => 'Track Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#f1f1f1',
				'selectors' => array( '{{WRAPPER}} .sifency-rp-track' => 'stroke: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'bar_color',
			array(
				'label'     => 'Progress Color',
				'type'      => Controls_Manager::COLOR,
				'default'   => '#333333',
				'selectors' => array( '{{WRAPPER}} .sifency-rp-bar' => 'stroke: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'bar_cap',
			array(
				'label'     => 'Line Cap',
				'type'      => Controls_Manager::SELECT,
				'default'   => 'round',
				'options'   => array(
					'round'  => 'Round',
					'butt'   => 'Flat',
					'square' => 'Square',
				),
				'selectors' => array( '{{WRAPPER}} .sifency-rp-bar' => 'stroke-linecap: {{VALUE}};' ),
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_style',
				'label'    => 'Item Style',
				'selector' => '.sifency-rp-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Title Style',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'tabs', 'transition', 'border', 'radius', 'shadow', 'stroke', 'bg' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style',
				'label'          => 'Content Style',
				'selector'       => '.sifency-content-style',
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'alignment', 'transition', 'border', 'radius', 'shadow', 'stroke', 'bg' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'number_style',
				'label'    => 'Number Style',
				'selector' => '.sifency-number-style',
				'exclude'  => array( 'tabs', 'alignment', 'transition', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'label_style',
				'label'      => 'Precentage',
				'selector'   => '.sifency-label-style',
				'color_only' => true,
				'exclude'    => array( 'tabs', 'alignment', 'transition', 'border', 'radius', 'shadow', 'stroke', 'bg' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings                = $this->get_settings_for_display();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-radial-progress-wrapper' );
		$this->add_render_attribute( 'grid', 'class', array( 'sifency-rp-grid', 'sifency-rp-layout-' . $settings['layout_type'] ) );

		// Global settings passed to wrapper
		$js_settings = array(
			'duration' => $settings['duration'],
			'layout'   => $settings['layout_type'],
			'stroke'   => $settings['stroke_width']['size'] ?? 10,
		);
		$this->add_render_attribute( 'wrapper', 'data-rp-global', wp_json_encode( $js_settings ) );

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'grid' ) . $animation_wrapper_attrs ); ?>>
				
				<?php
				foreach ( $settings['items'] as $index => $item ) :
					$key                  = 'item_' . $index;
					$animation_item_attrs = $animation_manager->get_item_attributes();
					$this->add_render_attribute( $key, 'class', 'sifency-rp-item' );
					$this->add_render_attribute( $key, 'data-percent', $item['percent'] );
					?>
					<div <?php echo wp_kses_post( $this->get_render_attribute_string( $key ) . $animation_item_attrs ); ?>>
						
						<div class="sifency-rp-container">
							<svg class="sifency-rp-svg" viewBox="0 0 200 200">
								<defs>
									<linearGradient id="sifency-rp-grad-<?php echo esc_attr( $this->get_id() ); ?>" x1="0%" y1="0%" x2="100%" y2="0%">
										<stop offset="0%" stop-color="#00c6fb" />
										<stop offset="100%" stop-color="#005bea" />
									</linearGradient>
								</defs>

								<circle class="sifency-rp-track" cx="100" cy="100" r="90" fill="none" stroke-width="10"></circle>
								<circle class="sifency-rp-bar" cx="100" cy="100" r="90" fill="none" stroke-width="10" stroke-dasharray="565.48" stroke-dashoffset="565.48"></circle>
							</svg>

							<div class="sifency-rp-inner-content">
								<?php if ( 'percent' === $item['inner_content_type'] ) : ?>
									<span class="sifency-rp-value sifency-number-style">0</span><span class="sifency-rp-unit sifency-label-style">%</span>
								<?php elseif ( 'icon' === $item['inner_content_type'] ) : ?>
									<div class="sifency-rp-icon">
										<?php \Elementor\Icons_Manager::render_icon( $item['icon'], array( 'aria-hidden' => 'true' ) ); ?>
									</div>
								<?php else : ?>
									<span class="sifency-rp-text"><?php echo esc_html( $item['custom_text'] ); ?></span>
								<?php endif; ?>
							</div>
						</div>

						<div class="sifency-rp-meta">
							<h3 class="sifency-rp-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>
							<div class="sifency-rp-subtitle sifency-content-style"><?php echo esc_html( $item['subtitle'] ); ?></div>
						</div>

					</div>
				<?php endforeach; ?>

			</div>

		</div>
		<?php
	}
}
