<?php
namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly

class Rivax_Elementor_Particles_Background {

    /**
     * A reference to an instance of this class.
     */
    private static $instance = null;


    public function __construct(){

        add_action( 'elementor/element/container/section_background_overlay/after_section_end', array( $this, 'container_settings' ) );
        add_action( 'elementor/frontend/container/before_render',  array( $this, 'container_before_render' ) );
        add_action( 'elementor/container/print_template',  array( $this, 'container_editor_render' ), 10, 2 );


    }


    /**
     * Returns the instance.
     *
     * @since  1.0.0
     * @access public
     * @return object
     */
    public static function get_instance() {
        // If the single instance hasn't been set, set it now.
        if ( null == self::$instance ) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function container_settings ( $element ) {

        $element->start_controls_section(
            'rivax_container_particles_section',
            array(
                'label' => esc_html__( 'Particles Background', 'newsfy' ),
                'tab'   => Controls_Manager::TAB_STYLE,
            )
        );

        $element->add_control(
            'rivax_particles_enable',
            [
                'label'        => esc_html__( 'Particles Background', 'newsfy' ),
                'type'         => Controls_Manager::SWITCHER,
                'render_type'  => 'template',
				'assets' => [
	                'scripts' => [
		                [
			                'name' => 'rivax-particles',
			                'conditions' => [
				                'terms' => [
					                [
						                'name' => 'rivax_particles_enable',
						                'operator' => '!==',
						                'value' => '',
					                ],
				                ],
			                ],
		                ],
	                ],
                ],
            ]
        );

        $element->add_control(
            'rivax_particles_zindex',
            [
                'label'              => esc_html__('Z-index', 'newsfy' ),
                'type'               => Controls_Manager::NUMBER,
                'selectors' => [
                    '{{WRAPPER}} .rivax-particles-wrapper' => 'z-index: {{VALUE}};',
                ],
                'condition'          => [
                    'rivax_particles_enable' => 'yes',
                ],
            ]
        );

        $element->add_control(
            'rivax_particles_opacity',
            [
                'label'     => esc_html__( 'Opacity', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'range'     => [
                    'px' => [
                        'min' => 0,
                        'max' => 1,
                        'step' => 0.1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .rivax-particles-wrapper' => 'opacity: {{SIZE}};',
                ],
                'condition'          => [
                    'rivax_particles_enable' => 'yes',
                ],
            ]
        );

        $element->add_control(
            'rivax_particles_json',
            [
                'type'    => Controls_Manager::TEXTAREA,
                'rows' => 10,
                'label'   => esc_html__('Add Particle Json', 'newsfy' ),
                'description'       => sprintf(esc_html__('%sClick here%s to generate particle JSON. %sSamples%s', 'newsfy'), '<a href="' . esc_url('https://vincentgarreau.com/particles.js/') . '" target="_blank">', '</a>', '<a href="' . esc_url(RIVAX_THEME_URI . '/assets/js/particles-sample.txt') . '" target="_blank">', '</a>'),
                'render_type' => 'template',
                'condition' => [
                    'rivax_particles_enable' => 'yes'
                ]
            ]
        );

        $element->end_controls_section();

    }



    /**
     * Before container render callback.
     *
     * @param object $element
     *
     * @return void
     */
    public function container_before_render( $element ) {
        $data     = $element->get_data();
        $settings = $data['settings'];

        if ( !empty($settings['rivax_particles_enable']) && !empty($settings['rivax_particles_json']) ) {

            wp_enqueue_script('rivax-particles');
            $element->add_render_attribute( '_wrapper', array(
                'class' => 'rivax-particles',
                'data-rivax-particles' => esc_attr(preg_replace('/\s+/', ' ',$settings['rivax_particles_json'])),
            ) );
        }
    }


    public function container_editor_render($template, $widget) {
        if ($widget->get_name() != 'container') {
            return $template;
        }
        ob_start();
        ?>
        <# if ( settings.rivax_particles_enable && settings.rivax_particles_json ) { #>
        <div class="rivax-particles-wrapper" id="rivax-particles-{{ view.getID() }}" data-rivax-particles="{{ settings.rivax_particles_json }}"></div>
        <# } #>
        <?php
        $particle_content = ob_get_contents();
        ob_end_clean();
        return $particle_content . $template;
    }

}

Rivax_Elementor_Particles_Background::get_instance();
