<?php
use Elementor\Controls_Manager;
use Elementor\Plugin;

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

	private static $loadscript = null;

	public function __construct() {

		$extrasListOpt = get_option( 'ecafe_extension' );
		if ( ! empty( $extrasListOpt ) && ( isset( $extrasListOpt['extrasList'] ) && ! empty( $extrasListOpt['extrasList'] ) ) ) {
			$this->extra_add_widgets_confettieffect_actions();
		}

		add_action( 'elementor/frontend/before_render', array( $this, 'ecconfettieffectrender' ), 10, 1 );
	}

	public function ec_confetti_enqueue_scripts() {

		if ( ! wp_script_is( 'ec-confettieffect', 'enqueued' ) ) {
			wp_enqueue_script( 'ec-confettieffect' );
		}
	}

	public function get_extra_list() {
		$extrasListOpt = get_option( 'ecafe_extension' );
		if ( ! empty( $extrasListOpt ) && ( isset( $extrasListOpt['extrasList'] ) && ! empty( $extrasListOpt['extrasList'] ) ) ) {
			return $extrasListOpt['extrasList'];
		}
		return array();
	}
	public function get_name() {
		return 'ecafe-confettieffect';
	}

	public function ec_confettieffect_style( $element ) {

		if ( self::$loadscript ) {
			return;
		}

		$settings             = $element->get_settings_for_display();
		$confettieffectSwitch = isset( $settings['confettieffectSwitch'] ) ? $settings['confettieffectSwitch'] : '';
		if ( ! empty( $confettieffectSwitch ) && $confettieffectSwitch == 'yes' ) {
			$this->ec_confetti_enqueue_scripts();
			self::$loadscript = true;
		}
	}

	protected function extra_add_widgets_confettieffect_actions() {
		if ( in_array( 'ecafe-confettieffect', $this->get_extra_list() ) ) {
			add_action( 'elementor/preview/enqueue_scripts', array( $this, 'ec_confetti_enqueue_scripts' ) );
			add_action( 'elementor/frontend/widget/before_render', array( $this, 'ec_confettieffect_style' ) );
			add_action( 'elementor/element/common/section_custom_css_pro/after_section_end', array( $this, 'add_confettieffect_control' ), 10, 2 );
		}
	}

	public function add_confettieffect_control( $element, $args ) {
		$element->start_controls_section(
			'confettieffect',
			array(
				'label' => esc_html__( 'EC Confetti Effect', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_ADVANCED,
			)
		);
		$element->add_control(
			'confettieffectSwitch',
			array(
				'label'        => esc_html__( 'Confetti Effect', 'essential-classy-addons-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off'    => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
				'return_value' => 'yes',
				'description'  => __( 'Check on Frontend.', 'essential-classy-addons-for-elementor' ),
			)
		);
		$element->add_control(
			'ecceTrigger',
			array(
				'label'       => esc_html__( 'Trigger', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'default'     => 'click',
				'label_block' => true,
				'options'     => array(
					'load'  => esc_html__( 'On Load', 'essential-classy-addons-for-elementor' ),
					'click' => esc_html__( 'Click', 'essential-classy-addons-for-elementor' ),
					'hover' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
				),
				'condition'   => array(
					'confettieffectSwitch' => 'yes',
				),
			)
		);

		$repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'ecceemojis',
			array(
				'label'       => esc_html__( 'Emojis', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '💥',
				'label_block' => true,
			)
		);
		$repeater->add_control(
			'eccecolor',
			array(
				'label'   => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#000000',
			)
		);
		$element->add_control(
			'ecceContent',
			array(
				'label'       => esc_html__( 'Content', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::REPEATER,
				'default'     => array(
					array(
						'ecceemojis' => '🌈',
						'eccecolor'  => '#ff0a54',
					),
					array(
						'ecceemojis' => '⚡️',
						'eccecolor'  => '#ff477e',
					),
					array(
						'ecceemojis' => '💥',
						'eccecolor'  => '#ff7096',
					),
					array(
						'ecceemojis' => '✨',
						'eccecolor'  => '#ff85a1',
					),
					array(
						'ecceemojis' => '💫',
						'eccecolor'  => '#fbb1bd',
					),
				),
				'condition'   => array(
					'confettieffectSwitch' => 'yes',
				),
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{ecceemojis}}',
			)
		);
		$element->add_control(
			'emojiSize',
			array(
				'label'     => __( 'Emoji Size', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 1,
				'max'       => 200,
				'step'      => 1,
				'default'   => 20,
				'condition' => array(
					'confettieffectSwitch' => 'yes',
				),
			)
		);
		$element->add_control(
			'confettiRadius',
			array(
				'label'     => __( 'Confetti Radius', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 1,
				'max'       => 200,
				'step'      => 1,
				'default'   => 5,
				'condition' => array(
					'confettieffectSwitch' => 'yes',
				),
			)
		);
		$element->add_control(
			'confettiNumber',
			array(
				'label'     => __( 'Confetti Number', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 1,
				'max'       => 500,
				'step'      => 10,
				'default'   => 20,
				'condition' => array(
					'confettieffectSwitch' => 'yes',
				),
			)
		);
		$element->end_controls_section();
	}

	public function ecconfettieffectrender( $element ) {
		$settings             = $element->get_settings_for_display();
		$confettieffectSwitch = isset( $settings['confettieffectSwitch'] ) ? $settings['confettieffectSwitch'] : '';
		if ( ! empty( $confettieffectSwitch ) && $confettieffectSwitch == 'yes' ) {
			if ( ! empty( $settings['ecceContent'] ) ) {
				$data_eccecon   = array();
				$data_eccecolor = array();
				foreach ( $settings['ecceContent'] as $index => $item ) {
					$data_eccecon[]   = ! empty( $item['ecceemojis'] ) ? $item['ecceemojis'] : '';
					$data_eccecolor[] = ! empty( $item['eccecolor'] ) ? $item['eccecolor'] : '';
					++$index;
				}
			}
			$emojiSize      = ! empty( $settings['emojiSize'] ) ? esc_attr( $settings['emojiSize'] ) : 20;
			$confettiRadius = ! empty( $settings['confettiRadius'] ) ? esc_attr( $settings['confettiRadius'] ) : 5;
			$confettiNumber = ! empty( $settings['confettiNumber'] ) ? esc_attr( $settings['confettiNumber'] ) : 20;

			$element->add_render_attribute(
				'_wrapper',
				array(
					'class'                  => 'ec-confettieffect',
					'data-ec-confettieffect' => esc_attr(
						wp_json_encode(
							array(
								'ecceTrigger'    => esc_attr( $settings['ecceTrigger'] ),
								'eccecon'        => $data_eccecon,
								'eccecolor'      => $data_eccecolor,
								'emojiSize'      => $emojiSize,
								'confettiRadius' => $confettiRadius,
								'confettiNumber' => $confettiNumber,
							)
						)
					),
				)
			);

		}
	}
}
