<?php

namespace DocketConnector\Core;

use DocketConnector\Admin as Admin;
use DocketConnector\Common as Common;
use DocketConnector\GravityForms\Setup;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Abort if this file is called directly.
}

if ( ! class_exists( Init::class ) ) {
	/**
	 * The core plugin class.
	 * Defines internationalization, admin-specific hooks, and public-facing site hooks.
	 */
	class Init {

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

		/**
		 * Initialize and define the core functionality of the plugin.
		 */
		public function __construct() {
			$this->load_dependencies();
			$this->define_common_hooks();
			$this->define_admin_hooks();
		}

		/**
		 * Loads the following required dependencies for this plugin.
		 *
		 * - Loader - Orchestrates the hooks of the plugin.
		 * - Admin - Defines all hooks for the admin area.
		 */
		private function load_dependencies(): void {
			$this->loader = new Loader();
		}

		/**
		 * Register all of the hooks related to both the admin area and the public-facing functionality of the plugin.
		 */
		private function define_common_hooks(): void {
			// $common = new Common\Common();
			// Example: $this->loader->add_filter( 'gform_currencies', $common, 'gf_currency_usd_whole_dollars', 50 );

			// Not only in Admin because of needing to make sure the Feed Add-On is loaded during form submissions.
			$this->define_gravity_forms_hooks();
		}

		/**
		 * Register all of the hooks related to the admin area functionality of the plugin.
		 * Also works during Ajax.
		 */
		private function define_admin_hooks(): void {
			$http = new Common\Utilities\Http();

			if ( ! $http->current_request_is( 'admin' ) ) {
				return;
			}

			$assets = new Admin\Assets();

			// Enqueue plugin's admin assets
			//$this->loader->add_action( 'admin_enqueue_scripts', $assets, 'enqueue_styles' );
			//$this->loader->add_action( 'admin_enqueue_scripts', $assets, 'enqueue_scripts' );

			$settings = new Admin\Settings\Main();

			// Plugin action links. Works but Gravity Forms Add-On framework does this for us already.
			// If we were to have more than 1 Add-On, there would probably be more than 1 Settings link.
			//$this->loader->add_filter(
			//	'plugin_action_links_' . PluginData::plugin_basename(),
			//	$settings,
			//	'customize_action_links'
			//);
		}

		/**
		 * Register all the hooks related to Gravity Forms (only runs in Admin context).
		 */
		private function define_gravity_forms_hooks(): void {
			$gf_setup = new Setup();

			if ( ! $gf_setup->is_gravity_forms_ready() ) {
				return;
			}

			$this->loader->add_action( 'gform_loaded', $gf_setup, 'load_addons', 5 );
			$this->loader->add_filter( 'gform_entry_detail_meta_boxes', $gf_setup, 'single_entry_add_docket_meta_box', 10, 3 );
			$this->loader->add_action( 'gform_post_payment_completed', $gf_setup, 'try_invoice_upon_payment_completed', 10, 2 );
		}

		/**
		 * Run the loader to execute all of the hooks with WordPress.
		 */
		public function run(): void {
			$this->loader->run();
		}

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