<?php

namespace StoreEngine\Admin;

use StoreEngine\Assets;
use StoreEngine\Utils\Helper;

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

class Setup {
	const PAGE_ID = 'storeengine-setup';

	private ?Assets $assets;

	public static function init() {
		$self         = new self();
		$self->assets = new Assets();
		if ( $self->is_current() ) {
			add_action( 'admin_init', [ $self, 'admin_init' ], 0 );
		}
		add_action( 'admin_menu', [ $self, 'register_admin_menu' ] );
	}

	public function register_admin_menu() {
		add_submenu_page(
			'options-writing.php', // make it hidden
			__( 'StoreEngine Setup', 'storeengine' ),
			__( 'StoreEngine Setup', 'storeengine' ),
			'manage_options',
			self::PAGE_ID
		);
	}


	public function admin_init() {
		do_action( 'storeengine/admin/setup/init', $this );

		$this->enqueue_assets();

		// Setup default heartbeat options
		add_filter( 'heartbeat_settings', function ( $settings ) {
			$settings['interval'] = 15;

			return $settings;
		} );

		$this->render();
		die;
	}

	public function is_current() {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return ( ! empty( $_GET['page'] ) && self::PAGE_ID === $_GET['page'] );
	}

	public function get_setup_scripts_data() {
		return apply_filters(
			'storeengine/admin/setup_scripts_data',
			array_merge(
				$this->assets->get_backend_script_data(),
				[
					'admin_url'     => admin_url(),
					'customize_url' => add_query_arg(
						'return',
						admin_url( 'admin.php?page=storeengine-settings&path=general' ),
						admin_url( 'customize.php' )
					),
				]
			)
		);
	}


	private function enqueue_assets() {
		wp_enqueue_style( 'storeengine-web-font', $this->assets->web_fonts_url( 'Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900' ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
		wp_enqueue_style(
			'storeengine-admin-icon',
			STOREENGINE_ASSETS_URI . 'library/icons/storeengine-icons.css',
			[ 'wp-components' ],
			filemtime(
				STOREENGINE_ASSETS_DIR_PATH .
				'library/icons/storeengine-icons.css'
			),
		);
		if ( Helper::is_plugin_active( 'ablocks/ablocks.php' ) ) {
			$Assets = new \ABlocks\Assets();
			$Assets->demo_template_importer_scripts();
		}

		$dependencies = include STOREENGINE_ASSETS_DIR_PATH . sprintf( 'build/setup.%s.asset.php', STOREENGINE_VERSION );
		wp_enqueue_style( 'storeengine-admin-style', STOREENGINE_ASSETS_URI . 'build/setup.css', array( 'wp-components' ), $dependencies['version'], 'all' );
		wp_add_inline_style( 'storeengine-admin-style', $this->assets->get_dynamic_css() );
		wp_enqueue_script( 'updates' );
		wp_enqueue_script(
			'storeengine-setup-scripts',
			STOREENGINE_ASSETS_URI . sprintf( 'build/setup.%s.js', STOREENGINE_VERSION ),
			$dependencies['dependencies'],
			$dependencies['version'],
			true
		);
		wp_localize_script( 'storeengine-setup-scripts', 'StoreEngineGlobal', $this->get_setup_scripts_data() );
		// Enqueue emoji styles to prevent deprecation notices
		wp_enqueue_emoji_styles();
	}

	private function render() {
		require STOREENGINE_ROOT_DIR_PATH . 'includes/admin/views/setup.php';
	}
}
