<?php

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

final class Sifency_Addons {

	private static $instance = null;

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

	/**
	 * It runs once when the plugin is activated.
	 */
	public static function activate() {
		update_option( 'sifency_addons_version', SIFENCY_ADDONS_VERSION );
	}

	private function __construct() {
		add_action( 'plugins_loaded', array( $this, 'init' ) );
		add_action( 'admin_init', array( $this, 'check_requirements_and_notices' ) );
		add_action( 'init', array( $this, 'sifency_addons_load_post_duplicator' ) );
		add_action(
			'plugins_loaded',
			function () {
				if ( is_admin() || did_action( 'elementor/loaded' ) ) {
					require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/sifency-ai/class-ai-module.php';
					Sifency_Addons_AI_Module::instance();
				}
			},
			20
		);
	}

	public function init() {
		if ( did_action( 'elementor/loaded' ) ) {
			require_once SIFENCY_ADDONS_DIR . 'base/load/assets-manager.php';
			\Sifency\Includes\Sifency_Addons_Elementor_Widgets_Asset_Manager::init();
		}
		$this->load_components();
		do_action( 'sifency_addons/loaded' );
		add_filter( 'template_include', array( $this, 'load_custom_post_type_templates' ) );
	}

	private function load_components() {
		require_once SIFENCY_ADDONS_DIR . 'base/load/assets-register.php';
		require_once SIFENCY_ADDONS_DIR . 'includes/elementor/class-widget-registry.php';
		require_once SIFENCY_ADDONS_DIR . 'includes/elementor/widget-filter-helpers.php';
		require_once SIFENCY_ADDONS_DIR . 'includes/elementor/elementor-init.php';
		require_once SIFENCY_ADDONS_DIR . 'includes/add-template/get-template.php';
		require_once SIFENCY_ADDONS_DIR . 'helpers/functions.php';
		require_once SIFENCY_ADDONS_DIR . 'helpers/post-summary-filter-helpers.php';
		require_once SIFENCY_ADDONS_DIR . 'helpers/template-inline-edit.php';
		require_once SIFENCY_ADDONS_DIR . 'helpers/theme-core-helpers.php';
		require_once SIFENCY_ADDONS_DIR . 'includes/custom-icon/custom-icons.php';
		require_once SIFENCY_ADDONS_DIR . 'theme-builder/theme-builder-loader.php';
		require_once SIFENCY_ADDONS_DIR . 'classes/class-admin-menu.php';
		require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/init.php';
		require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/data-management/helpers/class-wxr-settings.php';
		Sifency_Addons_WXR_Settings::instance();
		$is_core_active                           = class_exists( 'Sifency_Essential' );
		$sifency_addons_selective_environment_var = class_exists( 'Sifency_Addons_Theme_Builder_Master' ) &&
		Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment();
		if ( ! $is_core_active && ! $sifency_addons_selective_environment_var ) {
			require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/data-management/init.php';

		}
		require_once SIFENCY_ADDONS_DIR . 'classes/template-shortcode/init.php';
		require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/exporter/export.php';

		new \Sifency_Addons\Includes\Sifency_Addons_Elementor_Widgets_Asset_Manager();
		new \Sifency_Addons\Includes\Admin\Sifency_Addons_Admin_Widgets();
		new \Sifency_Addons\Includes\Elementor\Sifency_Addons_Elementor_Integration();
		new \Sifency\Admin\Elementor_Native_Exporter();
	}
	/**
	 * Loads custom templates for the plugin's custom post types.
	 *
	 * @param string $template The path to the template file.
	 * @return string The potentially modified template path.
	 */
	public function load_custom_post_type_templates( $template ) {
		$post_type_templates = array(
			'sifency_template' => 'templates/single-sifency_template.php',
			'themebuilder'     => 'templates/single-themebuilder.php',
			'templates'        => 'templates/single-section_templates.php',
		);

		$current_post_type = get_post_type();

		if ( is_singular() && isset( $post_type_templates[ $current_post_type ] ) ) {
			$custom_template = SIFENCY_ADDONS_DIR . $post_type_templates[ $current_post_type ];
			if ( file_exists( $custom_template ) ) {
				return $custom_template;
			}
		}

		return $template;
	}
	/**
	 * Check requirements and show notices in admin.
	 */
	public function check_requirements_and_notices() {
		$notice = \Sifency_Addons_Notice::instance();
		$notice->check_requirements();
	}

	public function sifency_addons_load_post_duplicator() {
		$file = SIFENCY_ADDONS_DIR . 'classes/admin-module/post-duplicator/post-duplicate.php';
		if ( file_exists( $file ) ) {
			include_once $file;
		}

	}

}
