<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Physics_Tags extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-physics-tags',
			'title'          => 'Sifency Physics Tags (Matter.js)',
			'icon'           => 'eicon-tags',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'matter', 'sifency-physics-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'physics', 'tags', 'matter.js', 'gravity', 'collision', 'bounce' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Tag Elements', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'tag_text',
			array(
				'label'   => esc_html__( 'Tag Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Web Design', 'sifency-addons' ),
			)
		);

		$repeater->add_control(
			'tag_color',
			array(
				'label'   => esc_html__( 'Tag Background Color', 'sifency-addons' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#ff3c00',
			)
		);

		$repeater->add_control(
			'tag_text_color',
			array(
				'label'   => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#ffffff',
			)
		);

		$this->add_control(
			'physics_tags',
			array(
				'label'       => esc_html__( 'Physics Tags', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'tag_text'  => 'GSAP',
						'tag_color' => '#88CE02',
					),
					array(
						'tag_text'  => 'React',
						'tag_color' => '#61DAFB',
					),
					array(
						'tag_text'  => 'UI/UX',
						'tag_color' => '#FF0055',
					),
					array(
						'tag_text'  => 'Framer',
						'tag_color' => '#0055FF',
					),
					array(
						'tag_text'  => 'WebGL',
						'tag_color' => '#9900FF',
					),
					array(
						'tag_text'  => 'WordPress',
						'tag_color' => '#21759B',
					),
				),
				'title_field' => '{{{ tag_text }}}',
			)
		);

		$this->end_controls_section();

		// --- Physics Engine Settings ---
		$this->start_controls_section(
			'section_physics_engine',
			array( 'label' => esc_html__( 'Physics Engine Controls', 'sifency-addons' ) )
		);

		$this->add_control(
			'gravity_y',
			array(
				'label'       => esc_html__( 'Gravity (Y-Axis)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => -2,
						'max'  => 2,
						'step' => 0.1,
					),
				),
				'default'     => array( 'size' => 1 ),
				'description' => 'Positive = Falls down. Negative = Floats up. 0 = Zero Gravity.',
			)
		);

		$this->add_control(
			'restitution',
			array(
				'label'       => esc_html__( 'Bounciness (Restitution)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1.2,
						'step' => 0.1,
					),
				),
				'default'     => array( 'size' => 0.6 ),
				'description' => '0 = No bounce (Lead). 1 = Perfect bounce (Rubber ball).',
			)
		);

		$this->add_control(
			'friction_air',
			array(
				'label'       => esc_html__( 'Air Friction', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0,
						'max'  => 0.1,
						'step' => 0.01,
					),
				),
				'default'     => array( 'size' => 0.01 ),
				'description' => 'How quickly the tags slow down while moving through the air.',
			)
		);

		$this->add_control(
			'add_walls',
			array(
				'label'       => esc_html__( 'Contain Inside Box (Walls)', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'If disabled, tags will fall off the screen forever.',
			)
		);

		$this->end_controls_section();

		// --- Layout & Typography ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Container Layout', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'box_height',
			array(
				'label'      => esc_html__( 'Physics Box Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'vh', 'px' ),
				'range'      => array(
					'vh' => array(
						'min' => 30,
						'max' => 100,
					),
					'px' => array(
						'min' => 300,
						'max' => 1000,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 500,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-physics-wrapper' => 'height: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_control(
			'bg_color',
			array(
				'label'     => esc_html__( 'Box Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-physics-wrapper' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Tag Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-physics-tag' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				'default'    => array(
					'top'    => '50',
					'bottom' => '50',
					'left'   => '50',
					'right'  => '50',
					'unit'   => 'px',
				), // Pill shape
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-physics-tag',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'name'       => 'tag_',
				'label'      => esc_html__( 'Tag Typography', 'sifency-addons' ),
				'selector'   => '.sifency-physics-tag',
				'color_only' => true,
				'exclude'    => array( 'bg', 'tabs', 'transition', 'alignment', 'stroke', 'shadow' ),
			)
		);
	}

	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );
		$tags       = $settings['physics_tags'];

		if ( empty( $tags ) ) {
			return;
		}

		$config = array(
			'gravityY'    => $settings['gravity_y']['size'] ?? 1,
			'restitution' => $settings['restitution']['size'] ?? 0.6,
			'frictionAir' => $settings['friction_air']['size'] ?? 0.01,
			'walls'       => $settings['add_walls'] === 'yes' ? 'true' : 'false',
		);

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-physics-wrapper" data-config='<?php echo esc_attr( wp_json_encode( $config ) ); ?>'>
			<canvas class="sifency-physics-canvas"></canvas>
			<div class="sifency-physics-dom">
				<?php foreach ( $tags as $index => $tag ) : ?>
					<div class="sifency-physics-tag elementor-repeater-item-<?php echo esc_attr( $tag['_id'] ); ?>" 
						 data-index="<?php echo esc_attr( $index ); ?>"
						 style="background-color: <?php echo esc_attr( $tag['tag_color'] ); ?>; color: <?php echo esc_attr( $tag['tag_text_color'] ); ?>;">
						<?php echo esc_html( $tag['tag_text'] ); ?>
					</div>
				<?php endforeach; ?>
			</div>

		</div>
		<?php
	}
}
