<?php
/**
 * Optikit Main Class
 *
 * @package Optikit
 */

namespace Optikit;

/**
 * Class Optikit
 */
final class Optikit extends \Optikit\DediData\Singleton {

	/**
	 * Plugin URL
	 *
	 * @var string $plugin_url
	 */
	protected $plugin_url;

	/**
	 * Plugin Folder
	 *
	 * @var string $plugin_folder
	 */
	protected $plugin_folder;

	/**
	 * Plugin Name
	 *
	 * @var string $plugin_name
	 */
	protected $plugin_name;

	/**
	 * Plugin Version
	 *
	 * @var string $plugin_version
	 */
	protected $plugin_version;

	/**
	 * Plugin Slug
	 *
	 * @var string $plugin_slug
	 */
	protected $plugin_slug;

	/**
	 * Plugin File
	 *
	 * @var string $plugin_file
	 */
	protected $plugin_file;

	/**
	 * Constructor
	 *
	 * @param mixed $plugin_file Plugin File Name.
	 * @see https://developer.wordpress.org/reference/functions/register_activation_hook
	 * @see https://developer.wordpress.org/reference/functions/register_deactivation_hook
	 * @see https://developer.wordpress.org/reference/functions/register_uninstall_hook
	 * @SuppressWarnings(PHPMD.ElseExpression)
	 */
	protected function __construct( $plugin_file = null ) {
		$this->plugin_file = $plugin_file;
		$this->set_plugin_info();
		register_activation_hook( $plugin_file, array( $this, 'activate' ) );
		register_deactivation_hook( $plugin_file, array( $this, 'deactivate' ) );
		register_uninstall_hook( $plugin_file, self::class . '::uninstall' );
		if ( is_admin() ) {
			add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ), 11 );
			add_action( 'init', array( $this, 'admin' ) );
		} else {
			add_action( 'wp_enqueue_scripts', array( $this, 'load_frontend_scripts' ), 11 );
			add_action( 'init', array( $this, 'run' ) );
		}
		// add_action( 'widgets_init', array( $this, 'widgets_init' ) );
		// \Optikit\Shortcodes::get_instance();
	}

	/**
	 * Activate the plugin
	 *
	 * @return void
	 * @see https://developer.wordpress.org/reference/functions/add_option
	 */
	public function activate() {
		add_option( $this->plugin_slug );
	}

	/**
	 * Run when plugins deactivated
	 *
	 * @return void
	 */
	public function deactivate() {
		// Clear any temporary data stored by plugin.
		// Flush Cache/Temp.
		// Flush Permalinks.
	}

	/**
	 * The function is used to load frontend scripts and styles in a WordPress plugin, with support for
	 * RTL (right-to-left) languages.
	 *
	 * @return void
	 */
	public function load_frontend_scripts() {
		/*
		if ( is_rtl() ) {
			wp_register_style( $this->plugin_slug . '-rtl', $this->plugin_url . '/assets/public/css/style.rtl.css', array(), $this->plugin_version );
			wp_enqueue_style( $this->plugin_slug . '-rtl' );
		} else {
			wp_register_style( $this->plugin_slug, $this->plugin_url . '/assets/public/css/style.css', array(), $this->plugin_version );
			wp_enqueue_style( $this->plugin_slug );
		}

		wp_register_script( $this->plugin_slug, $this->plugin_url . '/assets/public/js/script.js', array(), $this->plugin_version, true );
		wp_enqueue_script( $this->plugin_slug );
		*/

		$option = get_option( $this->plugin_slug );
		if ( ! is_array( $option ) ) {
			return;
		}

		$global = $option['global'];
		if ( true !== $global ) {
			return;
		}

		// Font Awesome CSS
		$font_awesome = $option['font-awesome'];
		if ( true === $font_awesome || ! isset( $font_awesome ) ) {
			if ( ! is_plugin_active( 'font-awesome/index.php' ) ) {
				wp_enqueue_style( 'font-awesome', $this->plugin_url . '/assets/libraries/font-awesome/css/all.min.css', array(), '7.2.0', 'all' );
			}
		}

		$bootstrap = $option['bootstrap'];
		if ( true === $bootstrap || ! isset( $bootstrap ) ) {
			// bootstrap js css load in footer
			wp_enqueue_script(
				'bootstrap',
				$this->plugin_url . '/assets/libraries/bootstrap/js/bootstrap.bundle.min.js',
				array( 'jquery' ),
				'5.3.3',
				array(
					'strategy'  => 'defer',
					'in_footer' => true,
				)
			);
			$rtl     = is_rtl() ? '-rtl' : '';
			$rtl_ext = is_rtl() ? '.rtl' : '';
			wp_enqueue_style( 'bootstrap' . $rtl, $this->plugin_url . '/assets/libraries/bootstrap/css/bootstrap' . $rtl_ext . '.min.css', array(), '5.3.8', 'all' );
		}
	}

	/**
	 * Styles for Admin
	 *
	 * @return void
	 */
	public function load_admin_scripts() {
		if ( is_rtl() ) {
			wp_register_style( $this->plugin_slug . '-rtl', $this->plugin_url . '/assets/backend/css/style-rtl.css', array(), $this->plugin_version );
			wp_enqueue_style( $this->plugin_slug . '-rtl' );
		} else {
			wp_register_style( $this->plugin_slug, $this->plugin_url . '/assets/backend/css/style.css', array(), $this->plugin_version );
			wp_enqueue_style( $this->plugin_slug );
		}

		wp_register_script( $this->plugin_slug, $this->plugin_url . '/assets/backend/js/script.js', array(), $this->plugin_version, true );
		wp_enqueue_script( $this->plugin_slug );

		// $rtl_ext = is_rtl() ? '.rtl' : '';
		// Load editor styles for both rtl and ltr
		/*
		add_editor_style(
			array(
				'css/editor-style.css',
				$this->plugin_url . '/assets/fontawesome-6.5.1/css/all.min.css',
				$this->plugin_url . '/assets/bootstrap-5.3.8/css/bootstrap' . $rtl_ext . '.min.css',
			)
		);
		*/
	}

	/**
	 * Widgets Initializations
	 *
	 * @return void
	 */
	public function widgets_init() {
		// register_widget( 'AparatFeedWidget\Aparat_Feed_Widget' );
	}

	/**
	 * Uninstall plugin
	 *
	 * @return void
	 * @see https://developer.wordpress.org/reference/functions/delete_option
	 */
	public static function uninstall() {
		delete_option( 'optikit' );
		// Remove Tables from wpdb
		// global $wpdb;
		// $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}optikit");
		// Clear any cached data that has been removed.
		wp_cache_flush();
	}

	/**
	 * Set Plugin Info
	 *
	 * @return void
	 */
	private function set_plugin_info() {
		$this->plugin_slug = basename( $this->plugin_file, '.php' );
		$this->plugin_url  = plugins_url( '', $this->plugin_file );

		if ( ! function_exists( 'get_plugins' ) ) {
			include_once \ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$this->plugin_folder  = plugin_dir_path( $this->plugin_file );
		$plugin_info          = get_plugins( '/' . plugin_basename( $this->plugin_folder ) );
		$plugin_file_name     = basename( $this->plugin_file );
		$this->plugin_version = $plugin_info[ $plugin_file_name ]['Version'];
		$this->plugin_name    = $plugin_info[ $plugin_file_name ]['Name'];
	}

	/**
	 * The function "run" is a placeholder function in PHP with no code inside.
	 *
	 * @return void
	 */
	public function run() {
		\Optikit\MetaBoosters\Meta_Booster::get_instance();
	}

	/**
	 * The admin function includes the options.php file and registers the admin menu.
	 *
	 * @return void
	 */
	public function admin() {
		\Optikit\Admin_Menu::get_instance();
		\Optikit\Optikit_Options::get_instance( $this->plugin_slug );
	}
}
