<?php

namespace HulkPlugins\ElementorPro\Forms\InternationalTelephoneField;

use Elementor\Plugin;
use ElementorPro\Modules\Forms\Registrars\Form_Fields_Registrar;
use ElementorPro\Modules\Forms\Widgets\Form;
use HulkPlugins\ElementorPro\Forms\InternationalTelephoneField\Traits\SingletonTrait;

defined( 'ABSPATH' ) || exit;

class Base {
	use SingletonTrait;

	private string $pluginFile = 'international-telephone-field-for-elementor-form/international-telephone-field-for-elementor-form.php';
	private string $pluginFilePro = 'international-telephone-field-for-elementor-form-premium/international-telephone-field-for-elementor-form.php';

	public function init_hooks(): void {
		add_action( 'admin_menu', [ $this,'admin_menu' ], 1000 );
		add_filter( "plugin_action_links_$this->pluginFile", [ $this, 'plugin_action_links' ] );
		add_filter( "plugin_action_links_$this->pluginFilePro", [ $this, 'plugin_action_links' ] );
		add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
		add_action( 'admin_init', [ $this, 'is_elementor_pro_exist' ] );
        add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'editor_enqueue_scripts' ] );
		add_action( 'elementor_pro/forms/fields/register', [ $this, 'register_fields' ] );
		add_filter( 'elementor/widget/print_template', [ $this, 'print_template' ], PHP_INT_MAX, 2 );
		hulk_epfitf_fs()->add_action( 'after_uninstall', [ $this, 'uninstall' ] );
	}

	/**
	 * Add sub menu page under elementor
	 * @return void
	 */
	public function admin_menu(): void {
		add_submenu_page(
			'elementor',
			esc_attr__( 'Telephone Field', 'international-telephone-field-for-elementor-form' ),
			esc_attr__( 'Telephone Field', 'international-telephone-field-for-elementor-form' ),
			'manage_options',
			'hulk-epfitf-telephone-field',
			[ $this,'render_submenu_page' ]
		);
	}

	/**
	 * Render settings page
	 * @return void
	 */
	public function render_submenu_page(): void {
		$settings_asset = require PLUGIN_DIR_PATH . 'build/admin/settings/index.asset.php';

		wp_enqueue_style(
			PREFIX . 'admin-settings',
			PLUGIN_DIR_URL . 'build/admin/settings/index.css',
			[],
			$settings_asset['version'],
		);

		wp_enqueue_script(
			PREFIX . 'admin-settings',
			PLUGIN_DIR_URL . 'build/admin/settings/index.js',
			$settings_asset['dependencies'],
			$settings_asset['version'],
			true
		);

		require PLUGIN_DIR_PATH . 'includes/render-settings.php';
	}

	/**
	 * Filters the form template content before it's printed in the editor.
	 * @param $template_content
	 * @param $widget
	 *
	 * @return array|mixed|string|string[]
	 */
	public function print_template( $template_content, $widget ) {
		/** @var Form $widget */
		if ( $widget->get_name() === 'form' ) {
			return '';
		}
		return $template_content;
	}

	/**
	 * Filters the list of action links displayed for a specific plugin in the Plugins list table.
	 *
	 * @param $links
	 *
	 * @return array
	 */
	public function plugin_action_links( $links ): array {
		$custom = [
			sprintf(
			/** @lang text */                '<a href="%s">%s</a>',
				esc_url( SETTINGS_URL ),
				esc_attr__( 'Settings', 'international-telephone-field-for-elementor-form' )
			),
		];

		return array_merge( $custom, $links );
	}

	/**
	 * Add plugin meta
	 *
	 * @param $links
	 * @param $file
	 *
	 * @return array|mixed
	 */
	public function plugin_row_meta( $links, $file ) {

		if ( ( $file === $this->pluginFile ) || ( $file === $this->pluginFilePro ) ) {

			$row_meta['docs'] = sprintf(
			/** @lang text */                '<a target="_blank" href="%1$s" title="%2$s">%2$s</a>',
				esc_url( DOCUMENTATION ),
				esc_html__( 'Docs', 'international-telephone-field-for-elementor-form' )
			);

			$row_meta['support'] = sprintf(
			/** @lang text */                '<a target="_blank" href="%1$s">%2$s</a>',
				esc_url( SUPPORT ),
				esc_html__( 'Help &amp; Support', 'international-telephone-field-for-elementor-form' )
			);

			$links = array_merge( $links, $row_meta );
		}

		return $links;
	}

	/**
	 * Function used to deactivate the plugin if "elementor pro" does not exist
	 *
	 * @return bool
	 */
	public function is_elementor_pro_exist(): bool {
		if ( is_plugin_active( 'elementor-pro/elementor-pro.php' ) ) {
			return true;
		}
		// If neither plugin is active, show an admin notice.
		add_action( 'admin_notices', [ $this, 'admin_notice_missing_required_plugin' ] );
		return false;
	}

	/**
	 * Show notice to enable Elementor Pro
	 * @return void
	 */
	public function admin_notice_missing_required_plugin(): void {
		$message = sprintf(
		// translators: %1$s replace with plugin name & %2$s replace with "Elementor Pro".
			esc_html__(
				'%1$s requires %2$s to be installed and activated.',
				'international-telephone-field-for-elementor-form'
			),
			esc_html__( 'International Telephone Field For Elementor Form', 'international-telephone-field-for-elementor-form' ),
			esc_html__( 'Elementor Pro', 'international-telephone-field-for-elementor-form' ),
		);
		printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', esc_html( $message ) );
		deactivate_plugins( 'international-telephone-field-for-elementor-form/international-telephone-field-for-elementor-form.php' );
	}

	/**
	 * After editor enqueue scripts.
	 *
	 * Fires after Elementor editor scripts are enqueued.
	 */
	public function editor_enqueue_scripts(): void {

		$editor_asset = require PLUGIN_DIR_PATH . 'build/editor/index.asset.php';

		wp_enqueue_style(
			PREFIX . 'editor',
			PLUGIN_DIR_URL . 'build/editor/index.css',
			[],
			$editor_asset['version'],
		);

		wp_enqueue_script(
			PREFIX . 'editor',
			PLUGIN_DIR_URL . 'build/editor/index.js',
			[ 'jquery', 'elementor-editor' ],
			$editor_asset['version'],
			true
		);
	}

	/**
	 * Elementor Pro form fields registration.
	 * @param Form_Fields_Registrar $formFieldsRegistrar
	 *
	 * @return void
	 */
	public function register_fields( Form_Fields_Registrar $formFieldsRegistrar ): void {
		$formFieldsRegistrar->register( new Field() );
	}

	/**
	 * Freemius uninstall hook
	 * @return void
	 */
	public function uninstall(): void {
		// Check if Elementor is loaded
		if ( did_action( 'elementor/loaded' ) ) {
			// Clear Elementor cache
			Plugin::instance()->files_manager->clear_cache();
		}
	}
}
