<?php
namespace Sifency_Addons\Includes\Elementor;
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
class Sifency_Elementor_Integration {

	private $widgets_config = [];

	public function __construct() {
		$categorized_widgets = include SIFENCY_ADDONS_DIR . 'includes/elementor/sifency-widgets.php';
		$flat_widgets = [];
		foreach ( $categorized_widgets as $category_widgets ) {
			$flat_widgets = array_merge( $flat_widgets, $category_widgets );
		}
		$this->widgets_config = $flat_widgets;

		add_action( 'elementor/elements/categories_registered', [ $this, 'register_widget_categories' ] );
		add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] );
	}

	/**
	 * Registers custom widget categories for Elementor.
	 */
	public function register_widget_categories( $elements_manager ) {
		$elements_manager->add_category(
			'sifency-addons-widgets',
			[ 'title' => esc_html__( 'Sifency Addons', 'sifency-addons' ), 'icon' => 'eicon-plug' ]
		);
		$elements_manager->add_category(
			'sifency-advance-widgets',
			[ 'title' => esc_html__( 'Sifency Advance', 'sifency-addons' ), 'icon' => 'eicon-plug' ]
		);
		$elements_manager->add_category(
			'sifency-post-components',
			[ 'title' => esc_html__( 'Sifency Post Components', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		if ( class_exists( 'WooCommerce' ) ) {
			$elements_manager->add_category(
				'sifency-addons-woo-widgets',
				[ 'title' => esc_html__( 'Sifency WooCommerce', 'sifency-addons' ), 'icon' => 'eicon-woocommerce' ]
			);
		}
		$elements_manager->add_category(
			'member-post-components',
			[ 'title' => esc_html__( 'Sifency Member Details', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-event-components',
			[ 'title' => esc_html__( 'Sifency Event Details', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-career-components',
			[ 'title' => esc_html__( 'Sifency Career Details', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-portfolio-components',
			[ 'title' => esc_html__( 'Sifency Portfolio Details', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-document-components',
			[ 'title' => esc_html__( 'Sifency Docs Details', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-service-components',
			[ 'title' => esc_html__( 'Sifency Service Details', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-digital-components',
			[ 'title' => esc_html__( 'Sifency Digital Goods', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
		$elements_manager->add_category(
			'sifency-edd-components',
			[ 'title' => esc_html__( 'Sifency Easy Digital Downloads', 'sifency-addons' ), 'icon' => 'eicon-single-post' ]
		);
	}

	/**
	 * Includes helper files (traits) required by the widgets.
	 */
	private function include_helper_files() {
		$helper_files = [
			'includes/traits/button.php',
			'includes/traits/title.php',
			'includes/traits/image.php',
			'includes/traits/button-icon.php',
			'includes/traits/icon.php',
			'includes/traits/item-style.php',
			'includes/traits/animation-controls.php',
			'includes/traits/parity-controls.php',
			'includes/traits/post-arg-controls.php',
			'includes/traits/slider-navigation.php',
			'includes/traits/slider-pagination.php',
			'includes/traits/sifency_slider_controllers.php',

			'includes/traits/content/render-contet-meta.php',
			'includes/traits/content/label-content.php',
			'includes/traits/content/separator-content.php',

			'includes/elementor/component/marquee/controls.php',
			'includes/elementor/component/accordion/controls.php',
			'includes/elementor/component/button/controls.php',
			'includes/elementor/component/iconbox/controls.php',
			'includes/elementor/component/video/controls.php',
			'includes/elementor/component/image/controls.php',
			'includes/elementor/component/icon-list/controls.php',

			'includes/elementor/dynamic/author-box/controls.php',
			'includes/elementor/dynamic/comment/controls.php',
			'includes/elementor/dynamic/search/controls.php',

			'includes/elementor/post-type/post-navigator/controls.php',

			'includes/elementor/loop/blog/controls.php',
			'includes/elementor/loop/awards/controls.php',
			'includes/elementor/loop/career/controls.php',
			'includes/elementor/loop/events/controls.php',
			'includes/elementor/loop/clients/controls.php',
			'includes/elementor/loop/services/controls.php',
			'includes/elementor/loop/team/controls.php',
			'includes/elementor/loop/projects/controls.php',
			'includes/elementor/loop/testimonials/controls.php',

			'includes/elementor/plugins/form/controls.php',
			'includes/elementor/plugins/newsletter/controls.php',

			'includes/elementor/woocommerce/products-slider/controls.php',
			'includes/elementor/woocommerce/product-grid/controls.php',
			'includes/elementor/woocommerce/related-products/controls.php',
			'includes/elementor/woocommerce/product-image/controls.php',
			'includes/elementor/woocommerce/product-rating/controls.php',

			'includes/elementor/plugins/form/render.php',
			'includes/elementor/plugins/newsletter/render.php',
			'includes/elementor/plugins/template/render.php',
		];

		foreach ( $helper_files as $file ) {
			$file_path = SIFENCY_ADDONS_DIR . $file;
			if ( file_exists( $file_path ) ) {
				require_once $file_path;
			}
		}
		
	}

	/**
	 * Registers all enabled widgets with Elementor.
	 *
	 * It iterates through the flattened widget config, checks if a widget is enabled
	 * in the plugin settings, and then registers it.
	 *
	 * @param \Elementor\Widgets_Manager $widgets_manager The Elementor widgets manager.
	 */
	public function register_widgets( $widgets_manager ) {
		$this->include_helper_files();

		$enabled_widgets_settings = get_option( 'sifency_addons_settings', [] );
		$enabled_widgets          = $enabled_widgets_settings['widgets'] ?? [];
		$is_woo_active            = class_exists( 'WooCommerce' );

		foreach ( $this->widgets_config as $widget_id => $widget_data ) {
			$is_premium_widget = ! empty( $widget_data['premium'] );
			if ( $is_premium_widget && ! class_exists( '\\Sifency_Addons_Theme_Builder_Master' ) ) {
				continue;
			}
			if ( $is_premium_widget && ! \Sifency_Addons_Theme_Builder_Master::sifency_addons_is_legal() ) {
				continue;
			}

			$is_widget_enabled = ! isset( $enabled_widgets[ $widget_id ] ) || 'on' === $enabled_widgets[ $widget_id ];

			if ( ! $is_widget_enabled ) {
				continue;
			}

			if ( ! empty( $widget_data['is_woo'] ) && ! $is_woo_active ) {
				continue;
			}

			$file_path = SIFENCY_ADDONS_DIR . $widget_data['file'];
			if ( ! file_exists( $file_path ) ) {
				continue;
			}
			require_once $file_path;

			$class_name = $widget_data['class'];
			if ( class_exists( $class_name ) ) {
				$widgets_manager->register( new $class_name() );
			}
		}
	}
}