<?php
namespace EcafeAddons;

use Elementor\Utils;
use Elementor\Core\Settings\Manager as SettingsManager;

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

final class EcafeElementLoad {

	private static $_instance;


	private $_modules_manager;

	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
			self::$_instance->init();
		}
		return self::$_instance;
	}

	public function init() {
		include_once ECAFE_CLASSES_URL . 'builder-content.php';
		include_once ECAFE_CLASSES_URL . 'builders/theme-template.php';
		include_once ECAFE_CLASSES_URL . 'conditions-file.php';
		include_once ECAFE_CLASSES_URL . 'theme-builder.php';
		include_once ECAFE_CLASSES_URL . 'elementor-document.php';
		include_once ECAFE_CLASSES_URL . 'conditions-rules.php';

		register_activation_hook( ECAFE_BASE_NAME, array( __CLASS__, 'activation_hook' ) );
	}

	private function includes() {

		require_once ECAFE_CLASSES_URL . 'class-helper.php';

		$defaultOnLoad = 'ecafedefaultoptions';
		$value         = '1';
		if ( is_admin() && get_option( $defaultOnLoad ) !== false ) {
		} elseif ( is_admin() ) {
			$defaultLoad = get_option( 'ecafe_widgets' );
			if ( $defaultLoad !== false && $defaultLoad != '' ) {
				$defaultNull = null;
				$onLoadAuto  = 'no';
				add_option( $defaultOnLoad, $value, $defaultNull, $onLoadAuto );
			} else {
				$widgetsPage                = get_option( 'ecafe_widgets' );
				$widgetsPage['widgetsload'] = array( 'ecafe-accordion', 'ecafe-adv-text-block', 'ecafe-back-to-top', 'ecafe-business-hours', 'ecafe-button','ecafe-calendly', 'ecafe-changelog', 'ecafe-chart','ecafe-circular-image-hover-effect','ecafe-contact-form-7','ecafe-copyright', 'ecafe-count-down', 'ecafe-coupon-code', 'ecafe-creative-gallery','ecafe-creative-title','ecafe-darkmode-styling', 'ecafe-dropcap', 'ecafe-flipbox', 'ecafe-gradient-underline-text','ecafe-gravity-forms','ecafe-grid-post-listing','ecafe-horizontal-page-scroll','ecafe-hover-background','ecafe-infobox', 'ecafe-social-icons', 'ecafe-title', 'ecafe-image-accordion', 'ecafe-image-animated-shape','ecafe-image-hover-effect','ecafe-image-scroll', 'ecafe-infocard', 'ecafe-infocircle', 'ecafe-infoprofile', 'ecafe-infounfold','ecafe-lottie', 'ecafe-manual-comparison-table','ecafe-marketing-link', 'ecafe-marquee','ecafe-marquee-text', 'ecafe-motion-text-circle', 'ecafe-page-piling', 'ecafe-page-scroll-to-id','ecafe-pdf-viewer', 'ecafe-preloader', 'ecafe-qr-code', 'ecafe-scroll-progress','ecafe-service-lists', 'ecafe-sticky-video', 'ecafe-toc', 'ecafe-teammember', 'ecafe-testimonial','ecafe-tabs', 'ecafe-text-background' );

				$defaultNull = null;
				$onLoadAuto  = 'no';
				add_option( 'ecafe_widgets', $widgetsPage, $defaultNull, $onLoadAuto );
				add_option( $defaultOnLoad, $value, $defaultNull, $onLoadAuto );
			}
		}

		require ECAFE_CLASSES_URL . 'class-panel-options.php';
	}

	/**
	 * Carry widgets
	 */
	public function carryWidgets() {
		require_once ECAFE_CLASSES_URL . 'widgets-passing-lists.php';
	}

	/**
	 * Editor load style
	 */
	public function afterEnqueueStylesEditor() {
		wp_enqueue_style( 'ecafe-elementor', ECAFE_ASSETS_PATH . 'css/admin/ecafe-elementor.css', array(), ECAFE_VERSION, false );

		$ui_theme = SettingsManager::get_settings_managers( 'editorPreferences' )->get_model()->get_settings( 'ui_theme' );
		if ( ! empty( $ui_theme ) && $ui_theme == 'dark' ) {
			wp_enqueue_style( 'ecafe-dark-elementor', ECAFE_ASSETS_PATH . 'css/admin/ecafe-dark-elementor.css', array(), ECAFE_VERSION, false );
		}
	}

	/**
	 * Admin load style
	 */
	public function ecafeElementorAdminCss( $hook ) {
		wp_enqueue_style( 'ecafe-elementor', ECAFE_ASSETS_PATH . 'css/admin/ecafe-elementor.css', array(), ECAFE_VERSION, false );
		wp_enqueue_script( 'ecafe-backuser', ECAFE_ASSETS_PATH . 'js/admin/ecafe-admin.js', array( 'jquery' ), ECAFE_VERSION, false );

		if ( ( 'post-new.php' === $hook || 'post.php' === $hook ) && get_post_type() === ECAFE_POST ) {
			wp_enqueue_style( 'select2-css', ECAFE_ASSETS_PATH . 'css/admin/select2.min.css', array(), ECAFE_VERSION );

			wp_enqueue_script( 'select2-js', ECAFE_ASSETS_PATH . 'js/admin/select2.min.js', array( 'jquery' ), ECAFE_VERSION, true );

			wp_localize_script(
				'ecafe-backuser',
				'ecafe_data',
				array(
					'nonce' => wp_create_nonce( 'ecafe-temp-nonce' ),
				)
			);
		}
	}

	/**
	 * Category for elementor
	 */
	public function categoryForElementor() {
		$EI = \Elementor\Plugin::$instance;
		$EI->elements_manager->add_category(
			'essential',
			array(
				'title' => esc_html__( 'EC Essential', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecpro',
			array(
				'title' => esc_html__( 'EC Pro', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecwoocommerce',
			array(
				'title' => esc_html__( 'EC WooCommerce', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecforms',
			array(
				'title' => esc_html__( 'EC Forms', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecembed',
			array(
				'title' => esc_html__( 'EC Embed', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecproschema',
			array(
				'title' => esc_html__( 'EC Schema', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecacf',
			array(
				'title' => esc_html__( 'EC ACF', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'eccfs',
			array(
				'title' => esc_html__( 'EC CFS', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecjetengine',
			array(
				'title' => esc_html__( 'EC JetEngine', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecmetabox',
			array(
				'title' => esc_html__( 'EC Metabox', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecpods',
			array(
				'title' => esc_html__( 'EC Pods', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ectoolset',
			array(
				'title' => esc_html__( 'EC Toolset', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
		$EI->elements_manager->add_category(
			'ecwck',
			array(
				'title' => esc_html__( 'EC WCK', 'essential-classy-addons-for-elementor' ),
				'icon'  => 'ecaddons',
			),
			1
		);
	}

	/**
	 * Settings links
	 */
	function ecafeSettingsLinks( $settingsLinks ) {
		$settingsLinksArray = array(
			'<a href="' . admin_url( 'admin.php?page=ecafe_widgets' ) . '">' . esc_html__( 'Settings', 'essential-classy-addons-for-elementor' ) . '</a>',
		);
		return array_merge( $settingsLinks, $settingsLinksArray );
	}

	function ecafeSettingsLinksRest( $settingsLinks ) {
		if ( defined( 'ECAFEP_VERSION' ) ) {
			$get_values = unserialize( get_option( 'ecafewhitelabel' ) );
			if ( ! empty( $get_values['plugin_name_free'] ) ) {
				$settingsLinksArray = array();
			} else {
				$settingsLinksArray = array(
					' | <a href="https://www.facebook.com/groups/340597333894907" target="_blank" style="color:#000; font-weight: bold;">' . __( 'Join/Contact US', 'essential-classy-addons-for-elementor' ) . '</a> | <a href="https://ecaddons.com/docs/" target="_blank" style="color:#000; font-weight: bold;">' . __( 'Document', 'essential-classy-addons-for-elementor' ) . '</a> | <a href="http://store.kapasias.com/helpdesk/" target="_blank" style="color:#000; font-weight: bold;">' . __( 'Support', 'essential-classy-addons-for-elementor' ) . '</a>',
				);
			}
		} else {
			$settingsLinksArray = array(
				' | <a href="https://www.facebook.com/groups/340597333894907" target="_blank" style="color:#000; font-weight: bold;">' . __( 'Join/Contact US', 'essential-classy-addons-for-elementor' ) . '</a> | <a href="https://ecaddons.com/docs/" target="_blank" style="color:#000; font-weight: bold;">' . __( 'Document', 'essential-classy-addons-for-elementor' ) . '</a> | <a href="http://store.kapasias.com/helpdesk/" target="_blank" style="color:#000; font-weight: bold;">' . __( 'Support', 'essential-classy-addons-for-elementor' ) . '</a>',
			);
		}

		return array_merge( $settingsLinks, $settingsLinksArray );
	}

	public function ecafeEnqueueStyles() {
		wp_enqueue_style( 'ecafe-frontend-css', ECAFE_ASSETS_PATH . 'css/custom/ecafe-front.min.css', array(), ECAFE_VERSION );
		// wp_enqueue_style('ecafe-lightbox',ECAFE_ASSETS_PATH . 'css/baharni/lightbox.css',[],ECAFE_VERSION);

		wp_enqueue_style( 'font-awesome-5-all', ELEMENTOR_ASSETS_URL . 'lib/font-awesome/css/all.min.css', array(), ECAFE_VERSION );

		if ( isset( $_GET['elementor-preview'] ) && (int) $_GET['elementor-preview'] ) {
			wp_register_script( 'ecafe-tilt-js-3rd', ECAFE_ASSETS_PATH . 'js/baharni/tilt.jquery.js', array( 'jquery' ), ECAFE_VERSION, false );
			wp_register_script( 'ecafe-tilt-js', ECAFE_ASSETS_PATH . 'js/custom/ecafe-tilt-custom.min.js', array( 'ecafe-tilt-js-3rd' ), ECAFE_VERSION, false );
			wp_enqueue_script( 'ecafe-tilt-js' );

			wp_enqueue_style( 'ecafe-tippy-css', ECAFE_ASSETS_PATH . 'css/baharni/tippy.min.css', array(), ECAFE_VERSION, false );
			wp_register_script( 'ecafe-popper-js', ECAFE_ASSETS_PATH . 'js/baharni/popper.min.js', array( 'jquery' ), ECAFE_VERSION, false );
			wp_register_script( 'ecafe-tippy-js-3rd', ECAFE_ASSETS_PATH . 'js/baharni/tippy-bundle.umd.min.js', array( 'ecafe-popper-js' ), ECAFE_VERSION, false );
			wp_register_script( 'ecafe-tippy-js', ECAFE_ASSETS_PATH . 'js/custom/ecafe-tippy-custom.min.js', array( 'ecafe-popper-js', 'ecafe-tippy-js-3rd' ), ECAFE_VERSION, false );
			wp_enqueue_script( 'ecafe-tippy-js' );
		}

		wp_register_script( 'ecafe-accordion', ECAFE_ASSETS_PATH . 'js/custom/ecafe-accordion.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-back-to-top', ECAFE_ASSETS_PATH . 'js/custom/ecafe-back-to-top.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-chart-3rd', ECAFE_ASSETS_PATH . 'js/baharni/chart.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-chart', ECAFE_ASSETS_PATH . 'js/custom/ecafe-chart.min.js', array( 'ecafe-chart-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-calendly-3rd', ECAFE_ASSETS_PATH . 'js/baharni/calendlywidget.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-changelog', ECAFE_ASSETS_PATH . 'js/custom/ecafe-changelog.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		// wp_register_script('ecafe-lightbox', ECAFE_ASSETS_PATH . 'js/baharni/lightbox.js', array('jquery'),ECAFE_VERSION,false );
		wp_register_script( 'ecafe-infounfold', ECAFE_ASSETS_PATH . 'js/custom/ecafe-infounfold.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-marquee', ECAFE_ASSETS_PATH . 'js/custom/ecafe-marquee.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-lax', ECAFE_ASSETS_PATH . 'js/baharni/lax.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-min-js', ECAFE_ASSETS_PATH . 'js/custom/ecafe.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-isotope', ECAFE_ASSETS_PATH . 'js/baharni/isotope.pkgd.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-imagesloaded', ECAFE_ASSETS_PATH . 'js/baharni/imagesloaded.pkgd.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-post-listing', ECAFE_ASSETS_PATH . 'js/custom/ecafe-post-listing.min.js', array( 'ecafe-isotope', 'ecafe-imagesloaded' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-tabs', ECAFE_ASSETS_PATH . 'js/custom/ecafe-tabs.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-whatsapp', ECAFE_ASSETS_PATH . 'js/custom/ecafe-whatsapp.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-countdown', ECAFE_ASSETS_PATH . 'js/custom/ecafe-countdown.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-coupon-code', ECAFE_ASSETS_PATH . 'js/custom/ecafe-coupon-code.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-lottie-3rd', ECAFE_ASSETS_PATH . 'js/baharni/lottie.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-lottie', ECAFE_ASSETS_PATH . 'js/custom/ecafe-lottie.min.js', array( 'ecafe-lottie-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-marketing-link', ECAFE_ASSETS_PATH . 'js/custom/ecafe-marketing-link.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-scroll-progress', ECAFE_ASSETS_PATH . 'js/custom/ecafe-scroll-progress.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-image-scroll', ECAFE_ASSETS_PATH . 'js/custom/ecafe-image-scroll.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-image-accordion', ECAFE_ASSETS_PATH . 'js/custom/ecafe-image-accordion.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-horizontal-page-scroll', ECAFE_ASSETS_PATH . 'js/custom/ecafe-horizontal-page-scroll.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-hover-background', ECAFE_ASSETS_PATH . 'js/custom/ecafe-hover-background.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-post-image', ECAFE_ASSETS_PATH . 'js/custom/ecafe-post-image.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-page-piling-3rd', ECAFE_ASSETS_PATH . 'js/baharni/pagepiling.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-page-piling', ECAFE_ASSETS_PATH . 'js/custom/ecafe-page-piling.min.js', array( 'ecafe-page-piling-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-page-scroll-to-id-3rd', ECAFE_ASSETS_PATH . 'js/baharni/jquery.PageScroll2id.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-page-scroll-to-id', ECAFE_ASSETS_PATH . 'js/custom/ecafe-page-scroll-to-id.min.js', array( 'ecafe-page-scroll-to-id-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-pdf-viewer-3rd', ECAFE_ASSETS_PATH . 'js/baharni/pdfobject.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-pdf-viewer', ECAFE_ASSETS_PATH . 'js/custom/ecafe-pdf-viewer.min.js', array( 'ecafe-pdf-viewer-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-tocbot-3rd', ECAFE_ASSETS_PATH . 'js/baharni/tocbot.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-toc', ECAFE_ASSETS_PATH . 'js/custom/ecafe-toc.min.js', array( 'ecafe-tocbot-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-qrcode-3rd', ECAFE_ASSETS_PATH . 'js/baharni/jquery.qrcode.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-qrcode', ECAFE_ASSETS_PATH . 'js/custom/ecafe-qrcode.min.js', array( 'ecafe-qrcode-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-infocircle', ECAFE_ASSETS_PATH . 'js/custom/ecafe-infocircle.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-sticky-video', ECAFE_ASSETS_PATH . 'js/custom/ecafe-sticky-video.min.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ec-confettieffect-3rd', ECAFE_ASSETS_PATH . 'js/baharni/js-confetti.browser.js', array( 'jquery' ), ECAFE_VERSION, false );
		wp_register_script( 'ec-confettieffect', ECAFE_ASSETS_PATH . 'js/custom/ec-confettieffect.min.js', array( 'ec-confettieffect-3rd' ), ECAFE_VERSION, false );
		wp_register_script( 'ecafe-event-tracker', ECAFE_ASSETS_PATH . 'js/custom/ecafe-event-tracker.min.js', array( 'jquery' ), ECAFE_VERSION, false );

		wp_localize_script(
			'jquery',
			'ecafe_data',
			array(
				'eca_ajax_url' => admin_url( 'admin-ajax.php' ),
			)
		);
	}

	/**
	 * Hooks
	 */
	private function hooks() {
		add_action( 'elementor/elements/categories_registered', array( $this, 'categoryForElementor' ) );

		// Register Widget Styles & Script
		if ( did_action( 'elementor/loaded' ) ) {
			add_action( 'wp_enqueue_scripts', array( $this, 'ecafeEnqueueStyles' ) );
		}

		add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'afterEnqueueStylesEditor' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'ecafeElementorAdminCss' ) );
		add_filter( 'plugin_action_links_' . ECAFE_BASE_NAME, array( $this, 'ecafeSettingsLinks' ) );
		add_filter( 'plugin_action_links_' . ECAFE_BASE_NAME, array( $this, 'ecafeSettingsLinksRest' ) );
	}

	/**
	 * Activation Hook
	 */
	public static function activation_hook( $network_wide ) {
		flush_rewrite_rules();
	}

	/**
	 * Constructor
	 */
	private function __construct() {
		$this->includes();
		$this->hooks();
		$this->carryWidgets();
		ecafeWidgetsPassed();
	}
}

EcafeElementLoad::instance();
