<?php
/**
 * Reusable Titanium Suite admin header.
 *
 * Copy this whole directory into a Titanium plugin and require this file.
 * The plugin name is supplied at render time; no product name is hardcoded.
 *
 * @package TitaniumSuiteHeader
 */

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

if ( ! class_exists( 'Titanium_Suite_Header' ) ) {
	final class Titanium_Suite_Header {

		/**
		 * Enqueue the shared header assets on the current plugin admin page.
		 *
		 * @param array $config Optional dark-mode configuration.
		 * @return void
		 */
		public static function enqueue_assets( $config = array() ) {
			$defaults = array(
				'storage_key'      => 'titaniumSuiteDarkMode',
				'extra_dark_class' => '',
			);
			$config   = wp_parse_args( $config, $defaults );

			$base_dir = plugin_dir_path( __FILE__ );
			$base_url = plugin_dir_url( __FILE__ );
			$css_file = $base_dir . 'assets/titanium-suite-header.css';
			$head_js  = $base_dir . 'assets/titanium-suite-header-head.js';
			$ui_js    = $base_dir . 'assets/titanium-suite-header.js';

			wp_enqueue_style(
				'titanium-suite-header',
				$base_url . 'assets/titanium-suite-header.css',
				array(),
				file_exists( $css_file ) ? (string) filemtime( $css_file ) : '1.0.0'
			);

			wp_enqueue_script(
				'titanium-suite-header-head',
				$base_url . 'assets/titanium-suite-header-head.js',
				array(),
				file_exists( $head_js ) ? (string) filemtime( $head_js ) : '1.0.0',
				false
			);

			wp_add_inline_script(
				'titanium-suite-header-head',
				'window.titaniumSuiteHeaderConfig = ' . wp_json_encode(
					array(
						'storageKey'     => sanitize_text_field( $config['storage_key'] ),
						'extraDarkClass' => sanitize_html_class( $config['extra_dark_class'] ),
					)
				) . ';',
				'before'
			);

			wp_enqueue_script(
				'titanium-suite-header',
				$base_url . 'assets/titanium-suite-header.js',
				array(),
				file_exists( $ui_js ) ? (string) filemtime( $ui_js ) : '1.0.0',
				true
			);
		}

		/**
		 * Render the Platinum header.
		 *
		 * @param array $args Header content and optional class names.
		 * @return void
		 */
		public static function render( $args = array() ) {
			$defaults = array(
				'title'            => '',
				'wordmark'         => 'Titanium',
				'edition'          => 'Free',
				'version'          => '',
				'dark_label'       => 'Dark mode',
				'wrapper_class'     => '',
				'storage_key'       => 'titaniumSuiteDarkMode',
				'extra_dark_class'  => '',
			);
			$args = wp_parse_args( $args, $defaults );

			$version_text = trim( (string) $args['edition'] );
			if ( '' !== trim( (string) $args['version'] ) ) {
				$version_text .= ' v' . trim( (string) $args['version'] );
			}

			$classes = 'titanium-suite-header';
			if ( '' !== trim( (string) $args['wrapper_class'] ) ) {
				$classes .= ' ' . implode(
					' ',
					array_map(
						'sanitize_html_class',
						preg_split( '/\s+/', trim( (string) $args['wrapper_class'] ) )
					)
				);
			}
			?>
			<div
				class="<?php echo esc_attr( $classes ); ?>"
				data-tsh-storage-key="<?php echo esc_attr( sanitize_text_field( $args['storage_key'] ) ); ?>"
				data-tsh-extra-dark-class="<?php echo esc_attr( sanitize_html_class( $args['extra_dark_class'] ) ); ?>"
			>
				<div class="titanium-suite-header__title">
					<span class="titanium-suite-header__wordmark"><?php echo esc_html( $args['wordmark'] ); ?></span>
					<?php if ( '' !== trim( (string) $args['title'] ) ) : ?>
						<strong><?php echo esc_html( $args['title'] ); ?></strong>
					<?php endif; ?>
				</div>

				<div class="titanium-suite-header__tools">
					<label class="titanium-suite-header__dark-toggle">
						<input type="checkbox" class="titanium-suite-header__dark-input">
						<span class="titanium-suite-header__dark-ui" aria-hidden="true"><i></i></span>
						<em><?php echo esc_html( $args['dark_label'] ); ?></em>
					</label>
					<div class="titanium-suite-header__version"><?php echo esc_html( $version_text ); ?></div>
				</div>
			</div>
			<?php
		}
	}
}
