<?php
namespace EcafeAddons;

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

if ( ! class_exists( 'ecafeWidgetsPassed' ) ) {

	/**
	 * Ecafe widgets passed
	 */
	class ecafeWidgetsPassed {

		private static $instance = null;

		/**
		 * Constructor
		 */
		private function __construct() {
			$this->init();
		}

		public function init() {
			add_action( 'elementor/widgets/register', array( $this, 'addWidgets' ) );
			add_action( 'elementor/controls/controls_registered', array( $this, 'add_controls' ), 10 );
		}

		/**
		 * Add Controls Extensions
		 */
		public function add_controls( $controls_manager ) {
			$grouped_control = array(
				'ecafe-widgets-extras'   => 'Ecafe_Widgets_Extras',
				'ecafe-displaycondition' => 'Ecafe_Displaycondition',
				'ecafe-liquid-glass'     => 'Ecafe_Liquidglass',
				'ecafe-customcss'        => 'Ecafe_Customcss',
				'ecafe-confettieffect'   => 'Ecafe_Confettieffect',
				'ecafe-boxshadow'        => 'Ecafe_Boxshadow',
				'ecafe-dropshadow'       => 'Ecafe_Dropshadow',
				'ecafe-textshadow'       => 'Ecafe_Textshadow',
				'ecafe-template-library' => 'Ecafe_Templatelibrary',
				'ecafe-fancyborder'      => 'Ecafe_Fancyborder',
				'ecafe-equalheight'      => 'Ecafe_Equalheight',
			);
			foreach ( $grouped_control as $control_id => $class_name ) {
				if ( $this->extensionWidgets( $control_id, true ) ) {
					new $class_name();
				}
			}
		}

		/**
		 * Add widgets
		 */
		public function addWidgets( $widgetsexEcutive ) {

			$combined = array(
				'ecafe-accordion'                   => '\EcafeAddons\Widgets\Ecafe_Accordion',
				'ecafe-adv-text-block'              => '\EcafeAddons\Widgets\Ecafe_Adv_Text_Block',
				'ecafe-back-to-top'                 => '\EcafeAddons\Widgets\Ecafe_Back_To_Top',
				'ecafe-business-hours'              => '\EcafeAddons\Widgets\Ecafe_Business_Hours',
				'ecafe-button'                      => '\EcafeAddons\Widgets\Ecafe_Button',
				'ecafe-count-down'                  => '\EcafeAddons\Widgets\Ecafe_Countdown',
				'ecafe-coupon-code'                 => '\EcafeAddons\Widgets\Ecafe_Coupon_Code',
				'ecafe-changelog'                   => '\EcafeAddons\Widgets\Ecafe_Changelog',
				'ecafe-calendly'                    => '\EcafeAddons\Widgets\Ecafe_Calendly',
				'ecafe-chart'                       => '\EcafeAddons\Widgets\Ecafe_Chart',
				'ecafe-circular-image-hover-effect' => '\EcafeAddons\Widgets\Ecafe_ecafeCircularImageHoverEffect',
				'ecafe-contact-form-7'              => '\EcafeAddons\Widgets\EcafeContactForm7',
				'ecafe-copyright'                   => '\EcafeAddons\Widgets\EcafeCopyright',
				'ecafe-creative-gallery'            => '\EcafeAddons\Widgets\EcafeCreativeGallery',
				'ecafe-creative-title'              => '\EcafeAddons\Widgets\EcafeCreativeTitle',
				'ecafe-darkmode-styling'            => '\EcafeAddons\Widgets\Ecafe_Darkmode_Styling',
				'ecafe-dropcap'                     => '\EcafeAddons\Widgets\Ecafe_Dropcap',
				'ecafe-dual-image'                  => '\EcafeAddons\Widgets\EcafeDualImage',
				'ecafe-flipbox'                     => '\EcafeAddons\Widgets\Ecafe_Flipbox',
				'ecafe-freebusy'                    => '\EcafeAddons\Widgets\Ecafe_Freebusy',
				'ecafe-gradient-underline-text'     => '\EcafeAddons\Widgets\Ecafe_GradientUnderlineText',
				'ecafe-gravity-forms'               => '\EcafeAddons\Widgets\Ecafe_Gravity_Forms',
				'ecafe-grid-post-listing'           => '\EcafeAddons\Widgets\Ecafe_Grid_Post_Listing',
				'ecafe-horizontal-page-scroll'      => '\EcafeAddons\Widgets\Ecafe_Horizontal_Page_Scroll',
				'ecafe-hover-background'            => '\EcafeAddons\Widgets\Ecafe_Hover_Background',
				'ecafe-image-hover-effect'          => '\EcafeAddons\Widgets\Ecafe_Image_Hover_Effect',
				'ecafe-image-mask-hover-effect'     => '\EcafeAddons\Widgets\Ecafe_ImageMaskHoverEffect',
				'ecafe-image-mask-layer'            => '\EcafeAddons\Widgets\Ecafe_ImageMaskLayer',
				'ecafe-image-accordion'             => '\EcafeAddons\Widgets\Ecafe_Image_Accordion',
				'ecafe-image-animated-shape'        => '\EcafeAddons\Widgets\Ecafe_Image_AnimatedShape',
				'ecafe-image-scroll'                => '\EcafeAddons\Widgets\Ecafe_Image_Scroll',
				'ecafe-imageswap'                   => '\EcafeAddons\Widgets\Ecafe_ImageSwap',
				'ecafe-infobox'                     => '\EcafeAddons\Widgets\Ecafe_Infobox',
				'ecafe-infocard'                    => '\EcafeAddons\Widgets\Ecafe_Infocard',
				'ecafe-infocircle'                  => '\EcafeAddons\Widgets\Ecafe_InfoCircle',
				'ecafe-infoprofile'                 => '\EcafeAddons\Widgets\Ecafe_Infoprofile',
				'ecafe-infounfold'                  => '\EcafeAddons\Widgets\Ecafe_InfounFold',
				'ecafe-lottie'                      => '\EcafeAddons\Widgets\Ecafe_Lottie',
				'ecafe-manual-comparison-table'     => '\EcafeAddons\Widgets\Ecafe_Manual_Comparison_Table',
				'ecafe-marketing-link'              => '\EcafeAddons\Widgets\Ecafe_Marketing_Link',
				'ecafe-marquee'                => '\EcafeAddons\Widgets\Ecafe_Marquee',
				'ecafe-marquee-text'                => '\EcafeAddons\Widgets\Ecafe_Marquee_Text',
				'ecafe-motion-text-circle'          => '\EcafeAddons\Widgets\Ecafe_Motion_Text_Circle',
				'ecafe-page-piling'                 => '\EcafeAddons\Widgets\Ecafe_Page_Piling',
				'ecafe-page-scroll-to-id'           => '\EcafeAddons\Widgets\Ecafe_Page_Scroll_To_Id',
				'ecafe-pdf-viewer'                  => '\EcafeAddons\Widgets\Ecafe_Pdf_Viewer',
				'ecafe-preloader'                   => '\EcafeAddons\Widgets\Ecafe_Preloader',
				'ecafe-social-icons'                => '\EcafeAddons\Widgets\Ecafe_Social_Icons',
				'ecafe-sticky-video'                => '\EcafeAddons\Widgets\Ecafe_Sticky_Video',
				'ecafe-scroll-progress'             => '\EcafeAddons\Widgets\Ecafe_Scroll_Progress',
				'ecafe-service-lists'               => '\EcafeAddons\Widgets\Ecafe_Service_Lists',
				'ecafe-toc'                         => '\EcafeAddons\Widgets\Ecafe_Toc',
				'ecafe-teammember'                  => '\EcafeAddons\Widgets\Ecafe_Teammember',
				'ecafe-testimonial'                 => '\EcafeAddons\Widgets\Ecafe_Testimonial',
				'ecafe-title'                       => '\EcafeAddons\Widgets\Ecafe_Title',
				'ecafe-tabs'                        => '\EcafeAddons\Widgets\Ecafe_Tabs',
				'ecafe-text-background'             => '\EcafeAddons\Widgets\Ecafe_Text_Background',
				'ecafe-whatsapp'                    => '\EcafeAddons\Widgets\Ecafe_Whatsapp',
				'ecafe-wpforms'                     => '\EcafeAddons\Widgets\Ecafe_Wpforms',
				'ecafe-woocommerce-cart'            => '\EcafeAddons\Widgets\Ecafe_Woocommerce_Cart',
				'ecafe-woocommerce-checkout'        => '\EcafeAddons\Widgets\Ecafe_Woocommerce_Checkout',
				'ecafe-woocommerce-myaccount'       => '\EcafeAddons\Widgets\Ecafe_Woocommerce_Myaccount',
				'ecafe-woocommerce-ordertrack'      => '\EcafeAddons\Widgets\Ecafe_Woocommerce_ordertrack',
				'ecafe-facebook-embed'              => '\EcafeAddons\Widgets\Ecafe_Facebook_Embed',
				'ecafe-googlemap-embed'             => '\EcafeAddons\Widgets\Ecafe_Googlemap_Embed',
				'ecafe-site-logo'                   => '\EcafeAddons\Widgets\Ecafe_Sitelogo',
				'ecafe-site-title'                  => '\EcafeAddons\Widgets\Ecafe_Sitetitle',
				'ecafe-site-tagline'                => '\EcafeAddons\Widgets\Ecafe_Sitetagline',
				'ecafe-post-title'                  => '\EcafeAddons\Widgets\Ecafe_Post_Title',
				'ecafe-post-meta'                   => '\EcafeAddons\Widgets\Ecafe_Post_Meta',
				'ecafe-post-content'                => '\EcafeAddons\Widgets\Ecafe_Post_Content',
				'ecafe-post-image'                  => '\EcafeAddons\Widgets\Ecafe_Post_Image',
				'ecafe-post-authorinfo'             => '\EcafeAddons\Widgets\Ecafe_Post_Authorinfo',
				'ecafe-post-comment'                => '\EcafeAddons\Widgets\Ecafe_Post_Comment',
				'ecafe-post-navigation'             => '\EcafeAddons\Widgets\Ecafe_Post_Navigation',
				'ecafe-qr-code'                     => '\EcafeAddons\Widgets\Ecafe_QRCode',
				'ecafe-randomimage'                 => '\EcafeAddons\Widgets\Ecafe_RandomImage',
				'ecafe-acf-text'                    => '\EcafeAddons\Widgets\Ecafe_Acf_Text',
				'ecafe-acf-text-area'               => '\EcafeAddons\Widgets\Ecafe_Acf_Text_Area',
				'ecafe-acf-wysiwyg-editor'          => '\EcafeAddons\Widgets\Ecafe_Acf_Wysiwyg_Editor',
				'ecafe-acf-url'                     => '\EcafeAddons\Widgets\Ecafe_Acf_Url',
				'ecafe-acf-email'                   => '\EcafeAddons\Widgets\Ecafe_Acf_Email',
				'ecafe-acf-number'                  => '\EcafeAddons\Widgets\Ecafe_Acf_Number',
				'ecafe-acf-image'                   => '\EcafeAddons\Widgets\Ecafe_Acf_Image',
				'ecafe-acf-date'                    => '\EcafeAddons\Widgets\Ecafe_Acf_Date',
				'ecafe-acf-time'                    => '\EcafeAddons\Widgets\Ecafe_Acf_Time',
				'ecafe-acf-date-time'               => '\EcafeAddons\Widgets\Ecafe_Acf_Date_Time',
				'ecafe-acf-oembed'                  => '\EcafeAddons\Widgets\Ecafe_Acf_Oembed',
				'ecafe-cfs-text'                    => '\EcafeAddons\Widgets\Ecafe_Cfs_Text',
				'ecafe-cfs-textarea'                => '\EcafeAddons\Widgets\Ecafe_Cfs_Textarea',
				'ecafe-cfs-wysiwyg'                 => '\EcafeAddons\Widgets\Ecafe_Cfs_Wysiwyg',
				'ecafe-cfs-hyperlink'               => '\EcafeAddons\Widgets\Ecafe_Cfs_Hyperlink',
				'ecafe-cfs-date'                    => '\EcafeAddons\Widgets\Ecafe_Cfs_Date',
				'ecafe-cfs-image'                   => '\EcafeAddons\Widgets\Ecafe_Cfs_Image',
				'ecafe-jetengine-text'              => '\EcafeAddons\Widgets\Ecafe_Jetengine_Text',
				'ecafe-jetengine-date'              => '\EcafeAddons\Widgets\Ecafe_Jetengine_Date',
				'ecafe-jetengine-time'              => '\EcafeAddons\Widgets\Ecafe_Jetengine_Time',
				'ecafe-jetengine-datetime'          => '\EcafeAddons\Widgets\Ecafe_Jetengine_Datetime',
				'ecafe-jetengine-textarea'          => '\EcafeAddons\Widgets\Ecafe_Jetengine_Textarea',
				'ecafe-jetengine-wysiwyg'           => '\EcafeAddons\Widgets\Ecafe_Jetengine_Wysiwyg',
				'ecafe-jetengine-media'             => '\EcafeAddons\Widgets\Ecafe_Jetengine_Media',
				'ecafe-jetengine-number'            => '\EcafeAddons\Widgets\Ecafe_Jetengine_Number',
				'ecafe-metabox-text'                => '\EcafeAddons\Widgets\Ecafe_Metabox_Text',
				'ecafe-metabox-textarea'            => '\EcafeAddons\Widgets\Ecafe_Metabox_Textarea',
				'ecafe-metabox-wysiwyg'             => '\EcafeAddons\Widgets\Ecafe_Metabox_Wysiwyg',
				'ecafe-metabox-email'               => '\EcafeAddons\Widgets\Ecafe_Metabox_Email',
				'ecafe-metabox-number'              => '\EcafeAddons\Widgets\Ecafe_Metabox_Number',
				'ecafe-metabox-url'                 => '\EcafeAddons\Widgets\Ecafe_Metabox_Url',
				'ecafe-metabox-imageadvanced'       => '\EcafeAddons\Widgets\Ecafe_Metabox_Imageadvanced',
				'ecafe-metabox-video'               => '\EcafeAddons\Widgets\Ecafe_Metabox_Video',
				'ecafe-metabox-date'                => '\EcafeAddons\Widgets\Ecafe_Metabox_Date',
				'ecafe-metabox-datetime'            => '\EcafeAddons\Widgets\Ecafe_Metabox_Datetime',
				'ecafe-metabox-time'                => '\EcafeAddons\Widgets\Ecafe_Metabox_Time',
				'ecafe-pods-plaintext'              => '\EcafeAddons\Widgets\Ecafe_Pods_Plaintext',
				'ecafe-pods-website'                => '\EcafeAddons\Widgets\Ecafe_Pods_Website',
				'ecafe-pods-phone'                  => '\EcafeAddons\Widgets\Ecafe_Pods_Phone',
				'ecafe-pods-email'                  => '\EcafeAddons\Widgets\Ecafe_Pods_Email',
				'ecafe-pods-plain-paragraph-text'   => '\EcafeAddons\Widgets\Ecafe_Pods_Plain_Paragraph_Text',
				'ecafe-pods-wysywyg'                => '\EcafeAddons\Widgets\Ecafe_Pods_Wysywyg',
				'ecafe-pods-date-time'              => '\EcafeAddons\Widgets\Ecafe_Pods_Datetime',
				'ecafe-pods-date'                   => '\EcafeAddons\Widgets\Ecafe_Pods_Date',
				'ecafe-pods-time'                   => '\EcafeAddons\Widgets\Ecafe_Pods_Time',
				'ecafe-pods-plain-number'           => '\EcafeAddons\Widgets\Ecafe_Pods_Plainnumber',
				'ecafe-pods-currency'               => '\EcafeAddons\Widgets\Ecafe_Pods_Currency',
				'ecafe-pods-image'                  => '\EcafeAddons\Widgets\Ecafe_Pods_Image',
				'ecafe-pods-video'                  => '\EcafeAddons\Widgets\Ecafe_Pods_Video',
				'ecafe-pods-oembed'                 => '\EcafeAddons\Widgets\Ecafe_Pods_Oembed',
				'ecafe-toolset-single-line'         => '\EcafeAddons\Widgets\Ecafe_Toolset_Singleline',
				'ecafe-toolset-multiple-lines'      => '\EcafeAddons\Widgets\Ecafe_Toolset_Multiplelines',
				'ecafe-toolset-wysiwyg'             => '\EcafeAddons\Widgets\Ecafe_Toolset_Wysiwyg',
				'ecafe-toolset-image'               => '\EcafeAddons\Widgets\Ecafe_Toolset_Image',
				'ecafe-toolset-video'               => '\EcafeAddons\Widgets\Ecafe_Toolset_Video',
				'ecafe-toolset-embedded-media'      => '\EcafeAddons\Widgets\Ecafe_Toolset_Embeddedmedia',
				'ecafe-toolset-email'               => '\EcafeAddons\Widgets\Ecafe_Toolset_Email',
				'ecafe-toolset-url'                 => '\EcafeAddons\Widgets\Ecafe_Toolset_Url',
				'ecafe-toolset-number'              => '\EcafeAddons\Widgets\Ecafe_Toolset_Number',
				'ecafe-toolset-phone'               => '\EcafeAddons\Widgets\Ecafe_Toolset_Phone',
				'ecafe-toolset-skype'               => '\EcafeAddons\Widgets\Ecafe_Toolset_Skype',
				'ecafe-toolset-date'                => '\EcafeAddons\Widgets\Ecafe_Toolset_Date',
				'ecafe-wck-text'                    => '\EcafeAddons\Widgets\Ecafe_Wck_Text',
				'ecafe-wck-textarea'                => '\EcafeAddons\Widgets\Ecafe_Wck_Textarea',
				'ecafe-wck-wysiwyg-editor'          => '\EcafeAddons\Widgets\Ecafe_Wck_Wysiwygeditor',
				'ecafe-wck-currency-select'         => '\EcafeAddons\Widgets\Ecafe_Wck_Currencyselect',
				'ecafe-wck-phone'                   => '\EcafeAddons\Widgets\Ecafe_Wck_Phone',
				'ecafe-wck-timepicker'              => '\EcafeAddons\Widgets\Ecafe_Wck_Timepicker',
				'ecafe-wck-number'                  => '\EcafeAddons\Widgets\Ecafe_Wck_Number',
				'ecafe-wck-upload-image'            => '\EcafeAddons\Widgets\Ecafe_Wck_Uploadimage',
				'ecafe-wck-upload-video'            => '\EcafeAddons\Widgets\Ecafe_Wck_Uploadvideo',
			);

			if ( has_filter( 'ecafep_register_widgets' ) ) {
				$combined = apply_filters( 'ecafep_register_widgets', $combined );
			}

			$optionFetch = \EcafeHelper::ecafeGetOption( 'general', 'widgetsload' );
			if ( ! empty( $optionFetch ) ) {
				array_push( $optionFetch, 'ecafe-widgets' );
				foreach ( $combined as $widgetId => $className ) {
					if ( in_array( $widgetId, $optionFetch ) ) {
						if ( $this->carryWidgets( $widgetId, true ) ) {
							$widgetsexEcutive->register( new $className() );
						}
					}
				}
			}
		}

		/**
		 * Carry widgets
		 */
		public function carryWidgets( $widgetId, $combined = false ) {
			$filename = sprintf( 'element/widgets/' . $widgetId . '.php' );
			$ec_path  = ECAFE_PATH;
			if ( defined( 'ECAFEP_VERSION' ) && defined( 'ECAFEP_PATH' ) ) {
				$ec_path = ECAFEP_PATH;
			}

			if ( file_exists( $ec_path . $filename ) ) {
				require $ec_path . $filename;
				return true;
			} elseif ( file_exists( ECAFE_PATH . $filename ) ) {
				require ECAFE_PATH . $filename;
				return true;
			} else {
				return false;
			}
		}

		/**
		 * Extension Includes
		 */
		public function extensionWidgets( $widgetId, $combined = false ) {

			$filename = sprintf( 'element/extension/' . $widgetId . '.php' );
			$ec_path  = ECAFE_PATH;
			if ( defined( 'ECAFEP_VERSION' ) && defined( 'ECAFEP_PATH' ) ) {
				$ec_path = ECAFEP_PATH;
			}

			if ( file_exists( $ec_path . $filename ) ) {
				require $ec_path . $filename;
				return true;
			} elseif ( file_exists( ECAFE_PATH . $filename ) ) {
				require ECAFE_PATH . $filename;
				return true;
			} else {
				return false;
			}
		}

		/**
		 * Instance fetch
		 */
		public static function instanceFetch( $handlers = array() ) {

			if ( null == self::$instance ) {
				self::$instance = new self( $handlers );
			}
			return self::$instance;
		}
	}

}

function ecafeWidgetsPassed() {
	return ecafeWidgetsPassed::instanceFetch();
}
