<?php

namespace WPAdminify\Pro;

use WPAdminify\Inc\Classes\Assets;
use WPAdminify\Inc\Classes\GoogleFontsLocal;

// no direct access allowed
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Pro-only asset enqueueing.
 *
 * The Customize > Body Font feature (option key
 * `admin_general_google_font`) is Pro-only. The free build of
 * Inc/Classes/Assets.php no longer touches that option; this Pro
 * subclass owns the body Google Font enqueue + local download flow.
 *
 * @author Jewel Theme <support@jeweltheme.com>
 */
class Assets_Pro extends Assets {

	/**
	 * Constructor.
	 *
	 * Note: the parent Inc/Classes/Assets is already instantiated by
	 * class-wp-adminify.php and registers its own admin_enqueue_scripts
	 * callback. We do NOT call parent::__construct() here to avoid
	 * double registration. We register an additional callback at a
	 * later priority so the Pro body font enqueue runs after the
	 * parent's main asset enqueue finishes.
	 */
	public function __construct() {
		$this->options = (array) \WPAdminify\Inc\Admin\AdminSettings::get_instance()->get();

		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_pro_body_google_font' ], 110 );

		// Pro-only settings flags injected into WP_ADMINIFY_ADMIN
		// (currently menu_search). Free leaves the localize array
		// untouched (see Inc/Classes/Assets.php).
		add_filter( 'adminify_admin_localize_data', [ $this, 'add_pro_localize_settings' ], 10, 2 );

		// Body Font (admin_general_google_font) download on settings
		// save. Free GoogleFontsLocal::process_fonts() no longer
		// touches this option key.
		add_filter( 'adminify__wpadminify_saved_before', [ $this, 'download_body_google_font_on_save' ], 10, 1 );
	}

	/**
	 * Download the Body Google Font when the user saves the Adminify
	 * settings, so a local copy is available before the next request
	 * tries to enqueue it.
	 *
	 * @param mixed $data Saved settings array (or whatever the
	 *                    adminify__wpadminify_saved_before filter
	 *                    is currently passing).
	 * @return mixed The data, untouched.
	 */
	public function download_body_google_font_on_save( $data ) {
		if ( ! is_array( $data ) ) {
			return $data;
		}

		if ( empty( $data['admin_general_google_font']['font-family'] ) ) {
			return $data;
		}

		$font_family  = $data['admin_general_google_font']['font-family'];
		$font_weights = ! empty( $data['admin_general_google_font']['font-weight'] )
			? $data['admin_general_google_font']['font-weight']
			: '400';

		$local_fonts = GoogleFontsLocal::get_instance();
		if ( ! $local_fonts->is_font_local( $font_family ) ) {
			$local_fonts->download_font( $font_family, $font_weights );
		}

		return $data;
	}

	/**
	 * Add Pro-only settings flags to the wp_localize_script payload.
	 *
	 * @param array $data    Localize array (admin_ajax, settings, ...).
	 * @param array $options Saved options.
	 * @return array
	 */
	public function add_pro_localize_settings( $data, $options ) {
		if ( ! isset( $data['settings'] ) || ! is_array( $data['settings'] ) ) {
			$data['settings'] = [];
		}

		$data['settings']['menu_search'] = ! empty( $options['menu_layout_settings']['menu_search'] );

		return $data;
	}

	/**
	 * Build the local URL for the configured Body Font.
	 *
	 * Returns the on-disk URL of the locally-stored Google Font set
	 * via the Customize > Body Font option. Empty string when no
	 * font is configured.
	 *
	 * @return string
	 */
	public function jltwp_adminify_google_fonts_url() {
		$font_family = ! empty( $this->options['admin_general_google_font']['font-family'] )
			? $this->options['admin_general_google_font']['font-family']
			: '';

		if ( empty( $font_family ) ) {
			return '';
		}

		$local_fonts = GoogleFontsLocal::get_instance();

		if ( ! $local_fonts->is_font_local( $font_family ) ) {
			$font_weights = ! empty( $this->options['admin_general_google_font']['font-weight'] )
				? $this->options['admin_general_google_font']['font-weight']
				: '400';
			$local_fonts->download_font( $font_family, $font_weights );
		}

		return $local_fonts->get_local_font_url( $font_family );
	}

	/**
	 * admin_enqueue_scripts callback that enqueues the Pro body font
	 * stylesheet only when an admin has chosen a font in the
	 * Customize > Body Font option.
	 */
	public function enqueue_pro_body_google_font() {
		if ( empty( $this->options['admin_general_google_font']['font-family'] ) ) {
			return;
		}

		$url = $this->jltwp_adminify_google_fonts_url();

		if ( empty( $url ) ) {
			return;
		}

		wp_enqueue_style( 'wp-adminify-google-fonts', $url );
	}
}
