<?php

/**
 * Core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the dashboard.
 *
 * @link       https://kaizencoders.com
 * @since      1.0.0
 *
 * @package    MagicLink
 */

namespace KaizenCoders\MagicLink;

/**
 * The core plugin class.
 *
 * This is used to define internationalization, dashboard-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    MagicLink
 * @subpackage MagicLink/includes
 * @author     KaizenCoders <hello@kaizencoders.com>
 */
class Plugin {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Loader $loader Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string $MagicLink The string used to uniquely identify this plugin.
	 */
	protected $plugin_name = 'magic-link';

	/**
	 * True instance of a class.
	 *
	 * @since 1.0.0
	 *
	 * @var Plugin|null $instance The instance of the class.
	 */
	public static $instance = null;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string $version The current version of the plugin.
	 */
	protected $version = '1.0.0';

	/**
	 * @var Object|DB
	 */
	public $db = null;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function __construct( $version = '' ) {
		$this->version = $version;
		$this->loader  = new Loader();
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the I18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {
		$plugin_i18n = new I18n();
		$plugin_i18n->set_domain( $this->get_plugin_name() );
		$plugin_i18n->load_plugin_textdomain();
	}

	/**
	 * Register all of the hooks related to the dashboard functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {
		$plugin_admin = new Admin( $this );

		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_admin_menu' );

		$this->loader->add_action( 'admin_print_scripts', $plugin_admin, 'remove_admin_notices', 999999999 );

		$this->loader->add_action( 'manage_users_columns', $plugin_admin, 'add_magic_link_column' );
		$this->loader->add_action( 'manage_users_custom_column', $plugin_admin, 'show_magic_link_button', 10, 3 );
		$this->loader->add_action( 'wp_ajax_generate_magic_link', $plugin_admin, 'generate_magic_link' );

		$this->loader->add_action( 'in_plugin_update_message-magic-link/magic-link.php', $plugin_admin, 'in_plugin_update_message', 10, 2 );
		$this->loader->add_filter( 'admin_footer_text', $plugin_admin, 'update_admin_footer_text', 999999999);

		$this->loader->add_filter( 'set-screen-option', $plugin_admin, 'save_screen_options', 10, 3 );
	}

	private function define_frontend_hooks() {
		$frontend = new Frontend( $this );


		if ( Helper::is_magic_link_enabled() ) {
			$this->loader->add_action( 'init', $frontend, 'handle_login_request' );
			$this->loader->add_action( 'init', $frontend, 'handle_magic_link_login' );
			$this->loader->add_action( 'login_form_login', $frontend, 'maybe_redirect' );
			$this->loader->add_action( 'login_form_magic_link', $frontend, 'handle_send_magic_link' );

			if ( Helper::is_show_on_login_form() ) {
				$this->loader->add_action( 'login_footer', $frontend, 'add_magic_link_button' );
				$this->loader->add_action( 'login_head', $frontend, 'login_css' );
			}
		}
	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * Load the dependencies, define the locale, and set the hooks for the Dashboard and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @return    string    The name of the plugin.
	 * @since     1.0.0
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @return    Loader    Orchestrates the hooks of the plugin.
	 * @since     1.0.0
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @return    string    The version number of the plugin.
	 * @since     1.0.0
	 */
	public function get_version() {
		return $this->version;
	}

	/**
	 * Get Settings.
	 *
	 * @return false|mixed|null
	 *
	 * @since 1.1.2
	 */
	public function get_settings() {
		return get_option( 'kc_ml_settings' );
	}

	public function is_pro() {
		if ( defined( 'KC_ML_DEV_MODE' ) && KC_ML_DEV_MODE ) {
			return true;
		}

		if ( magic_link_fs()->is_premium() && file_exists( KC_ML_PLUGIN_DIR . 'pro/includes/Init_PRO.php' ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Init Classes
	 *
	 * @since 1.0.0
	 */
	public function init_classes() {
		$classes = [
			'KaizenCoders\MagicLink\Install',
			'KaizenCoders\MagicLink\Uninstall',
			'KaizenCoders\MagicLink\Shortcode',
			'KaizenCoders\MagicLink\Ajax',

			// Load PRO classes if available.
			'KaizenCoders\MagicLink\PRO\Init_PRO',
		];

		foreach ( $classes as $class ) {
			$this->loader->add_class( $class );
		}
	}

	public function add_cli_commands() {
		\WP_CLI::add_command( 'magic-link', 'KaizenCoders\MagicLink\CLI\Commands\MagicLink' );
	}

	public function define_constants() {
		/* @const  KC_ML_ADMIN_TEMPLATES_DIR */
		if ( ! defined( 'KC_ML_ADMIN_TEMPLATES_DIR' ) ) {
			define( 'KC_ML_ADMIN_TEMPLATES_DIR', KC_ML_PLUGIN_DIR . 'lite/includes/Admin/Templates' );
		}

		if ( ! defined( 'MAGIC_LINK_USERNAME_ONLY' ) ) {
			define( 'MAGIC_LINK_USERNAME_ONLY', false );
		}

	}

	public static function instance() {
		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Plugin ) ) {
			self::$instance = new Plugin( KC_ML_PLUGIN_VERSION );

			self::$instance->set_locale();
			self::$instance->define_frontend_hooks();
			self::$instance->define_admin_hooks();
			self::$instance->init_classes();
			self::$instance->define_constants();

			if ( defined( 'WP_CLI' ) && WP_CLI ) {
				self::$instance->add_cli_commands();
			}
		}

		return self::$instance;
	}
}
