<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Advanced_Particles extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency_advanced_particles',
			'title'          => 'Sifency Particles',
			'icon'           => 'eicon-sitemap',
			'category'       => 'sifency-business-widgets',
			'keywords'       => array( 'particles', 'background', 'animation', 'effect' ),
			'style_depends'  => array( 'sifency-particles-style' ),
			'script_depends' => array( 'particles', 'sifency-particles-script' ),
		);}
	protected function register_controls() {

		// --- GENERAL SETTINGS ---
		$this->start_controls_section(
			'section_target',
			array(
				'label' => esc_html__( 'Target & General', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'target_type',
			array(
				'label'   => esc_html__( 'Target Element', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'parent',
				'options' => array(
					'parent' => esc_html__( 'Parent Container (Section/Column)', 'sifency-addons' ),
					'custom' => esc_html__( 'Specific CSS ID', 'sifency-addons' ),
				),
			)
		);

		$this->add_control(
			'custom_id',
			array(
				'label'       => esc_html__( 'Target CSS ID', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'my-custom-id',
				'condition'   => array( 'target_type' => 'custom' ),
			)
		);
		$this->add_control(
			'parent_offset',
			array(
				'label'       => esc_html__( 'Parent Level', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 1,
				'max'         => 10,
				'step'        => 1,
				'default'     => 1,
				'description' => esc_html__( '1 = nearest container, 2 = parent of parent, etc.', 'sifency-addons' ),
				'condition'   => array(
					'target_type' => 'parent',
				),
			)
		);
		$this->end_controls_section();

		// --- LAYER CONTROLS HELPER FUNCTION ---
		$this->register_layer_controls( 'deep', 'Deep Layer (Back)' );
		$this->register_layer_controls( 'middle', 'Middle Layer (Center)' );
		$this->register_layer_controls( 'top', 'Top Layer (Front/Overlay)' );
		$this->add_sifency_addons_help_controls();
	}

	protected function register_layer_controls( $id, $label ) {
		$this->start_controls_section(
			'section_' . $id,
			array(
				'label' => esc_html( $label ),
			)
		);
		$this->add_control(
			'enable_' . $id,
			array(
				'label'   => esc_html__( 'Enable Layer', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => ( $id == 'deep' ? 'yes' : 'no' ),
			)
		);
		$this->add_control(
			'type_' . $id,
			array(
				'label'     => esc_html__( 'Config Type', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'preset',
				'options'   => array(
					'preset' => esc_html__( 'Modern Presets', 'sifency-addons' ),
					'json'   => esc_html__( 'Custom JSON Data', 'sifency-addons' ),
				),
				'condition' => array( 'enable_' . $id => 'yes' ),
			)
		);
		$this->add_control(
			'preset_' . $id,
			array(
				'label'     => esc_html__( 'Select Preset', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'nasa',
				'options'   => array(
					'nasa'      => 'NASA (Stars)',
					'bubbles'   => 'Floating Bubbles',
					'snow'      => 'Soft Snow',
					'lines'     => 'Network Lines',
					'polygon'   => 'Polygon Mask',
					'confetti'  => 'Confetti Storm',
					'fireflies' => 'Magic Fireflies',
					'bokeh'     => 'Blurred Bokeh',
					'parallax'  => 'Parallax Shapes',
					'fountain'  => 'Particle Fountain',
				),
				'condition' => array(
					'enable_' . $id => 'yes',
					'type_' . $id   => 'preset',
				),
			)
		);
		$this->add_control(
			'json_' . $id,
			array(
				'label'       => esc_html__( 'Particle.js JSON', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'rows'        => 10,
				'placeholder' => '{ "particles": { ... } }',
				'description' => 'Paste your JSON from particles.js.org or tsParticles.',
				'condition'   => array(
					'enable_' . $id => 'yes',
					'type_' . $id   => 'json',
				),
			)
		);
		$this->add_control(
			'opacity_' . $id,
			array(
				'label'     => esc_html__( 'Layer Opacity', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					),
				),
				'default'   => array( 'size' => 1 ),
				'selectors' => array( '{{WRAPPER}} .layer-' . $id => 'opacity: {{SIZE}};' ),
				'condition' => array( 'enable_' . $id => 'yes' ),
			)
		);
		$this->end_controls_section();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();
		$unique_id  = 'sifency-part-' . $this->get_id();

		$config = array(
			'target'    => $settings['target_type'],
			'target_id' => $settings['custom_id'],
			'layers'    => array(
				'deep'   => array(
					'enabled' => $settings['enable_deep'],
					'type'    => $settings['type_deep'],
					'preset'  => $settings['preset_deep'],
					'json'    => $settings['json_deep'],
				),
				'middle' => array(
					'enabled' => $settings['enable_middle'],
					'type'    => $settings['type_middle'],
					'preset'  => $settings['preset_middle'],
					'json'    => $settings['json_middle'],
				),
				'top'    => array(
					'enabled' => $settings['enable_top'],
					'type'    => $settings['type_top'],
					'preset'  => $settings['preset_top'],
					'json'    => $settings['json_top'],
				),
			),
		);
		?>
		<div id="<?php echo esc_attr( $unique_id, $wrapper_id ); ?>" 
			 class="sifency-advanced-particles-hold" 
			 data-config='<?php echo json_encode( $config ); ?>'>
		</div>
		<?php
	}
}
