<?php
/**
 * Handles the custom meta box on the WooCommerce product edit screen.
 *
 * @package    Ai_Try_N_Buy
 * @subpackage Ai_Try_N_Buy/admin
 * @author     Edu Brazeal <contact@edubrazeal.com>
 */
class Aitnb_Product_Metabox {

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

	/**
	 * 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    $plugin_name       The name of this plugin.
	 * @param    string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	/**
	 * Adds the meta box to the product edit screen.
	 *
	 * @since    1.0.0
	 */
	public function add_metabox() {
		add_meta_box(
			'aitnb_product_settings_metabox',
			__( 'AI Try n Buy Settings', 'virtual-ai-fit-and-style-assistant' ),
			array( $this, 'render_metabox_content' ),
			'product',
			'advanced',
			'default'
		);
	}

	/**
	 * Renders the content of the meta box.
	 *
	 * @since    1.0.0
	 * @param    WP_Post    $post    The post object.
	 */
	public function render_metabox_content( $post ) {
		wp_nonce_field( 'aitnb_save_product_meta_data', 'aitnb_metabox_nonce' );

		$enable_tryon = get_post_meta( $post->ID, '_aitnb_enable_tryon', true );
		$item_type    = get_post_meta( $post->ID, '_aitnb_item_type', true );
		$asset_id     = get_post_meta( $post->ID, '_aitnb_overlay_asset_id', true );

		require_once plugin_dir_path( __FILE__ ) . 'views/product-metabox-view.php';
	}


	/**
	 * Saves the meta box data when the product is saved.
	 *
	 * @since    1.0.0
	 * @param    int    $post_id    The ID of the post being saved.
	 */
	public function save_metabox_data( $post_id ) {
		if ( ! isset( $_POST['aitnb_metabox_nonce'] ) ) {
			return;
		}

		if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aitnb_metabox_nonce'] ) ), 'aitnb_save_product_meta_data' ) ) {
			return;
		}

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

		if ( ! current_user_can( 'edit_product', $post_id ) ) {
			return;
		}

		$enable_tryon = isset( $_POST['aitnb_enable_tryon'] ) ? 'yes' : 'no';
		update_post_meta( $post_id, '_aitnb_enable_tryon', $enable_tryon );

		if ( isset( $_POST['aitnb_ar_mode'] ) ) {
			$ar_mode = sanitize_text_field( wp_unslash( $_POST['aitnb_ar_mode'] ) );
			update_post_meta( $post_id, '_aitnb_ar_mode', $ar_mode );
		}

		if ( isset( $_POST['aitnb_item_type'] ) ) {
			$item_type = sanitize_text_field( wp_unslash( $_POST['aitnb_item_type'] ) );
			update_post_meta( $post_id, '_aitnb_item_type', $item_type );
		}

		if ( isset( $_POST['aitnb_overlay_asset_id'] ) ) {
			$asset_id = absint( $_POST['aitnb_overlay_asset_id'] );
			update_post_meta( $post_id, '_aitnb_overlay_asset_id', $asset_id );
		}

		if ( isset( $_POST['aitnb_asset_position'] ) && is_array( $_POST['aitnb_asset_position'] ) ) {
			$position = array(
				'x' => isset( $_POST['aitnb_asset_position']['x'] ) ? sanitize_text_field( wp_unslash( $_POST['aitnb_asset_position']['x'] ) ) : '0',
				'y' => isset( $_POST['aitnb_asset_position']['y'] ) ? sanitize_text_field( wp_unslash( $_POST['aitnb_asset_position']['y'] ) ) : '0',
				'z' => isset( $_POST['aitnb_asset_position']['z'] ) ? sanitize_text_field( wp_unslash( $_POST['aitnb_asset_position']['z'] ) ) : '0',
			);
			update_post_meta( $post_id, '_aitnb_asset_position', $position );
		}

		if ( isset( $_POST['aitnb_asset_scale'] ) ) {
			$scale = sanitize_text_field( wp_unslash( $_POST['aitnb_asset_scale'] ) );
			update_post_meta( $post_id, '_aitnb_asset_scale', $scale );
		}

		$enable_fit = isset( $_POST['aitnb_enable_fit_recommender'] ) ? 'yes' : 'no';
		update_post_meta( $post_id, '_aitnb_enable_fit_recommender', $enable_fit );

		if ( isset( $_POST['aitnb_size_chart'] ) ) {
			$size_chart = sanitize_textarea_field( wp_unslash( $_POST['aitnb_size_chart'] ) );
			update_post_meta( $post_id, '_aitnb_size_chart', $size_chart );
		}

		$enable_style = isset( $_POST['aitnb_enable_style_assistant'] ) ? 'yes' : 'no';
		update_post_meta( $post_id, '_aitnb_enable_style_assistant', $enable_style );

		if ( isset( $_POST['aitnb_style_assistant_categories'] ) && is_array( $_POST['aitnb_style_assistant_categories'] ) ) {
			$sanitized_cats = array_map( 'absint', $_POST['aitnb_style_assistant_categories'] );
			update_post_meta( $post_id, '_aitnb_style_assistant_categories', $sanitized_cats );
		} else {
			delete_post_meta( $post_id, '_aitnb_style_assistant_categories' );
		}
	}
}