<?php
/**
 * Main plugin runtime.
 *
 * @package IMPACT_WIDGETS_ELEMENTOR
 */

namespace ImpactAddons\ImpactWidgetsForElementor;

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

final class Plugin {
	private static ?Plugin $instance = null;

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

		return self::$instance;
	}

	private function __construct() {
		add_action( 'plugins_loaded', array( $this, 'init' ) );
	}

	public function init(): void {
		if ( ! $this->is_compatible() ) {
			return;
		}

		require_once IMPACT_WIDGETS_ELEMENTOR_PATH . 'includes/core/class-widgets-manager.php';
		require_once IMPACT_WIDGETS_ELEMENTOR_PATH . 'includes/admin/class-dashboard.php';

		new Core\Widgets_Manager();
		new Admin\Dashboard();
	}

	private function is_compatible(): bool {
		global $wp_version;

		if ( version_compare( PHP_VERSION, IMPACT_WIDGETS_ELEMENTOR_MINIMUM_PHP_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_php_version' ) );
			return false;
		}

		if ( version_compare( $wp_version, IMPACT_WIDGETS_ELEMENTOR_MINIMUM_WP_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_wp_version' ) );
			return false;
		}

		if ( ! did_action( 'elementor/loaded' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_missing_elementor' ) );
			return false;
		}

		if ( ! defined( 'ELEMENTOR_VERSION' ) || version_compare( ELEMENTOR_VERSION, IMPACT_WIDGETS_ELEMENTOR_MINIMUM_ELEMENTOR_VERSION, '<' ) ) {
			add_action( 'admin_notices', array( $this, 'admin_notice_minimum_elementor_version' ) );
			return false;
		}

		return true;
	}

	public function admin_notice_missing_elementor(): void {
		echo '<div class="notice notice-warning is-dismissible"><p>' . esc_html__( 'Impact Widgets for Elementor requires Elementor to be installed and activated.', 'impact-widgets-for-elementor' ) . '</p></div>';
	}

	public function admin_notice_minimum_elementor_version(): void {
		printf(
			'<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
			esc_html( sprintf(
				/* translators: %s: minimum Elementor version. */
				__( 'Impact Widgets for Elementor requires Elementor version %s or greater.', 'impact-widgets-for-elementor' ),
				IMPACT_WIDGETS_ELEMENTOR_MINIMUM_ELEMENTOR_VERSION
			) )
		);
	}

	public function admin_notice_minimum_php_version(): void {
		printf(
			'<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
			esc_html( sprintf(
				/* translators: %s: minimum PHP version. */
				__( 'Impact Widgets for Elementor requires PHP version %s or greater.', 'impact-widgets-for-elementor' ),
				IMPACT_WIDGETS_ELEMENTOR_MINIMUM_PHP_VERSION
			) )
		);
	}

	public function admin_notice_minimum_wp_version(): void {
		printf(
			'<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
			esc_html( sprintf(
				/* translators: %s: minimum WordPress version. */
				__( 'Impact Widgets for Elementor requires WordPress version %s or greater.', 'impact-widgets-for-elementor' ),
				IMPACT_WIDGETS_ELEMENTOR_MINIMUM_WP_VERSION
			) )
		);
	}
}
