<?php

namespace HulkPlugins\ElementorPro\Forms\AdvancedFields\Controllers;

use HulkPlugins\ElementorPro\Forms\AdvancedFields\Services\SettingsService;
use HulkPlugins\ElementorPro\Forms\AdvancedFields\Traits\SingletonTrait;
use function HulkPlugins\ElementorPro\Forms\AdvancedFields\hulk_affef_fs;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\DEMO;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\DOCUMENTATION;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\GROUP;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\PLUGIN_DIR_PATH;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\PLUGIN_DIR_URL;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\PREFIX;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\REVIEWS;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\SUPPORT;
use const HulkPlugins\ElementorPro\Forms\AdvancedFields\VERSION;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class SettingsController {
	use SingletonTrait;

	private string $page = 'advanced-fields-for-elementor-forms-settings';
	private SettingsService $service;
	private static SettingsController $instance;

	private function __construct( SettingsService $service ) {
        $this->service = $service;
	}

	public static function getInstance( SettingsService $service ): self {
		if ( ! isset( self::$instance ) ) {
			self::$instance = new self( $service );
		}

		return self::$instance;
	}

	public function init_hooks(): void {
		add_action( 'admin_menu', [ $this,'admin_menu' ], 1000 );
		add_filter( 'elementor/admin-top-bar/is-active', [ $this, 'admin_top_bar' ], PHP_INT_MAX, 2 );
		add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ], PHP_INT_MAX );
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
	}

	/**
	 * Add settings page under elementor
	 * @return void
	 */
	public function admin_menu(): void {
		add_submenu_page(
			'elementor',
			esc_attr__( 'Advanced Fields', 'advanced-fields-for-elementor-forms' ),
			esc_attr__( 'Advanced Fields', 'advanced-fields-for-elementor-forms' ),
			'manage_options',
			'advanced-fields-for-elementor-forms-settings',
			[ $this, 'render_settings_page' ],
		);
	}

	/**
	 * Hide the elementor admin top bar on AdvancedFields settings page
	 * @param $is
	 * @param $current_screen
	 *
	 * @return false|mixed
	 */
	public function admin_top_bar( $is, $current_screen ) {
		if ( str_contains( $current_screen->id, $this->page ) ) {
			return false;
		}
		return $is;
	}

	/**
	 * Enqueues scripts and styles for settings page
	 * @return void
	 */
	public function enqueue_admin_scripts(): void {

		if ( ! wp_script_is( 'react-jsx-runtime', 'registered' ) ) {
			wp_register_script(
				'react-jsx-runtime',
				PLUGIN_DIR_URL . 'assets/js/react-jsx-runtime.js',
				[ 'react' ],
				'18.3.1',
				true
			);
		}

		$assets = require PLUGIN_DIR_PATH . 'build/admin/settings/index.asset.php';

        wp_register_style(
            'advanced-fields-for-elementor-forms-settings',
            PLUGIN_DIR_URL . 'build/admin/settings/index.css',
            [ 'wp-components' ],
	        $assets['version'],
        );

        wp_register_script(
            'advanced-fields-for-elementor-forms-settings',
            PLUGIN_DIR_URL . 'build/admin/settings/index.js',
	        $assets['dependencies'],
	        $assets['version'],
            true
        );
	}

	/**
	 * Render settings
	 * @return void
	 */
	public function render_settings_page(): void {
		echo /** @lang text */ '<div class="hulk-settings" id="hulk-ffef-settings"></div>';
		wp_enqueue_script( 'advanced-fields-for-elementor-forms-settings-runtime' );
		wp_enqueue_style( 'advanced-fields-for-elementor-forms-settings' );
        wp_enqueue_script( 'advanced-fields-for-elementor-forms-settings' );

		// Load translation files for JavaScript
        wp_set_script_translations(
            'advanced-fields-for-elementor-forms-settings',
            'advanced-fields-for-elementor-forms',
            PLUGIN_DIR_PATH . 'languages'
        );

        wp_localize_script(
            'advanced-fields-for-elementor-forms-settings',
            PREFIX . 'settings',
            [
				'VERSION'         => VERSION,
				'SUPPORT'         => SUPPORT,
				'PREFIX'          => PREFIX,
				'GROUP'           => GROUP,
				'DOCUMENTATION'   => DOCUMENTATION,
				'DEMO'            => DEMO,
				'REVIEWS'         => REVIEWS,
				'PLUGIN_DIR_URL'  => PLUGIN_DIR_URL,
				'restUrl'         => rest_url(),
				'adminColor'      => get_user_option( 'admin_color', get_current_user_id() ),
				'nonce'           => wp_create_nonce( 'wp_rest' ),
				'settings'        => $this->service->getSettings(),
	            'supportedFields' => $this->service->getSupportedFields(),
	            'freemius'        => [
					'get_trial_url'        => hulk_affef_fs()->get_trial_url(),
					'can_use_premium_code' => hulk_affef_fs()->can_use_premium_code(),
					'is_premium'           => hulk_affef_fs()->is_premium(),
		            'is_not_paying'        => hulk_affef_fs()->is_not_paying(),
		            'checkout_url'         => hulk_affef_fs()->checkout_url(),
		            'get_upgrade_url'      => hulk_affef_fs()->get_upgrade_url(),
	            ],
            ]
        );
	}

	/**
	 * Admin footer text
	 * @param $text
	 *
	 * @return string
	 */
	public function admin_footer_text( $text ): string {
		$screen = get_current_screen();
		if ( str_contains( $screen->id, $this->page ) ) {
			return sprintf(
			/* translators: 1: Elementor, 2: Link to plugin review */
				__( 'Enjoyed %1$s? Please leave us a %2$s rating. We really appreciate your support!', 'advanced-fields-for-elementor-forms' ),
				'<strong>' . esc_html__( 'Advanced Fields For Elementor Forms', 'advanced-fields-for-elementor-forms' ) . '</strong>',
				'<a href="' . esc_url( REVIEWS ) . '" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>'
			);
		}
		return $text;
	}
}
