<?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       http://example.com
 * @since      1.0.0
 *
 * @package    Ultimate_3D_Model_Viewer
 * @subpackage Ultimate_3D_Model_Viewer/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    Ultimate_3D_Model_Viewer
 * @subpackage Ultimate_3D_Model_Viewer/includes
 * @author     Alexander Dort GmbH <robin@alexanderdort.com>
 */
class Ultimate_3D_Model_Viewer {

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

	/**
	 * 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( 'ULTIMATE_3D_MODEL_VIEWER_VERSION' ) ) {
			$this->version = ULTIMATE_3D_MODEL_VIEWER_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->ultimate_3d_model_viewer = 'ultimate-3d-model-viewer';

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

		if(function_exists('u3mv_fs')) {
			$this->define_uninstall_hook();
		}

	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Ultimate_3D_Model_Viewer_Loader. Orchestrates the hooks of the plugin.
	 * - Ultimate_3D_Model_Viewer_Admin. Defines all hooks for the admin area.
	 * - Ultimate_3D_Model_Viewer. 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 orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/ultimate-3d-model-viewer-loader.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/ultimate-3d-model-viewer-admin.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/ultimate-3d-model-viewer-public.php';

		/**
		 * The class responsible for uninstalling the plugin
		 */
		require_once plugin_dir_path( dirname(__FILE__)) . 'includes/ultimate-3d-model-viewer-uninstaller.php';


		$this->loader = new Ultimate_3D_Model_Viewer_Loader();

	}

	/**
	 * 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 Ultimate_3D_Model_Viewer_Admin( $this->get_ultimate_3d_model_viewer(), $this->get_version() );

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

		// pass freemius data and functions to js files
		$this->loader->add_action( 'enqueue_block_editor_assets', $plugin_admin, 'u3mv_fs_pass_freemius_data_to_js');

		// add new metabox to WooCommerce product editor
		$this->loader->add_action( 'add_meta_boxes', $plugin_admin, 'add_woo_commerce_model_viewer_product_metabox');
		$this->loader->add_action( 'save_post_product', $plugin_admin, 'save_woo_product_metabox_data');

		// allow special file extensions
		$this->loader->add_filter( 'upload_mimes', $plugin_admin, 'allow_custom_mimes');
		$this->loader->add_filter( 'wp_check_filetype_and_ext', $plugin_admin, 'allow_custom_filetypes', 10, 4);

		// add custom gutenberg category
		$this->loader->add_filter( 'block_categories_all', $plugin_admin, 'add_custom_gutenberg_category', 10,2);

	}

	/**
	 * 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 Ultimate_3D_Model_Viewer_Public( $this->get_ultimate_3d_model_viewer(), $this->get_version() );

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

		$this->loader->add_action( 'init', $plugin_public, 'register_model_viewer_gutenberg_block');

		$this->loader->add_action( 'elementor/elements/categories_registered', $plugin_public, 'add_elementor_widget_categories');
		$this->loader->add_action( 'elementor/widgets/register', $plugin_public, 'register_model_viewer_elementor_widget');
		
		$this->loader->add_action( 'woocommerce_before_single_product', $this, 'position_woo_commerce_model_viewer');
	}


	public function position_woo_commerce_model_viewer() {
		global $post;

		if (!$post || 'product' !== $post->post_type) {
		return;
		}

		$model_position = get_post_meta($post->ID, '_u3dmv_model_position', true);

		switch ($model_position) {
			case "above":
            	add_action('woocommerce_before_single_product_summary', ['Ultimate_3D_Model_Viewer_Public', 'display_3D_woo_product']);
			break;

			case "under":
				add_action( 'woocommerce_product_thumbnails', ['Ultimate_3D_Model_Viewer_Public', 'display_3D_woo_product']);
			break;

			case "right":
			case "left":
				add_action( 'woocommerce_before_single_product_summary', ['Ultimate_3D_Model_Viewer_Public', 'display_3D_woo_product']);
			break;

			case 'replace':
				remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20);
				add_action('woocommerce_before_single_product_summary', ['Ultimate_3D_Model_Viewer_Public', 'display_3D_woo_product'], 20);
			break;

			default:
            	add_action('woocommerce_before_single_product_summary', ['Ultimate_3D_Model_Viewer_Public', 'display_3D_woo_product']);
			break;
		}
	}

	private function define_uninstall_hook() {
		$plugin_uninstall = new Ultimate_3D_Model_Viewer_Uninstaller($this->get_ultimate_3d_model_viewer(), $this->get_version());

		u3mv_fs()->add_action('after_uninstall', [$plugin_uninstall, 'someInstanceMethod']);
	}

	/**
	 * 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_ultimate_3d_model_viewer() {
		return $this->ultimate_3d_model_viewer;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Ultimate_3D_Model_Viewer_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;
	}

}
