<?php
/**
 * Class: Eac_Injection_Lottie
 *
 * Description: Injecte la section et les controls dans les Colonnes
 * après la section 'Motion effects' sous l'onglet 'Advanced'
 *
 * @since 1.9.3
 */

namespace EACCustomWidgets\Includes\Elementor\Injection;

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

use EACCustomWidgets\EAC_Plugin;
use EACCustomWidgets\Core\Eac_Load_Config;

use Elementor\Controls_Manager;
use Elementor\Element_Base;

class Eac_Injection_Lottie {

	/**
	 * @var $target_elements
	 *
	 * La liste des éléments cibles
	 */
	private $target_elements = array( 'column', 'container' );

	/**
	 * Constructeur de la class
	 */
	public function __construct() {
		add_action( 'elementor/element/after_section_end', array( $this, 'inject_section' ), 10, 2 );

		add_filter( 'elementor/column/print_template', array( $this, 'print_template' ), 10, 2 );
		add_filter( 'elementor/container/print_template', array( $this, 'print_template' ), 10, 2 );

		add_action( 'elementor/frontend/column/before_render', array( $this, 'render_lottie' ) );
		add_action( 'elementor/frontend/container/before_render', array( $this, 'render_lottie' ) );

		/** Chargement de jQuery */
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_jquery' ) );
		add_action( 'elementor/frontend/after_enqueue_scripts', array( $this, 'enqueue_scripts' ), 16 );
	}

	/**
	 * enqueue_scripts_jquery
	 *
	 * Check si Jquery est chargé sinon charge la lib
	 */
	public function enqueue_scripts_jquery() {
		if ( ! wp_script_is( 'jquery', 'enqueued' ) ) {
			wp_enqueue_script( 'jquery' );
		}
	}

	/**
	 * enqueue_scripts
	 *
	 * Mets le script dans le file
	 */
	public function enqueue_scripts() {
		wp_enqueue_script( 'lottie-animation' );

		wp_enqueue_script( 'eac-lottie-anim', EAC_Plugin::instance()->get_script_url( 'assets/js/elementor/eac-lottie-animations' ), array( 'jquery', 'lottie-animation', 'elementor-frontend' ), EAC_PLUGIN_VERSION, true );
		wp_enqueue_style( 'eac-lottie-anim', EAC_Plugin::instance()->get_style_url( 'assets/css/lottie-animations' ), array(), EAC_PLUGIN_VERSION );
	}

