<?php

namespace Storeplugin\PDFInvoice;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Framework\Contracts\ServiceInterface;
use Storeplugin\PDFInvoice\Config;

/**
 * Asset class to enqueue css and js assets
 *
 * @since 1.0.0
 */
class Asset implements ServiceInterface
{
	/**
	 * Register the service by adding hooks.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function register(): void
	{
		add_action('wp_enqueue_scripts', [$this, 'enqueueViewAssets']);
	}

	/**
	 * Enqueue the public assets (styles and scripts).
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public function enqueueViewAssets(): void
	{
		wp_register_style(
			Config::asset('style'),
			trailingslashit(NXTPDF_URL) . 'assets/css/style.css',
			[],
			Config::version()
		);
	}

	/**
	 * Register app admin assets on demand.
	 *
	 * @return void
	 */
	public static function registerAppAdminAssets(): void
	{
		$adminStyle = Config::asset('admin-style');
		$commonScript = Config::asset('common-script');
		$adminScript = Config::asset('admin-script');
		$setupWizard = Config::asset('setup-wizard');
		$adminScriptVersion = self::getAssetVersion('assets/js/pdf-editor.js');

		if (!wp_style_is($adminStyle, 'registered')) {
			wp_register_style(
				$adminStyle,
				trailingslashit(NXTPDF_URL) . 'assets/css/pdf-editor.css',
				[],
				Config::version()
			);
		}

		if (!wp_script_is($commonScript, 'registered')) {
			// Common script
			wp_register_script(
				$commonScript,
				trailingslashit(NXTPDF_URL) . 'assets/js/common.js',
				[],
				Config::version(),
				true
			);
		}

		if (!wp_script_is($adminScript, 'registered')) {
			wp_register_script(
				$adminScript,
				trailingslashit(NXTPDF_URL) . 'assets/js/pdf-editor.js',
				['jquery'],
				$adminScriptVersion,
				true
			);

			wp_localize_script($adminScript, 'nxtpdf_admin', [
				'ajaxUrl' => admin_url('admin-ajax.php'),
				'nonce' => wp_create_nonce('nxtpdf_document_nonce'),
				'strings' => [
					'generating' => __('Generating preview...', 'storeinvoice-print-pdf-invoices-and-packing-slips'),
					'error' => __('Error generating preview', 'storeinvoice-print-pdf-invoices-and-packing-slips'),
					'saved' => __('Settings saved!', 'storeinvoice-print-pdf-invoices-and-packing-slips'),
				]
			]);
		}

		if (!wp_script_is($setupWizard, 'registered')) {
			wp_register_script(
				$setupWizard,
				trailingslashit(NXTPDF_URL) . 'assets/js/setup-wizard.js',
				['jquery', Config::asset('select2-script')],
				Config::version(),
				true
			);
		}
	}

	/**
	 * Resolve an asset version that changes when the file changes.
	 *
	 * @param string $relativePath Relative path from the plugin root.
	 * @return string
	 */
	private static function getAssetVersion(string $relativePath): string
	{
		$absolutePath = trailingslashit(NXTPDF_DIR) . ltrim($relativePath, '/');
		$filemtime = file_exists($absolutePath) ? filemtime($absolutePath) : false;

		return $filemtime ? (string) $filemtime : Config::version();
	}

	/**
	 * Register framework assets on demand.
	 *
	 * @return void
	 */
	public static function registerFrameworkAssets(): void
	{
		$frameworkStyle = Config::asset('framework');
		$select2Style = Config::asset('select2-style');
		$optionsScript = Config::asset('options');
		$select2Script = Config::asset('select2-script');
		$draggableScript = Config::asset('draggable');
		$iconPickerScript = Config::asset('icon-picker');
		$sortableScript = 'sortablejs';

		if (!wp_style_is($frameworkStyle, 'registered')) {
			wp_register_style(
				$frameworkStyle,
				trailingslashit(NXTPDF_URL) . 'assets/css/framework/framework.css',
				[],
				Config::version()
			);
		}

		if (!wp_style_is($select2Style, 'registered')) {
			wp_register_style(
				$select2Style,
				trailingslashit(NXTPDF_URL) . 'assets/css/framework/select2.min.css',
				[],
				Config::version()
			);
		}

		if (!wp_script_is($optionsScript, 'registered')) {
			// Register options.js (navigation functionality)
			wp_register_script(
				$optionsScript,
				trailingslashit(NXTPDF_URL) . 'assets/js/framework/options.js',
				['jquery'],
				Config::version(),
				true
			);
		}

		if (!wp_script_is($select2Script, 'registered')) {
			wp_register_script(
				$select2Script,
				trailingslashit(NXTPDF_URL) . 'assets/js/framework/select2.min.js',
				['jquery'],
				Config::version(),
				true
			);
		}

		if (!wp_script_is($sortableScript, 'registered')) {
			// Register SortableJS library
			wp_register_script(
				$sortableScript,
				trailingslashit(NXTPDF_URL) . 'assets/js/framework/sortable.min.js',
				[],
				'1.15.7',
				true
			);
		}

		if (!wp_script_is($draggableScript, 'registered')) {
			// Register draggable.js (drag and drop functionality for list tables)
			wp_register_script(
				$draggableScript,
				trailingslashit(NXTPDF_URL) . 'assets/js/framework/draggable.js',
				['jquery', $sortableScript],
				Config::version(),
				true
			);
		}

		if (!wp_script_is($iconPickerScript, 'registered')) {
			wp_register_script(
				$iconPickerScript,
				trailingslashit(NXTPDF_URL) . 'assets/js/framework/icon-picker.js',
				[],
				Config::version(),
				true
			);
		}
	}
}
