<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


use Elementor\Plugin;
use Elementor\Plugin as Elementor;
use Prodigy\Includes\Prodigy;
use Prodigy\Includes\Support\Addons\Elementor\Widgets\{Prodigy_Cart,
	Prodigy_Cart_Page,
	Prodigy_Categories,
	Prodigy_Category,
	Prodigy_My_Account,
	Prodigy_Products_Brands,
	Prodigy_Redemption_Rules,
	Prodigy_Related_Products,
	Prodigy_Search,
	Prodigy_Thank_You_Page};
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Archive\{Prodigy_Active_Filters,
	Prodigy_Breadcrumbs,
	Prodigy_Filter_Price,
	Prodigy_Filters,
	Prodigy_Products,
	Prodigy_Sorting};
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Archive\Prodigy_Categories as Archive_Categories;
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Product\{Prodigy_Add_To_Cart,
	Prodigy_Additional_Information,
	Prodigy_Brands,
	Prodigy_Images,
	Prodigy_Meta,
	Prodigy_Price_Breaks,
	Prodigy_Price_Range,
	Prodigy_Rating,
	Prodigy_Review,
	Prodigy_Short_Description,
	Prodigy_Tabs,
	Prodigy_Title};
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Product\Prodigy_Breadcrumbs as Product_Breadcrumbs;

/**
 *
 * Addons For Elementor plugin.
 *
 * @package WordPress
 * @subpackage PAE
 */
class ProdigyElementor {

	public $version;
	private $file;

	protected $plugin_url;
	private $plugin_path;

	const MINIMUM_PHP_VERSION = '7.0';

	/**
	 * __construct function.
	 *
	 * @access public
	 * @return void
	 */
	public function __construct( $file ) {
		global $prodigy_css_path;

		$this->version = '';
		$this->file    = $file;

		/* Plugin URL/path settings. */
		$this->plugin_url  = str_replace( '/classes', '', plugins_url( plugin_basename( __DIR__ ) ) );
		$this->plugin_path = str_replace( 'classes', '', plugin_dir_path( __FILE__ ) );

		$prodigy_css_path = site_url( '/wp-content/plugins/' . PRODIGY_PLUGIN_DIRECTORY_NAME . '/includes/support/addons/elementor/css/', '' );
	}

