<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://flothemes.com/
 * @since      1.0.0
 *
 * @package    Flo_Flex_Builder
 * @subpackage Flo_Flex_Builder/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-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    Flo_Flex_Builder
 * @subpackage Flo_Flex_Builder/includes
 * @author     Flothemes <alexb@flothemes.com>
 */
class Flo_Flex_Builder {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Flo_Flex_Builder_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    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

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

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		if (defined('Flo_Flex_Builder_Version')) {
			$this->version = Flo_Flex_Builder_Version;
		} else {
			$this->version = '0.1';
		}
		$this->plugin_name = 'flo-flex-builder';

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Flo_Flex_Builder_Loader. Orchestrates the hooks of the plugin.
	 * - Flo_Flex_Builder_i18n. Defines internationalization functionality.
	 * - Flo_Flex_Builder_Admin. Defines all hooks for the admin area.
	 * - Flo_Flex_Builder_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {
		/**
		 * The class responsible for reusable global variables
		 * core plugin.
		 */
		require_once dirname(dirname(__FILE__)) . '/includes/class-flo-flex-builder-get-global-data.php';
		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-i18n.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-stylekits.php';

		/**
		 * The class responsible for typography functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/typography/class-flo-flex-builder-typography.php';

		/**
		 * The class responsible for typography check post functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/typography/class-flo-flex-builder-typography-post-check.php';

		/**
		 * The class responsible for generating data for the builder (fonts, colors, blocks)
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-data.php';

		/**
		 * Render block and element class
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-render-elements.php';
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-render-groups.php';
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-render-block.php';

		/**
		 * Function Render Gutenderg Blocks
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/render-flo-gutenberg-blocks.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-flo-flex-builder-admin.php';

		/**
		 * The class responsible for handling all update use-cases and ensuring that default data will be installed
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-flo-flex-builder-update.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-flo-flex-builder-public.php';

		$this->loader = new Flo_Flex_Builder_Loader();

		if ( function_exists('register_block_type') && FLEX_GUTENBERG ) {
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-flo-flex-gutenberg.php';
			require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-flo-flex-slideshow.php';
		}
	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Flo_Flex_Builder_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 Flo_Flex_Builder_i18n();

		$this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {

		$plugin_admin = new Flo_Flex_Builder_Admin($this->get_plugin_name(), $this->get_version());
		$initStylekits = new Flo_Flex_Builder_Stylekits();
		$updateStylekits = new Flo_Flex_Builder_Update();

		if ( function_exists('register_block_type') && FLEX_GUTENBERG ) {
			$flex_gutenberg = new Flo_Flex_Builder_Gutenberg();
			$flex_slideshow = new Flo_Flex_Builder_Slideshow();
		}

		$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_enqueue_scripts', $initStylekits, 'enqueue_scripts');
		// $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'send_sub_data', 11);

		// TOOD: CHECK RE-CHECK IF ALL IS GOOD, THIS IS BIG CHANGES
		$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'initAdminData', 11);

		// // load/save the json files
		// // MAKE SURE THIS IS COMMENTED
		// // UNCOMMENT ONLY WHEN YOU NEED TO CHANGE ACF FIELDS
		// $this->loader->add_action( 'acf/settings/save_json', $plugin_admin, 'set_json_save_path', 11 );

		$this->loader->add_action('acf/settings/load_json', $plugin_admin, 'add_json_load_path', 11);

		// reset order data transients
		$this->loader->add_action('pre_update_option_flotheme_theme_key', $plugin_admin, 'reset_order_data_on_order_update', 10, 2);
		$this->loader->add_action('core_version_check_locale', $plugin_admin, 'reset_order_data', 100);

		// append flex block to page builder
		$isFloMainTheme = $this::isFlothemes();
		$layoutName = $isFloMainTheme ? 'blocks' : 'layout';
		$this->loader->add_action('acf/load_field/name=' . $layoutName, $plugin_admin, 'append_blocks');

		$this->loader->add_action('admin_init', $plugin_admin, 'enable_disable_local_acf_json');
		$this->loader->add_action('admin_init', $plugin_admin, 'flo_include_updater');
		$this->loader->add_action('admin_init', $plugin_admin, 'flex_set_theme_data');

		$this->loader->add_action('after_switch_theme', $plugin_admin, 'flex_reset_theme_data');

		$this->loader->add_action('wp_ajax_flex_upload_screen', $plugin_admin, 'flex_upload_screen');
		$this->loader->add_action('wp_ajax_flex_delete_screen', $plugin_admin, 'flex_delete_screen');

		$this->loader->add_action('wp_ajax_refetch_order_data', $plugin_admin, 'refetch_order_data');

		//enable metaboxes
		$this->loader->add_action('admin_init', $plugin_admin, 'enable_metaboxes');

		//remove filter get_font_styles_data after theme setup
		$this->loader->add_action('after_setup_theme', $initStylekits, 'remove_filter_get_font_styles_data');
		//add FLEX_STYLEKITS constant
		$this->loader->add_action('setup_theme', $initStylekits, 'add_flex_stylekits_constant', 9);

		//set acf stylekits globals
		$this->loader->add_action('acf/enqueue_scripts', $initStylekits, 'set_acf_globals');

		//remove flex block page if is not flex theme
		$this->loader->add_action('admin_menu', $initStylekits, 'remove_flex_admin_non_flex');

		//after switch of theme check handle update
		$this->loader->add_action('after_switch_theme', $updateStylekits, 'handle_switch');

		// for yoast compatibility
		$this->loader->add_action('wp_ajax_flex_fetch_data_for_yoast', $plugin_admin, 'flex_fetch_data_for_yoast');

		// Element renderers
		$this->loader->add_action('wp_ajax_render_server_element', $plugin_admin, 'render_server_element');

		// Get menu list with IDs
		$this->loader->add_action('wp_ajax_get_menu_list', $plugin_admin, 'get_menu_list');

		// Save Menu
		$this->loader->add_action('wp_ajax_save_menu', $plugin_admin, 'save_menu');

		// Delete Menu
		$this->loader->add_action('wp_ajax_delete_menu', $plugin_admin, 'delete_menu');

		// Get menu's data
		$this->loader->add_action('wp_ajax_get_menu_data', $plugin_admin, 'get_menu_data');

		// Get menu list with IDs
		$this->loader->add_action('wp_ajax_save_global_elements', $plugin_admin, 'save_global_elements');

		// My Blocks - Save, Get List, Delete Block, Rename Block
		$this->loader->add_action('wp_ajax_my_blocks_save_block', $plugin_admin, 'my_blocks_save_block');
		$this->loader->add_action('wp_ajax_my_blocks_get_list', $plugin_admin, 'my_blocks_get_list');
		$this->loader->add_action('wp_ajax_my_blocks_delete_block', $plugin_admin, 'my_blocks_delete_block');
		$this->loader->add_action('wp_ajax_my_blocks_rename_block', $plugin_admin, 'my_blocks_rename_block');

		// Save option handler
		$this->loader->add_action('wp_ajax_save_flex_option', $plugin_admin, 'save_flex_option');

		// Random Request
		$this->loader->add_action('wp_ajax_get_misc_request', $plugin_admin, 'get_misc_request');
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_public_hooks() {

		$plugin_public = new Flo_Flex_Builder_Public($this->get_plugin_name(), $this->get_version());

		$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
		$typography_check = new Flo_Flex_Builder_Typography_Post_Check();
	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @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.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

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

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

	public static function isFlothemes() {
		$themeName = wp_get_theme()->get('Name');
		$isFlothemes = $themeName === 'Flothemes' || $themeName === 'Noticeable';
		return $isFlothemes;
	}
}
