<?php
/**
 * Wider Admin Menu Module
 *
 * @package WiderAdminMenu
 * @since 1.0.0
 */

namespace WiderAdminMenu;

use WP_PowerSuite\Core\AbstractModule;
use WP_PowerSuite\Core\AdminAssetContext;
use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

class Module extends AbstractModule {

	private const MODULE_ID = 'wider-admin-menu';
	private const MODULE_VERSION = '1.3.0';

	private const MIN_WIDTH_PX = 160;
	private const MAX_WIDTH_PX = 300;
	private const DEFAULT_WIDTH_PX = 200;

	private ?array $settings_cache = null;

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

	public function getName(): string {
		return __( 'Wider Admin Menu', 'wp-powersuite' );
	}

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

	public function getDescription(): string {
		return __( 'More horizontal room for the sidebar menu—less cramped labels for busy sites.', 'wp-powersuite' );
	}

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

	public static function sanitizeMenuWidth( mixed $value ): int {
		$width = absint( $value );

		if ( $width < self::MIN_WIDTH_PX ) {
			return self::MIN_WIDTH_PX;
		}

		if ( $width > self::MAX_WIDTH_PX ) {
			return self::MAX_WIDTH_PX;
		}

		return $width > 0 ? $width : self::DEFAULT_WIDTH_PX;
	}

	/**
	 * Resolved menu width when this module is active.
	 *
	 * @param array<string, mixed>|null $settings Optional pre-loaded settings.
	 */
	public static function resolveMenuWidthPx( ?array $settings = null ): int {
		if ( $settings === null ) {
			$settings = OptionStoreModuleSettingsRepository::forModule( self::MODULE_ID )->all( [] );
		}

		return self::sanitizeMenuWidth( $settings['menu_width'] ?? self::DEFAULT_WIDTH_PX );
	}

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

		add_action( 'admin_head', [ $this, 'addStyles' ], 99 );
		add_filter( 'wpps_admin_menu_width', [ $this, 'filterAdminMenuWidth' ] );
		add_filter( 'wpps_content_folders_admin_menu_width', [ $this, 'filterAdminMenuWidth' ] );
	}

	public function filterAdminMenuWidth( int $width ): int {
		return self::resolveMenuWidthPx( $this->getSettings() );
	}

	public function addStyles(): void {
		$hook = function_exists( 'get_current_screen' ) && get_current_screen()
			? (string) get_current_screen()->id
			: '';

		if ( ! AdminAssetContext::shouldLoadAssets( $hook ) ) {
			return;
		}

		$menu_width = self::resolveMenuWidthPx( $this->getSettings() );
		$width_css  = $menu_width . 'px';

		if ( ! is_rtl() ) {
			$margin_property = 'margin-left';
			$position        = 'left';
		} else {
			$margin_property = 'margin-right';
			$position        = 'right';
		}

		$gf_editor_width           = (string) ( 594 - 160 + $menu_width ) . 'px';
		$fluent_cart_inner_wrapper = (string) ( $menu_width - 30 ) . 'px';

		?>
		<style id="wpps-wider-admin-menu">
			@media screen and (min-width: 783px) {
				#wpcontent,
				#wpfooter {
					<?php echo esc_html( $margin_property ); ?>: <?php echo esc_attr( $width_css ); ?>;
				}

				#adminmenuback,
				#adminmenuwrap,
				#adminmenu,
				#adminmenu .wp-submenu {
					width: <?php echo esc_attr( $width_css ); ?>;
				}

				#adminmenu .wp-submenu {
					<?php echo esc_html( $position ); ?>: <?php echo esc_attr( $width_css ); ?>;
				}

				#adminmenu .wp-not-current-submenu .wp-submenu,
				.folded #adminmenu .wp-has-current-submenu .wp-submenu {
					min-width: <?php echo esc_attr( $width_css ); ?>;
				}

				#qm {
					<?php echo esc_html( $margin_property ); ?>: <?php echo esc_attr( $width_css ); ?>;
				}

				@media (min-width: 960px) {
					.woocommerce-layout__header,
					#e-admin-top-bar-root {
						width: calc(100% - <?php echo esc_attr( $width_css ); ?>);
					}
				}

				.auto-fold .interface-interface-skeleton {
					<?php echo esc_html( $position ); ?>: <?php echo esc_attr( $width_css ); ?>;
				}

				body.toplevel_page_gf_edit_forms .gform-form-toolbar {
					width: calc(100% - <?php echo esc_attr( $width_css ); ?>) !important;
				}

				.form_editor_fields_container {
					max-width: calc(100% - <?php echo esc_attr( $gf_editor_width ); ?>) !important;
				}

				.auto-fold.frm-admin-page-styles:not(.frm-full-screen) .frm_page_container,
				.auto-fold:not(.frm-full-screen) .frm_wrap .frm_page_container,
				.frm-unfold.frm-admin-page-styles:not(.frm-full-screen) .frm_page_container,
				.frm-unfold:not(.frm-full-screen) .frm_wrap .frm_page_container {
					<?php echo esc_html( $position ); ?>: <?php echo esc_attr( $width_css ); ?> !important;
				}

				#wpbody-content #fct_admin_menu_holder .fct_admin_menu_wrap {
					width: calc(100% - <?php echo esc_attr( $width_css ); ?>) !important;
				}

				.fct-reports-view-inner {
					padding-<?php echo esc_html( $position === 'left' ? 'left' : 'right' ); ?>: <?php echo esc_attr( $fluent_cart_inner_wrapper ); ?> !important;
				}

				.fct-setting-container .fct-tab-wrapper {
					padding-<?php echo esc_html( $position === 'left' ? 'left' : 'right' ); ?>: <?php echo esc_attr( $width_css ); ?> !important;
				}
			}

			@media screen and (max-width: 960px) and (min-width: 783px) {
				#wpcontent,
				#wpfooter {
					<?php echo esc_html( $margin_property ); ?>: 160px;
				}

				#adminmenuback,
				#adminmenuwrap,
				#adminmenu,
				#adminmenu .wp-submenu {
					width: 160px;
				}

				#adminmenu .wp-submenu {
					<?php echo esc_html( $position ); ?>: 160px;
				}

				#adminmenu .wp-not-current-submenu .wp-submenu,
				.folded #adminmenu .wp-has-current-submenu .wp-submenu {
					min-width: 160px;
				}

				#qm {
					<?php echo esc_html( $margin_property ); ?>: 160px;
				}

				.auto-fold .interface-interface-skeleton {
					<?php echo esc_html( $position ); ?>: 160px;
				}
			}
		</style>
		<?php
	}

	private function getSettings(): array {
		if ( $this->settings_cache !== null ) {
			return $this->settings_cache;
		}

		$settings = $this->moduleSettings()->all( [] );
		$defaults = [
			'menu_width' => self::DEFAULT_WIDTH_PX,
		];

		$this->settings_cache = array_merge( $defaults, $settings );

		return $this->settings_cache;
	}
}
