<?php
namespace Jet_Engine_Divi_Views\Wizard;

use Jet_Engine_Divi_Views\Plugin as Plugin;

/**
 * Class description
 *
 * @package   package_name
 * @author    Cherry Team
 * @license   GPL-2.0+
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

class Manager {

	/**
	 * @var string
	 */
	public $page_slug = 'divi-views-wizard';

	public $redirect_option = 'jedv_wizard_activation_redirect';

	public $redirect_page = 'divi-views-wizard';

	/**
	 * @var null
	 */
	private static $instance = null;

	/**
	 * @return Wizard|null
	 */
	public static function get_instance() {

		// If the single instance hasn't been set, set it now.
		if ( null == self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	}

	/**
	 * [load_files description]
	 * @return [type] [description]
	 */
	public function load_files() {}

	/**
	 * Init comnonents
	 */
	public function init_components() {}

	/**
	 * [register_page description]
	 * @return [type] [description]
	 */
	public function register_page() {
		add_menu_page(
			esc_html__( 'Divi Wizard', 'jet-engine-divi-views' ),
			esc_html__( 'Divi Wizard', 'jet-engine-divi-views' ),
			'manage_options',
			$this->page_slug,
			array( $this, 'render_page' ),
			'dashicons-admin-customizer',
			56
		);

		remove_menu_page( $this->page_slug );
	}

	/**
	 * [render_page description]
	 * @return [type] [description]
	 */
	public function render_page() {
		echo '<div id="divi-views-wizard" class="divi-views-wizard"></div>';
	}

	/**
	 * @return array
	 */
	public function get_divi_compatibility_plugins() {
		$divi_compatibility_plugins = apply_filters( 'jet-engine-divi-views/divi-compatibility-plugins',
			[ 'jet-engine/jet-engine.php', 'jet-smart-filters/jet-smart-filters.php' ]
		);

		$all_jet_plugins = \Jet_Dashboard\Dashboard::get_instance()->plugin_manager->get_plugin_data_list();

		$all_jet_plugins = array_map( function ( $plugin_data ) {

			switch ( $plugin_data['slug'] ) {
				case 'jet-smart-filters/jet-smart-filters.php':
					$plugin_data['desc_alt'] = __( 'Add instant filtering to products and custom post types, smart search, location awareness, and smooth browsing to any website - turning visitors into engaged users who find what they want faster and buy more often', 'jet-engine-divi-views' );
					break;

				case 'jet-engine/jet-engine.php':
					$plugin_data['desc_alt'] = __( 'Create business directories, job boards, event calendars, product catalogs, and more - all featuring maps, ratings, wishlists and personalized content display.', 'jet-engine-divi-views' );;
					break;

				default:
					$plugin_data['desc_alt'] = false;
					break;
			}

			return $plugin_data;
		}, $all_jet_plugins );

		return array_values( array_filter( $all_jet_plugins, function( $plugin_data ) use ( $divi_compatibility_plugins ) {
			return in_array( $plugin_data['slug'], $divi_compatibility_plugins );
		} ) );
	}

	/**
	 * Template type popup assets
	 *
	 * @return void
	 */
	public function enqueue_scripts() {

		$screen = get_current_screen();

		if ( 'toplevel_page_divi-views-wizard' !== $screen->id ) {
			return false;
		}

		$module_data = Plugin::instance()->framework->get_included_module_data( 'cherry-x-vue-ui.php' );
		$ui          = new \CX_Vue_UI( $module_data );
		$ui->enqueue_assets_components();

		wp_enqueue_script(
			'divi-views-wizard-script',
			JET_ENGINE_DIVI_VIEWS_URL . 'includes/wizard/assets/builder/js/app.js', [
				'jquery',
				'wp-api-fetch',
				'cx-vue-ui-components'
			],
			JET_ENGINE_DIVI_VIEWS_VERSION,
			true
		);

		wp_localize_script(
			'divi-views-wizard-script',
			'wizardConfig',
			apply_filters(
				'jet-engine-divi-views/wizard/localize-config',
				[
					'createPageTemplatePath' => 'jet-theme-core-api/v2/create-page-template',
					'ajaxUrl'                => esc_url( admin_url( 'admin-ajax.php' ) ),
					'nonce'                  => wp_create_nonce( \Jet_Dashboard\Dashboard::get_instance()->dashboard_slug ),
					'jetEngineNonce'         => wp_create_nonce( 'jet-engine-dashboard' ),
					'licenseList'            => array_values( \Jet_Dashboard\Utils::get_license_list() ),
					'primaryLicenseData'     => \Jet_Dashboard\Dashboard::get_instance()->license_manager->get_primary_license_data(),
					'compatibilityPlugins'   => $this->get_divi_compatibility_plugins(),
					'config' => [
						'jetEngineLinks' => [
							[
								'slug' => 'custom-post-type',
								'name' => __( 'Custom Post Type', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-engine-cpt' ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Create and manage custom post types for your project content.', 'jet-engine-divi-views' ),
							],
							[
								'slug' => 'listings',
								'name' => __( 'Listings', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'post_type' => 'jet-engine' ], admin_url( 'edit.php' ) ),
								'desc' => __( 'Build listing templates to display posts, terms, users, and custom data.', 'jet-engine-divi-views' ),
							],
							[
								'slug' => 'custom-taxonomy',
								'name' => __( 'Custom Taxonomy', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-engine-cpt-tax' ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Add custom taxonomies to organize and filter your content.', 'jet-engine-divi-views' ),
							],
							[
								'slug' => 'query',
								'name' => __( 'Query', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-engine-query' ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Create custom queries to control which items appear in listings.', 'jet-engine-divi-views' ),
							],
						],
						'smartFiltersLinks' => [
							[
								'slug' => 'filter-providers',
								'name' => __( 'Enable filter providers (sources)', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-dashboard-settings-page', 'subpage' => 'jet-smart-filters-general-settings' ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Turn on the providers you want to use as filter sources across the site.', 'jet-engine-divi-views' ),
							],
							[
								'slug' => 'create-filters',
								'name' => __( 'Create filters', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-smart-filters' ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Set up new filters and manage existing ones for your listings.', 'jet-engine-divi-views' ),
							],
							[
								'slug' => 'indexer-settings',
								'name' => __( 'Indexer settings', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-dashboard-settings-page', 'subpage' => 'jet-smart-filters-indexer-settings'  ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Configure the indexer to improve filter counts and performance.', 'jet-engine-divi-views' ),
							],
							[
								'slug' => 'seo-rules',
								'name' => __( 'Use SEO and Sitemap rules', 'jet-engine-divi-views' ),
								'link' => add_query_arg( [ 'page' => 'jet-dashboard-settings-page', 'subpage' => 'jet-smart-filters-seo-rules-settings'  ], admin_url( 'admin.php' ) ),
								'desc' => __( 'Control SEO behavior and sitemap rules for filtered pages.', 'jet-engine-divi-views' ),
							],

						],
						'licenseUpgrageLink' => '#',
						'pricingLink' => '#',
						'buildAILink' => add_query_arg( [ 'page' => 'website-builder' ], admin_url( 'admin.php' ) ),
						'jetDashboardLink' => add_query_arg( [ 'page' => 'jet-dashboard' ], admin_url( 'admin.php' ) ),
					]
				]
			)
		);
	}

	/**
	 * @return void
	 */
	public function maybe_redirect_after_activation() {

		if ( ! get_option( $this->redirect_option ) ) {
			return;
		}

		delete_option( $this->redirect_option );

		if (
			isset( $_GET['activate-multi'] )
			|| is_network_admin()
			|| ( defined( 'WP_CLI' ) && WP_CLI )
			|| ( defined( 'DOING_AJAX' ) && DOING_AJAX )
		) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$target = admin_url( 'admin.php?page=' . rawurlencode( $this->redirect_page ) );

		if ( isset( $_GET['page'] ) && $_GET['page'] === $this->redirect_page ) {
			return;
		}

		wp_safe_redirect( $target );
		exit;
	}

	/**
	 * @param $links
	 * @return mixed
	 */
	public function plugin_action_links( $links ) {
		$links['wizard_link'] = sprintf( '<a href="%1$s" target="_blank"><b>%2$s</b></a>', add_query_arg( [ 'page' => 'divi-views-wizard' ], admin_url( 'admin.php' ) ), esc_html__( 'Go to Wizard', 'jet-engine-divi-views' ) );

		return $links;
	}


	/**
	 * Constructor for the class
	 */
	public function __construct() {
		$this->load_files();

		add_action( 'jet-engine-divi-views/init', [ $this, 'init_components' ] );

		if ( is_admin() ) {
			add_action( 'admin_menu', [ $this, 'register_page' ], 20 );
			add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
			add_action( 'admin_init', [ $this, 'maybe_redirect_after_activation' ] );
		}

		add_filter( 'plugin_action_links_' . JET_ENGINE_DIVI_VIEWS_PLUGIN_BASE, [ $this, 'plugin_action_links' ] );

	}

}
