<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       http://example.com
 * @since      1.0.0
 *
 * @package    Ultimate_3D_Model_Viewer
 * @subpackage Ultimate_3D_Model_Viewer/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Ultimate_3D_Model_Viewer
 * @subpackage Ultimate_3D_Model_Viewer/admin
 * @author     Alexander Dort GmbH <robin@alexanderdort.com>
 */
class Ultimate_3D_Model_Viewer_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $ultimate_3d_model_viewer   The ID of this plugin.
	 */
	private $ultimate_3d_model_viewer;

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

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $ultimate_3d_model_viewer   The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $ultimate_3d_model_viewer, $version ) {

		$this->ultimate_3d_model_viewer = $ultimate_3d_model_viewer;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Ultimate_3D_Model_Viewer_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Ultimate_3D_Model_Viewer_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->ultimate_3d_model_viewer, plugin_dir_url( __FILE__ ) . 'css/ultimate-3d-model-viewer-admin.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Plugin_Name_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Plugin_Name_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script( $this->ultimate_3d_model_viewer, plugin_dir_url( __FILE__ ) . 'js/ultimate-3d-model-viewer-admin.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_media();
	}

	public function allow_custom_mimes($existing_mimes) {
		$existing_mimes['glb'] = 'model/gltf-binary';
		$existing_mimes['gltf'] = 'model/gltf+json';
		$existing_mimes['usdz'] = 'model/vnd.usdz+zip';
		$existing_mimes['fbx'] = 'application/octet-stream';
		$existing_mimes['obj'] = 'text/plain';
		$existing_mimes['hdr'] = 'image/vnd.radiance';
		$existing_mimes['stl'] = 'model/stl';

		return $existing_mimes;
	}

	public function allow_custom_filetypes($data, $file, $filename, $mimes) {
		$ext = pathinfo($filename, PATHINFO_EXTENSION);
		if (in_array($ext, ['glb', 'gltf', 'usdz', 'fbx', 'obj', 'hdr', 'stl'])) {
			$data['ext'] = $ext;
			$data['type'] = $mimes[$ext] ?? 'application/octet-stream';
			$data['proper_filename'] = $filename;
		}
		return $data;
	}
	

	public function add_custom_gutenberg_category($categories, $post) {
		return array_merge(
			$categories,
			[
				[
					'slug'  => '3d',
					'title' => __('3D', 'ultimate-3d-model-viewer'),
					'icon'  => null, // Optional, could be a dashicon slug or SVG
				],
			]
    	);
	}

	public function u3mv_fs_pass_freemius_data_to_js() {
		wp_localize_script(
			'ultimate-3d-model-viewer-block-editor-script',
			'u3mv_fs',
			[
				'is_premium' => u3mv_fs()->can_use_premium_code__premium_only() ? 'true' : 'false',
				'upgrade_url' => u3mv_fs()->checkout_url(),
			]
		);
	}

	public function add_woo_commerce_model_viewer_product_metabox() {
		add_meta_box(
			'u3dmv_model_viewer_settings', // Metabox ID
			esc_html(__('Ultimate 3D Model Viewer Settings', 'ultimate-3d-model-viewer')), // Title
			[ $this, 'u3dmv_model_viewer_metabox_callback' ], // Callback
			'product', // Post type: WooCommerce products
			'normal', // Position: 'normal', 'side', or 'advanced'
			'default' // Priority
		);
	}

	public function u3dmv_model_viewer_metabox_callback($post) {
		wp_nonce_field('u3dmv_save_metabox', 'u3dmv_metabox_nonce');

		require_once plugin_dir_path( dirname(__FILE__)) . 'admin/partials/ultimate-3d-model-viewer-woo-product-panel.php';
	}


	public function save_woo_product_metabox_data($post_id) {
		$nonce = isset($_POST['u3dmv_metabox_nonce']) ? sanitize_text_field(wp_unslash($_POST['u3dmv_metabox_nonce'])) : '';

		if (empty($nonce) || !wp_verify_nonce($nonce, 'u3dmv_save_metabox')) {
			return;
		}

		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
			return;
		}

		if (isset($_POST['u3dmv_model_src'])) {
			update_post_meta($post_id, '_u3dmv_model_src', sanitize_text_field(wp_unslash($_POST['u3dmv_model_src'])));
		}

		if (isset($_POST['u3dmv_model_height'])) {
			update_post_meta($post_id, '_u3dmv_model_height', sanitize_text_field(wp_unslash($_POST['u3dmv_model_height'])));
		}

		if (isset($_POST['u3dmv_model_display'])) {
			update_post_meta($post_id, '_u3dmv_model_display', '1');		
		} else {
			update_post_meta($post_id, '_u3dmv_model_display', '0');		
		}

		if (isset($_POST['u3dmv_model_position'])) {
			update_post_meta($post_id, '_u3dmv_model_position', sanitize_text_field(wp_unslash($_POST['u3dmv_model_position'])));
		}

		if (isset($_POST['u3dmv_model_dimensions'])) {
			update_post_meta($post_id, '_u3dmv_model_dimensions', '1');
		} else {
			update_post_meta($post_id, '_u3dmv_model_dimensions', '0');
		}

		if (isset($_POST['u3dmv_model_dimensions_scale_factor'])) {
			update_post_meta($post_id, '_u3dmv_model_dimensions_scale_factor', sanitize_text_field(wp_unslash($_POST['u3dmv_model_dimensions_scale_factor'])));
		}

		if (isset($_POST['u3dmv_model_dimensions_custom'])) {
			update_post_meta($post_id, '_u3dmv_model_dimensions_custom', '1');
		} else {
			update_post_meta($post_id, '_u3dmv_model_dimensions_custom', '0');
		}

		if (isset($_POST['u3dmv_model_dimensions_x'])) {
			update_post_meta($post_id, '_u3dmv_model_dimensions_x', sanitize_text_field(wp_unslash($_POST['u3dmv_model_dimensions_x'])));
		}

		if (isset($_POST['u3dmv_model_dimensions_y'])) {
			update_post_meta($post_id, '_u3dmv_model_dimensions_y', sanitize_text_field(wp_unslash($_POST['u3dmv_model_dimensions_y'])) );
		}

		if (isset($_POST['u3dmv_model_dimensions_z'])) {
			update_post_meta($post_id, '_u3dmv_model_dimensions_z', sanitize_text_field(wp_unslash($_POST['u3dmv_model_dimensions_z'])) );
		}

		if (isset($_POST['u3dmv_model_poster'])) {
			update_post_meta($post_id, '_u3dmv_model_poster', sanitize_text_field(wp_unslash($_POST['u3dmv_model_poster'])) );
		}

		if (isset($_POST['u3dmv_model_env'])) {
			update_post_meta($post_id, '_u3dmv_model_env', sanitize_text_field(wp_unslash($_POST['u3dmv_model_env'])) );
		}

		if (isset($_POST['u3dmv_model_skybox'])) {
			update_post_meta($post_id, '_u3dmv_model_skybox', sanitize_text_field(wp_unslash($_POST['u3dmv_model_skybox'])) );
		}

		if (isset($_POST['u3dmv_model_tone_mapping'])) {
			update_post_meta($post_id, '_u3dmv_model_tone_mapping', sanitize_text_field(wp_unslash($_POST['u3dmv_model_tone_mapping'])) );
		}

		if (isset($_POST['u3dmv_model_exposure'])) {
			update_post_meta($post_id, '_u3dmv_model_exposure', sanitize_text_field(wp_unslash($_POST['u3dmv_model_exposure'])) );
		}

		if (isset($_POST['u3dmv_model_shadow_int'])) {
			update_post_meta($post_id, '_u3dmv_model_shadow_int', sanitize_text_field(wp_unslash($_POST['u3dmv_model_shadow_int'])) );
		}

		if (isset($_POST['u3dmv_model_shadow_soft'])) {
			update_post_meta($post_id, '_u3dmv_model_shadow_soft', sanitize_text_field(wp_unslash($_POST['u3dmv_model_shadow_soft'])) );
		}

		if (isset($_POST['u3dmv_model_camera_orbit'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_orbit', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_orbit'])) );
		}

		if (isset($_POST['u3dmv_model_camera_orbit_max'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_orbit_max', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_orbit_max'])) );
		}

		if (isset($_POST['u3dmv_model_camera_orbit_min'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_orbit_min', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_orbit_min'])) );
		}

		if (isset($_POST['u3dmv_model_camera_target'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_target', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_target'])) );
		}

		if (isset($_POST['u3dmv_model_camera_fov'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_fov', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_fov'])) );
		}

		if (isset($_POST['u3dmv_model_camera_fov_max'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_fov_max', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_fov_max'])) );
		}

		if (isset($_POST['u3dmv_model_camera_fov_min'])) {
			update_post_meta($post_id, '_u3dmv_model_camera_fov_min', sanitize_text_field(wp_unslash($_POST['u3dmv_model_camera_fov_min'])) );
		}

		if (isset($_POST['u3dmv_model_loading'])) {
			update_post_meta($post_id, '_u3dmv_model_loading', sanitize_text_field(wp_unslash($_POST['u3dmv_model_loading'])) );
		}

		if (isset($_POST['u3dmv_model_rotation'])) {
			update_post_meta($post_id, '_u3dmv_model_rotation', '1');
		} else {
			update_post_meta($post_id, '_u3dmv_model_rotation', '0');
		}

		if (isset($_POST['u3dmv_model_ar'])) {
			update_post_meta($post_id, '_u3dmv_model_ar', '1');		
		} else {
			update_post_meta($post_id, '_u3dmv_model_ar', '0');		
		}

		if (isset($_POST['u3dmv_model_ios_src'])) {
			update_post_meta($post_id, '_u3dmv_model_ios_src', sanitize_text_field(wp_unslash($_POST['u3dmv_model_ios_src'])) );
		}

	}
}