<?php
namespace EVSS\Admin;

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

if ( !class_exists( 'Submenu' ) ) {
	class Submenu {
		public function __construct() {
			add_action( 'admin_menu', [ $this, 'adminMenu' ] );
			add_action( 'admin_enqueue_scripts', [ $this, 'adminEnqueueScripts' ] );
		}

		function adminMenu() {
			add_submenu_page(
				'tools.php',
				'Swipe Slider - Plugin Envision',
				'Swipe Slider',
				'manage_options',
				'swipe-slider',
				function() {
					$evssHasFSInfo = EVSS_HAS_FS ? [
						'hasFS' => true,
						'plan' => [
							'name' => 'PLAN_NAME' === evss_fs()->get_plan_name() ? 'free' : evss_fs()->get_plan_name(), 
							'title' => 'PLAN_TITLE' === evss_fs()->get_plan_title() ? '' : evss_fs()->get_plan_title()
						],
						'miftah' => is_object( evss_fs()->_get_license() ) ? evss_fs()->_get_license()->secret_key : ''
					] : [];
				?>
					<div id='evssDashboard' data-info='<?php echo esc_attr( json_encode( array_merge( [ 'version' => EVSS_VERSION, 'plan' => [ 'name' => 'free' ] ], $evssHasFSInfo ) ) ); ?>'></div>
				<?php }
			);
		}

		function adminEnqueueScripts( $path ){
			if( strpos( $path, 'swipe-slider' ) !== false ) {
				wp_enqueue_style( 'evss-admin-dashboard', EVSS_DIR_URL . 'build/admin/dashboard.css', [], EVSS_VERSION );

				$dependencies = [ 'react', 'react-dom', 'wp-i18n', 'wp-data', 'wp-util' ];

				$asset_file = EVSS_DIR_PATH . 'build/admin/dashboard.asset.php';
				if ( file_exists( $asset_file ) ) {
					$asset = require_once $asset_file;
					$dependencies = array_merge( $asset['dependencies'], [ 'wp-util' ] );
				}

				wp_enqueue_script( 'evss-admin-dashboard', EVSS_DIR_URL . 'build/admin/dashboard.js', $dependencies, EVSS_VERSION, true );

				wp_set_script_translations( 'evss-admin-dashboard', 'swipe-slider', EVSS_DIR_PATH . 'languages' );
			}
		}
	}
	new Submenu();
}