	/**
	 * inject_section
	 *
	 * Inject le control après la section 'section_effects' Advanced tab
	 * pour les colonnes
	 *
	 * @param Element_Base $element    The edited element.
	 * @param String       $section_id L'ID de la section
	 * @param array        $args       Section arguments.
	 */
	public function inject_section( $element, $section_id ) {

		if ( ! $element instanceof Element_Base ) {
			return;
		}

		if ( 'section_effects' === $section_id && in_array( $element->get_name(), $this->target_elements, true ) ) {
			$element->start_controls_section(
				'eac_custom_element_lottie',
				array(
					'label' => esc_html__( 'Lottie background', 'eac-components' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

				$element->add_control(
					'eac_element_lottie',
					array(
						'label'        => esc_html__( 'Lottie animation', 'eac-components' ),
						'type'         => Controls_Manager::SWITCHER,
						'label_on'     => esc_html( 'On' ),
						'label_off'    => esc_html( 'Off' ),
						'return_value' => 'yes',
						'default'      => '',
					)
				);

				$element->add_control(
					'eac_element_lottie_usage',
					array(
						'type' => Controls_Manager::RAW_HTML,
						'raw'  => sprintf(
							'%1$s%2$s%3$s',
							'<a href="https://elementor-addon-components.com/add-lottie-animation-in-elementor/#use-lottie-animations-in-the-background-of-an-element" target="_blank" rel="noopener noreferrer">',
							esc_html__( 'Consult the documentation', 'eac-components' ),
							'</a>'
						),
						'content_classes' => 'elementor-descriptor',
						'condition'       => array( 'eac_element_lottie' => 'yes' ),
					)
				);

				$element->add_control(
					'eac_element_lottie_is_json_enable',
					array(
						'label'        => 'JSON URL',
						'type'         => Controls_Manager::HIDDEN,
						'default'      => \EACCustomWidgets\Core\Utils\Eac_Tools_Util::is_json_enable() ? 'true' : 'false',
						'save_default' => false,
						'condition'    => array( 'eac_element_lottie' => 'yes' ),
					)
				);

				$element->add_control(
					'eac_element_lottie_source',
					array(
						'label'     => esc_html__( 'Origine', 'eac-components' ),
						'type'      => Controls_Manager::CHOOSE,
						'options'   => array(
							'file' => array(
								'title' => esc_html__( 'Media file', 'eac-components' ),
								'icon'  => 'eicon-document-file',
							),
							'url'  => array(
								'title' => esc_html( 'URL' ),
								'icon'  => 'eicon-link',
							),
						),
						'default'   => 'file',
						'condition' => array( 'eac_element_lottie' => 'yes' ),
					)
				);

				$element->add_control(
					'eac_element_lottie_media_file',
					array(
						'label'        => esc_html__( 'Select file', 'eac-components' ),
						'type'         => 'FILE_VIEWER',
						'library_type' => array( 'application/json' ), // propriété utilisée par le script 'eac-file-viewer-control.js'
						'description'  => esc_html__( 'Select the file from the media library', 'eac-components' ),
						'condition'    => array(
							'eac_element_lottie'        => 'yes',
							'eac_element_lottie_source' => 'file',
						),
					)
				);

				$element->add_control(
					'eac_element_lottie_media_url',
					array(
						'label'       => esc_html( 'URL' ),
						'type'        => Controls_Manager::URL,
						'description' => esc_html__( "URL animation <a href='https://lottiefiles.com/' target='_blank' rel='nofollow noopener noreferrer'>here</a>", 'eac-components' ),
						'placeholder' => 'https://lottiefiles.com/anim.json/',
						'dynamic'     => array(
							'active' => true,
						),
						'condition'   => array(
							'eac_element_lottie'                => 'yes',
							'eac_element_lottie_source'         => 'url',
							'eac_element_lottie_is_json_enable' => 'true',
						),
					)
				);

				$element->add_control(
					'eac_element_lottie_media_url_info',
					array(
						'type'            => Controls_Manager::RAW_HTML,
						'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
						'raw'             => esc_html__( "Elementor/Advanced settings, activate option 'Enable Unfiltered File Uploads'", 'eac-components' ),
						'condition'   => array(
							'eac_element_lottie'                => 'yes',
							'eac_element_lottie_source'         => 'url',
							'eac_element_lottie_is_json_enable' => 'false',
						),
					)
				);

				$element->add_control(
					'eac_element_lottie_loop',
					array(
						'label'       => esc_html__( 'Loop', 'eac-components' ),
						'type'        => Controls_Manager::CHOOSE,
						'options'     => array(
							'yes' => array(
								'title' => esc_html__( 'Loop', 'eac-components' ),
								'icon'  => 'eicon-check',
							),
							'no'  => array(
								'title' => esc_html__( 'Only once', 'eac-components' ),
								'icon'  => 'eicon-ban',
							),
						),
						'default'     => 'yes',
						'toggle'      => false,
						'render_type' => 'template',
						'separator'   => 'before',
						'condition'   => array( 'eac_element_lottie' => 'yes' ),
					)
				);

				$element->add_control(
					'eac_element_lottie_speed',
					array(
						'label'     => esc_html__( 'Speed', 'eac-components' ),
						'type'      => Controls_Manager::NUMBER,
						'default'   => 1,
						'min'       => 0.1,
						'max'       => 3,
						'step'      => 0.1,
						'condition' => array( 'eac_element_lottie' => 'yes' ),
					)
				);

				/**$element->add_control(
					'eac_element_lottie_rotate',
					array(
						'label'     => esc_html( 'Rotation' ),
						'type'      => Controls_Manager::SLIDER,
						'default'   => array(
							'size' => 0,
							'unit' => 'px',
						),
						'range'     => array(
							'px' => array(
								'min'  => -180,
								'max'  => 180,
								'step' => 10,
							),
						),
						'selectors' => array( '{{WRAPPER}} .lottie-anim_wrapper.lottie-anim_wrapper-bg' => 'transform: rotate({{SIZE}}deg);' ),
						'condition' => array( 'eac_element_lottie' => 'yes' ),
					)
				);*/

				$element->add_control(
					'eac_element_lottie_viewport',
					array(
						'label'       => esc_html__( 'Enable in Window', 'eac-components' ),
						'description' => esc_html__( 'Active only in the visible part of the window', 'eac-components' ),
						'type'        => Controls_Manager::CHOOSE,
						'options'     => array(
							'yes' => array(
								'title' => esc_html__( 'Yes', 'eac-components' ),
								'icon'  => 'eicon-check',
							),
							'no'  => array(
								'title' => esc_html__( 'No', 'eac-components' ),
								'icon'  => 'eicon-ban',
							),
						),
						'default'     => 'no',
						'separator'   => 'before',
						'condition'   => array( 'eac_element_lottie' => 'yes' ),
					)
				);

				/** Ajout de la class 'lottie-anim_wrapper-bg' puiqu'un widget Lottie peut être dans la colonne */
				$element->add_control(
					'eac_element_lottie_opacity',
					array(
						'label'     => esc_html__( 'Opacity', 'eac-components' ),
						'type'      => Controls_Manager::SLIDER,
						'default'   => array( 'size' => 1 ),
						'range'     => array(
							'px' => array(
								'max'  => 1,
								'min'  => 0.1,
								'step' => 0.1,
							),
						),
						'selectors' => array( '{{WRAPPER}} .lottie-anim_wrapper.lottie-anim_wrapper-bg' => 'opacity: {{SIZE}};' ),
						'condition' => array( 'eac_element_lottie' => 'yes' ),
					)
				);

			$element->end_controls_section();
		}
	}

	/**
	 * render_lottie
	 *
	 * Modifie l'objet avant le rendu en frontend
	 *
	 * @param $element  Element_Base
	 */
	public function render_lottie( $element ) {
		$settings = $element->get_settings_for_display();

		if ( ! in_array( $element->get_name(), $this->target_elements, true ) ) {
			return;
		}

		/**
		 * Le control existe et il est renseigné
		 * La fonctionnalité des médias est active
		 */
		if ( isset( $settings['eac_element_lottie'] ) && 'yes' === $settings['eac_element_lottie'] ) {
			$url = '';

			if ( 'file' === $settings['eac_element_lottie_source'] ) {
				$url = $settings['eac_element_lottie_media_file'];
			} elseif ( 'true' === $settings['eac_element_lottie_is_json_enable'] && 'url' === $settings['eac_element_lottie_source'] && ! empty( $settings['eac_element_lottie_media_url']['url'] ) ) {
				$url = $settings['eac_element_lottie_media_url']['url'];
			}
			$viewp    = 'yes' === $settings['eac_element_lottie_viewport'] ? 'viewport' : 'none';
			$autoplay = 'yes' === $settings['eac_element_lottie_viewport'] ? 'false' : 'true';
			$loop     = 'yes' === $settings['eac_element_lottie_loop'] ? 'true' : 'false';
			$speed    = ! empty( $settings['eac_element_lottie_speed'] ) ? $settings['eac_element_lottie_speed'] : '1';

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

			?>
			<script type="text/javascript" lbg-js-id="<?php echo esc_attr( $element->get_id() ); ?>">
				jQuery(document).ready(function () {
					jQuery(".elementor-element-<?php echo esc_attr( $element->get_id() ); ?>")
						.prepend("<div class='lottie-anim_wrapper lottie-anim_wrapper-bg' data-src='<?php echo esc_url( $url ); ?>' data-autoplay='<?php echo esc_attr( $autoplay ); ?>' data-loop='<?php echo esc_attr( $loop ); ?>' data-speed='<?php echo esc_attr( $speed ); ?>' data-reverse='1' data-renderer='svg' data-trigger='<?php echo esc_attr( $viewp ); ?>' data-elem-id='<?php echo esc_attr( $element->get_id() ); ?>' data-name='lottie_<?php echo esc_attr( $element->get_id() ); ?>' style='position: absolute; top: 0; left: 0; right: 0; bottom: 0; min-block-size: 50px;'></div>");
				});
			</script>
			<?php
		}
	}

	/**
	 * print_template
	 *
	 * Modifie l'objet avant le rendu dans l'éditeur
	 *
	 * @param $element  Element_Base
	 */
	public function print_template( $template, $element ) {

		if ( ! in_array( $element->get_name(), $this->target_elements, true ) ) {
			return $template; }

		$old_template = $template;
		ob_start();
		?>
		<#
		if(settings.eac_element_lottie && 'yes' === settings.eac_element_lottie) {
			var url = '';
			if ('file' === settings.eac_element_lottie_source) {
				url = settings.eac_element_lottie_media_file;
			} else if ('true' === settings.eac_element_lottie_is_json_enable && 'url' === settings.eac_element_lottie_source && '' !== settings.eac_element_lottie_media_url.url ) {
				url = settings.eac_element_lottie_media_url.url;
			}
			var elemId = view.getID();
			var lottieName = 'lottie_' + view.getID();
			var viewp = 'yes' === settings.eac_element_lottie_viewport ? 'viewport' : 'none';
			var autoplay = 'yes' === settings.eac_element_lottie_viewport ? 'false' : 'true';
			var loop = 'yes' === settings.eac_element_lottie_loop ? 'true' : 'false';
			var speed = settings.eac_element_lottie_speed;
		#>

			<div class="lottie-anim_wrapper lottie-anim_wrapper-bg" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; min-block-size: 50px;"
			data-src="{{ url }}"
			data-autoplay="{{ autoplay }}"
			data-loop="{{ loop }}"
			data-speed="{{ speed }}"
			data-reverse="1"
			data-renderer="svg"
			data-trigger="{{ viewp }}"
			data-elem-id="{{ elemId }}"
			data-name="{{ lottieName }}"></div>
		<# } #>
		<?php
		$lottie_content = ob_get_clean();
		$template       = $lottie_content . $old_template;
		return $template;
	}
}
