<?php

namespace FooterTimezoneDisplay;

use WP_PowerSuite\Core\AbstractModule;

class Module extends AbstractModule {

	private const MODULE_ID = 'footer-timezone-display';
	private const MODULE_VERSION = '1.1.0';

	public function getId(): string {
		return self::MODULE_ID;
	}

	public function getName(): string {
		return __( 'Footer Timezone Display', 'wp-powersuite' );
	}

	public function getVersion(): string {
		return self::MODULE_VERSION;
	}

	public function getDescription(): string {
		return __( 'Current site time and WordPress timezone in the admin footer for scheduling and team coordination.', 'wp-powersuite' );
	}

	public function getContext(): string|array {
		return [ 'admin' ];
	}

	protected function registerHooks(): void {
		if ( ! is_admin() ) {
			return;
		}

		// Run after Custom Admin Footer Text (20) so timezone appends to custom right text.
		add_filter( 'update_footer', [ $this, 'addTimezoneDisplay' ], 9999 );
		add_action( 'admin_head', [ $this, 'injectTimezoneStyles' ], 999 );
	}

	public function addTimezoneDisplay( string $text ): string {
		$timezone = wp_timezone();
		$timezone_string = $timezone->getName();
		$format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
		$formatted_time = wp_date( $format );

		$timezone_display = '<span class="wpps-timezone-display">'
			. '<span class="dashicons dashicons-clock wpps-timezone-display__icon" aria-hidden="true"></span>'
			. esc_html( $formatted_time ) . ' (' . esc_html( $timezone_string ) . ')</span>';

		if ( $text !== '' ) {
			return $text . ' <span class="wpps-timezone-separator">|</span> ' . $timezone_display;
		}

		return $timezone_display;
	}

	public function injectTimezoneStyles(): void {
		?>
		<style type="text/css" id="wpps-timezone-display-styles">
			.wpps-timezone-separator {
				color: #c3c4c7;
				margin: 0 8px;
			}
			.wpps-timezone-display {
				color: #646970;
				font-size: 11px;
			}
			.wpps-timezone-display__icon {
				font-size: 14px;
				width: 14px;
				height: 14px;
				vertical-align: middle;
				margin-right: 4px;
			}
		</style>
		<?php
	}
}
