<?php
defined( 'ABSPATH' ) || exit;

class BW_WebP_Plugin {

	private static ?self $instance = null;

	public BW_WebP_Settings $settings;
	public BW_WebP_Manifest $manifest;
	public BW_WebP_Queue $queue;
	public BW_WebP_Rewrite $rewrite;
	public BW_WebP_Rest $rest;
	public BW_WebP_Admin $admin;

	public static function instance(): self {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->settings = new BW_WebP_Settings();
		$this->manifest = new BW_WebP_Manifest();
		$this->rewrite  = new BW_WebP_Rewrite();
		$this->queue    = new BW_WebP_Queue( $this->manifest, $this->settings );
		$this->rest     = new BW_WebP_Rest( $this->manifest, $this->settings, $this->queue );
		$this->admin    = new BW_WebP_Admin( $this->settings, $this->manifest, $this->rewrite );
	}

	public function boot(): void {
		load_plugin_textdomain( 'bw-webp', false, dirname( plugin_basename( BW_WEBP_FILE ) ) . '/languages' );

		$this->queue->register_hooks();
		$this->rest->register_hooks();
		if ( is_admin() ) {
			$this->admin->register_hooks();
		}

		if ( defined( 'WP_CLI' ) && WP_CLI ) {
			$cli = new BW_WebP_Cli( $this->manifest, $this->settings, $this->queue );
			WP_CLI::add_command( 'bw-webp', $cli );
		}
	}
}