	/**
	 * init function.
	 *
	 * @access public
	 * @return void
	 */
	public function init() {
		// Register Widget Styles
		add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'pae_editor_enqueue_scripts' ) );

		// Check for required PHP version
		if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_php_version' ) );
			return;
		}

		// Check if Elementor installed and activated
		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_missing_main_plugin' ) );
			return;
		}

		// Add Plugin actions
		add_action( 'elementor/widgets/register', array( $this, 'init_widgets' ) );
		$this->set_main_prodigy_category();
		add_action( 'elementor/elements/categories_registered', array( $this, 'add_elementor_widget_categories' ) );
		// Run this on activation.
		register_activation_hook( $this->file, array( $this, 'activation' ) );
	}

	public function set_main_prodigy_category() {
		Elementor::$instance->elements_manager->add_category(
			'prodigy-addons',
			array(
				'title' => __( 'Prodigy Commerce', 'prodigy-commerce' ),
				'icon'  => 'fa fa-plug',
			)
		);
	}

	public function pae_editor_enqueue_scripts() {
		wp_enqueue_style(
			'prdg-icon',
			$this->safe_url( PRODIGY_PLUGIN_URL . 'includes/support/addons/elementor/css/prgicon.css' ),
			false
		);

		wp_enqueue_style(
			'eicon-icon',
			$this->safe_url( PRODIGY_PLUGIN_URL . 'includes/support/addons/elementor/css/eicon.css' ),
			false
		);

		// Enqueue script to unlock Cloud Templates
		wp_enqueue_script(
			'prodigy-unlock-cloud-templates',
			$this->safe_url( PRODIGY_PLUGIN_URL . 'includes/support/addons/elementor/js/unlock-cloud-templates.js' ),
			array( 'jquery', 'elementor-editor' ),
			PRODIGY_VERSION,
			true
		);
	}


	/**
	 * Admin notice
	 *
	 * Warning when the site doesn't have a minimum required PHP version.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function admin_notice_minimum_php_version() {
		if ( isset( $_GET['activate'] ) ) {
			unset( $_GET['activate'] );
		}

		$message = sprintf(
			/* translators: 1: plugin name, 2: PHP, 3: required PHP version */
			esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'prodigy-commerce' ),
			'<strong>' . esc_html__( 'Prodigy Addons for Elementor', 'prodigy-commerce' ) . '</strong>',
			'<strong>' . esc_html__( 'PHP', 'prodigy-commerce' ) . '</strong>',
			self::MINIMUM_PHP_VERSION
		);

		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', esc_html( $message ) );
	}

	/**
	 * Admin notice
	 *
	 * Warning when the site doesn't have Elementor installed or activated.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function admin_notice_missing_main_plugin() {
		if ( isset( $_GET['activate'] ) ) {
			unset( $_GET['activate'] );
		}

		$message = sprintf(
			/* translators: 1: plugin name, 2: Elementor */
			esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'prodigy-commerce' ),
			'<strong>' . esc_html__( 'Prodigy Addons for Elementor', 'prodigy-commerce' ) . '</strong>',
			'<strong><a href="https://elementor.com/?ref=14615">' . esc_html__( 'Elementor Page Builder', 'prodigy-commerce' ) . '</a></strong>'
		);

		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', esc_html( $message ) );
	}

	public function add_elementor_widget_categories( $elements_manager ) {
		$elements_manager->add_category(
			'prodigy-product',
			array(
				'title' => __( 'Prodigy Product', 'prodigy-commerce' ),
				'icon'  => 'fa fa-plug',
			)
		);
	}

	/**
	 * Init Widgets
	 *
	 * Include widgets files and register them
	 *
	 * @throws Exception
	 * @since 1.0.0
	 *
	 * @access public
	 */
	public function init_widgets() {
		// Include Widget files
		$saved_options = get_option( 'pg_save_data' );

		require_once $this->plugin_path . '/widgets/class-prodigy-products.php';
		wp_enqueue_script( 'slick.min', Prodigy::plugin_url() . '/web/templates/js/slick.min.js', array( 'jquery' ), PRODIGY_VERSION, false );
		Plugin::instance()->widgets_manager->register( new \Prodigy\Includes\Support\Addons\Elementor\Widgets\Prodigy_Products() );

		require_once $this->plugin_path . '/widgets/class-prodigy-my-account.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_My_Account() );

		if ( isset( $saved_options['categories'] ) ) {
			require_once $this->plugin_path . '/widgets/class-prodigy-categories.php';
			Plugin::instance()->widgets_manager->register( new Prodigy_Categories() );
		}

		require_once $this->plugin_path . '/widgets/class-prodigy-cart.php';
		Plugin::instance()->widgets_manager->register(
			new Prodigy_Cart()
		);

		require_once $this->plugin_path . '/widgets/class-prodigy-cart-page.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Cart_Page() );

		require_once $this->plugin_path . '/widgets/class-prodigy-thank-you-page.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Thank_You_Page() );

		require_once $this->plugin_path . '/widgets/class-prodigy-search.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Search() );

		require_once $this->plugin_path . '/widgets/class-products-brands.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Products_Brands() );

		require_once $this->plugin_path . '/widgets/class-prodigy-redemption-rules.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Redemption_Rules() );

		/*
		 * Archive
		 */
		if ( isset( $saved_options['category'] ) ) {
			require_once $this->plugin_path . '/widgets/class-prodigy-category.php';
			Plugin::instance()->widgets_manager->register( new Prodigy_Category() );
		}

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-breadcrumbs.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Breadcrumbs() );

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-sorting.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Sorting() );

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-filters.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Filters() );

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-products.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Products() );

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-filter-price.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Filter_Price() );

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-categories.php';
		Plugin::instance()->widgets_manager->register( new Archive_Categories() );

		require_once $this->plugin_path . '/widgets/archive/class-prodigy-active-filters.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Active_Filters() );

		/*
		 * Single Product
		 */
		require_once $this->plugin_path . '/widgets/product/class-prodigy-breadcrumbs.php';
		Plugin::instance()->widgets_manager->register( new Product_Breadcrumbs() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-title.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Title() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-short-description.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Short_Description() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-price-range.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Price_Range() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-rating.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Rating() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-add-to-cart.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Add_To_Cart() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-brands.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Brands() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-images.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Images() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-meta.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Meta() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-tabs.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Tabs() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-related-products.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Related_Products() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-additional-information.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Additional_Information() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-price-breaks.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Price_Breaks() );

		require_once $this->plugin_path . '/widgets/product/class-prodigy-review.php';
		Plugin::instance()->widgets_manager->register( new Prodigy_Review() );
	}

	/**
	 * activation function.
	 *
	 * @access public
	 * @return void
	 */
	public function activation() {
		$this->register_plugin_version();

		if ( get_option( 'pg_save_data' ) === false ) {
			$vc_default_options = array(
				'products'   => 'on',
				'categories' => 'on',
				'category'   => 'on',
			);

			update_option( 'pg_save_data', $vc_default_options );
		}
	}

	/**
	 * register_plugin_version function.
	 *
	 * @access public
	 * @return void
	 */
	public function register_plugin_version() {
		if ( $this->version != '' ) {
			update_option( 'pa_elementor' . '-version', $this->version );
		}
	}


	/**
	 * Generate safe url
	 *
	 * @since v3.0.0
	 */
	public function safe_url( $url ) {
		if ( is_ssl() ) {
			$url = wp_parse_url( $url );

			if ( ! empty( $url['host'] ) ) {
				$url['scheme'] = 'https';
			}

			return $this->unparse_url( $url );
		}

		return $url;
	}

	public function unparse_url( $parsed_url ): string {
		$scheme   = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '';
		$host     = $parsed_url['host'] ?? '';
		$port     = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : '';
		$user     = $parsed_url['user'] ?? '';
		$pass     = isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : '';
		$pass     = ( $user || $pass ) ? "$pass@" : '';
		$path     = $parsed_url['path'] ?? '';
		$query    = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '';
		$fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '';

		return "$scheme$user$pass$host$port$path$query$fragment";
	}
}
