<?php
namespace SabaiApps\Directories\Platform\WordPress;

class Loader {

	private static $_path, $_version, $_priority;

	public static function pluginsDir() {
		return isset( self::$_path ) ? dirname( self::$_path ) : WP_CONTENT_DIR . '/plugins';
	}

	public static function plugin( $file = false ) {
		return $file ? self::pluginsDir() . '/directories-pro/directories-pro.php' : 'directories-pro';
	}

	public static function vendorDir() {
		return self::pluginsDir() . '/directories-pro/vendor';
	}

	public static function register( $path, $version, $priority = 0 ) {
		if ( isset( self::$_path ) ) {
			if ( self::$_version !== $version ) {
				if ( version_compare( $version, self::$_version, '<' ) ) {
					return;
				}
			} elseif ( $priority <= self::$_priority ) {
					return;
			}
			self::$_path = $path;
		} else {
			self::$_path = $path;
			add_action(
				'plugins_loaded',
				function () {
					self::getPlatform()->run();
				}
			);
			if ( is_admin() ) {
				register_activation_hook(
					self::plugin( true ),
					function () {
						self::getPlatform()->activate();
					}
				);
				register_uninstall_hook( self::plugin( true ), array( __CLASS__, '_uninstall' ) ); // needs to be static
			}
		}
		self::$_version  = $version;
		self::$_priority = $priority;
	}

	/**
	 * @return Platform
	 */
	public static function getPlatform() {
		if ( ! class_exists( '\SabaiApps\Directories\Platform\WordPress\Platform', false ) ) {
			if ( ! isset( self::$_path ) ) {
				throw new \RuntimeException( 'Path is not set!' );
			}
			$loader = require self::$_path . '/vendor/autoload.php';
			if ( PHP_VERSION_ID >= 80000
				&& defined( 'DRTS_WORDPRESS_COMPOSER_USE_PHP8' )
				&& DRTS_WORDPRESS_COMPOSER_USE_PHP8
			) {
				foreach ( array(
					'Psr\\Log\\' => array( self::$_path . '/vendor8/psr/log/src' ),
				) as $namespace => $path ) {
					$loader->setPsr4( $namespace, $path );
				}
			}
		}
		return Platform::getInstance();
	}

	public static function _uninstall() {
		// For some reason, uninstall hook is called even when deleting Directories (v1.14),
		// so do nothing if Directories Pro is active
		if ( is_plugin_active( 'directories-pro/directories-pro.php' ) ) {
			return;
		}

		try {
			self::getPlatform()->getApplication( true, true )->Uninstall();
		} catch ( \SabaiApps\Directories\Exception\NotInstalledException $e ) {
		}
	}
}
