<?php

class Dp_Owl_Carousel_Pro_Public {

	private $plugin_name;
	private $version;

	public function __construct( $plugin_name, $version ) {
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	public function enqueue_scripts() {
		if ( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() && ! wp_script_is( 'dp-owl-carousel-pro-admin-cpt-modal' ) ) {
			Dp_Owl_Carousel_Pro_Utils::enqueue_and_localize_cpt_modal_script();
		}
		wp_register_script( 'dp-ocp-mousewheel', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js', array( 'jquery' ), DPOCP_VERSION, true );
		if ( defined( 'ET_BUILDER_URI' ) ) {
			wp_register_script( 'magnific-popup', ET_BUILDER_URI . '/feature/dynamic-assets/assets/js/magnific-popup.js', array( 'jquery' ), ET_CORE_VERSION, true );
		}
	}

	public function get_carousel_items_data_ajax() {
		if ( isset( $_POST['module_props'] ) && isset( $_POST['module_class'] ) ) {
			echo json_encode( Dp_Owl_Carousel_Pro_Utils::get_carousel_items_data( $_POST['module_props'], $_POST['module_class'] ) );
		}
		wp_die();
	}

	public function get_image_sizes_ajax() {
		echo json_encode( Dp_Owl_Carousel_Pro_Utils::get_custom_sizes() );
		wp_die();
	}

	public function popup_page_template( $template ) {
		if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'ocp_popup_fetch' ) ) {
			if ( is_single() && isset( $_GET['dp_action'] ) && 'ocp_popup_fetch' === $_GET['dp_action'] ) {
				if ( glob( get_stylesheet_directory() . '/dp-owl-carousel/dp-popup-template.php' ) ) {
					$template = get_stylesheet_directory() . '/dp-owl-carousel/dp-popup-template.php';
				} else {
					$template = DPOCP_DIR . 'templates/dp-popup-template.php';
				}
			}
		}

		return $template;
	}

	public function et_global_assets_list( $assets_list ) {
		$assets_prefix = et_get_dynamic_assets_path();
		if ( ! ( isset( $assets['et_icons_all'] ) && isset( $assets['et_icons_fa'] ) ) ) {
			$assets_list['et_icons_all'] = array(
				'css' => "{$assets_prefix}/css/icons_all.css",
			);
			$assets_list['et_icons_fa']  = array(
				'css' => "{$assets_prefix}/css/icons_fa_all.css",
			);
		}
		if ( ! isset( $assets['et_jquery_magnific_popup'] ) ) {
			$assets_list['et_jquery_magnific_popup'] = array(
				'css' => "{$assets_prefix}/css/magnific_popup.css",
			);
		}

		return $assets_list;
	}

}
