<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
 * Provides the admin area view for the product meta box.
 *
 * This file is used to markup the admin-facing aspects of the plugin
 * for an individual product's settings.
 *
 * @link       https://trynbuy.edubrazeal.com/
 * @since      1.0.0
 *
 * @package    Ai_Try_N_Buy
 * @subpackage Ai_Try_N_Buy/admin/views
 */

$position                 = get_post_meta( $post->ID, '_aitnb_asset_position', true );
$scale                    = get_post_meta( $post->ID, '_aitnb_asset_scale', true );
$ar_mode                  = get_post_meta( $post->ID, '_aitnb_ar_mode', true );
$enable_fit_recommender   = get_post_meta( $post->ID, '_aitnb_enable_fit_recommender', true );
$size_chart               = get_post_meta( $post->ID, '_aitnb_size_chart', true );
$enable_style_assistant   = get_post_meta( $post->ID, '_aitnb_enable_style_assistant', true );
$style_assistant_cats     = get_post_meta( $post->ID, '_aitnb_style_assistant_categories', true );

if ( ! is_array( $style_assistant_cats ) ) {
	$style_assistant_cats = array();
}

$position = wp_parse_args( (array) $position, array( 'x' => 0, 'y' => 0.10, 'z' => -0.5 ) );
$scale    = ! empty( $scale ) ? $scale : 0.3;
$ar_mode  = ! empty( $ar_mode ) ? $ar_mode : 'face';

?>

<table class="form-table">
	<tbody>
		<tr>
			<th colspan="2">
				<h4><?php esc_html_e( 'Virtual Try-On', 'virtual-ai-fit-and-style-assistant' ); ?></h4>
			</th>
		</tr>
		<tr>
			<th>
				<label for="aitnb_enable_tryon">
					<?php esc_html_e( 'Enable Virtual Try-On', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<input type="checkbox" id="aitnb_enable_tryon" name="aitnb_enable_tryon" value="yes" <?php checked( $enable_tryon, 'yes' ); ?> />
				<p class="description">
					<?php esc_html_e( 'Check this box to enable the virtual try-on feature for this product.', 'virtual-ai-fit-and-style-assistant' ); ?>
				</p>
			</td>
		</tr>

		<tr>
			<th>
				<label for="aitnb_ar_mode">
					<?php esc_html_e( 'AR Tracking Mode', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<select id="aitnb_ar_mode" name="aitnb_ar_mode">
					<option value="face" <?php selected( $ar_mode, 'face' ); ?>><?php esc_html_e( 'Face Tracking (Front Camera)', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="world" <?php selected( $ar_mode, 'world' ); ?>><?php esc_html_e( 'World Tracking (Back Camera)', 'virtual-ai-fit-and-style-assistant' ); ?></option>
				</select>
				<p class="description">
					<?php esc_html_e( 'Choose "Face Tracking" for items like glasses or hats. Choose "World Tracking" for items like furniture.', 'virtual-ai-fit-and-style-assistant' ); ?>
				</p>
			</td>
		</tr>

		<tr>
			<th>
				<label for="aitnb_item_type">
					<?php esc_html_e( 'Item Type', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<select id="aitnb_item_type" name="aitnb_item_type">
					<option value=""><?php esc_html_e( '-- Select Type --', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="sunglasses" <?php selected( $item_type, 'sunglasses' ); ?>><?php esc_html_e( 'Sunglasses', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="hat" <?php selected( $item_type, 'hat' ); ?>><?php esc_html_e( 'Hat', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="necklace" <?php selected( $item_type, 'necklace' ); ?>><?php esc_html_e( 'Necklace', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="earrings" <?php selected( $item_type, 'earrings' ); ?>><?php esc_html_e( 'Earrings', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="chair" <?php selected( $item_type, 'chair' ); ?>><?php esc_html_e( 'Chair', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="table" <?php selected( $item_type, 'table' ); ?>><?php esc_html_e( 'Table', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="lamp" <?php selected( $item_type, 'lamp' ); ?>><?php esc_html_e( 'Lamp', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="artwork" <?php selected( $item_type, 'artwork' ); ?>><?php esc_html_e( 'Artwork', 'virtual-ai-fit-and-style-assistant' ); ?></option>
					<option value="other" <?php selected( $item_type, 'other' ); ?>><?php esc_html_e( 'Other', 'virtual-ai-fit-and-style-assistant' ); ?></option>
				</select>
				<p class="description">
					<?php esc_html_e( 'Select the type of item to help position the overlay correctly.', 'virtual-ai-fit-and-style-assistant' ); ?>
				</p>
			</td>
		</tr>

		<tr>
			<th>
				<label for="aitnb-upload-asset-button">
					<?php esc_html_e( 'Try-On Asset', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<div class="aitnb-asset-uploader">
					<div class="aitnb-asset-preview">
						<?php
						if ( $asset_id ) {
							$asset_url       = wp_get_attachment_url( $asset_id );
							$asset_url_lower = strtolower( $asset_url );
							if ( $asset_url && ( substr( $asset_url_lower, -4 ) === '.glb' ) ) {
								// For GLB files, show the filename as WordPress can't generate a thumbnail.
								echo '<strong>' . esc_html__( 'Selected:', 'virtual-ai-fit-and-style-assistant' ) . '</strong><br/>' . esc_html( basename( $asset_url ) );
							} else {
								// For images, display the thumbnail.
								echo wp_get_attachment_image( $asset_id, 'thumbnail' );
							}
						}
						?>
					</div>
					<input type="hidden" id="aitnb_overlay_asset_id" name="aitnb_overlay_asset_id" value="<?php echo esc_attr( $asset_id ); ?>" />
					<button type="button" class="button aitnb-upload-asset-button" id="aitnb-upload-asset-button">
						<?php esc_html_e( 'Upload / Select Asset', 'virtual-ai-fit-and-style-assistant' ); ?>
					</button>
					<button type="button" class="button aitnb-remove-asset-button <?php echo $asset_id ? '' : 'aitnb-hidden'; ?>">
						<?php esc_html_e( 'Remove Asset', 'virtual-ai-fit-and-style-assistant' ); ?>
					</button>
					<p class="description">
						<?php
							echo wp_kses(
								__( '<b>Face Tracking:</b> Accepts .png and .glb files.<br><b>World Tracking:</b> Accepts .glb 3D models only.', 'virtual-ai-fit-and-style-assistant' ),
								array(
									'br' => array(),
									'b'  => array(),
								)
							);
						?>
					</p>
				</div>
			</td>
		</tr>

		<tr>
			<th>
				<label><?php esc_html_e( 'Asset Position (X, Y, Z)', 'virtual-ai-fit-and-style-assistant' ); ?></label>
			</th>
			<td>
				<input type="text" size="4" name="aitnb_asset_position[x]" value="<?php echo esc_attr( $position['x'] ); ?>" placeholder="X" />
				<input type="text" size="4" name="aitnb_asset_position[y]" value="<?php echo esc_attr( $position['y'] ); ?>" placeholder="Y" />
				<input type="text" size="4" name="aitnb_asset_position[z]" value="<?php echo esc_attr( $position['z'] ); ?>" placeholder="Z" />
			</td>
		</tr>
		<tr>
			<th>
				<label for="aitnb_asset_scale"><?php esc_html_e( 'Asset Scale', 'virtual-ai-fit-and-style-assistant' ); ?></label>
			</th>
			<td>
				<input type="text" size="4" id="aitnb_asset_scale" name="aitnb_asset_scale" value="<?php echo esc_attr( $scale ); ?>" />
				<p class="description"><?php esc_html_e( 'A single number to control the overall size. e.g., 0.3 for images or 0.0015 for models.', 'virtual-ai-fit-and-style-assistant' ); ?></p>
			</td>
		</tr>

		<tr>
			<th colspan="2">
				<hr>
				<h4><?php esc_html_e( 'AI Fit Recommendation', 'virtual-ai-fit-and-style-assistant' ); ?></h4>
			</th>
		</tr>
		<tr>
			<th>
				<label for="aitnb_enable_fit_recommender">
					<?php esc_html_e( 'Enable AI Fit Recommendation', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<input type="checkbox" id="aitnb_enable_fit_recommender" name="aitnb_enable_fit_recommender" value="yes" <?php checked( $enable_fit_recommender, 'yes' ); ?> />
				<p class="description">
					<?php esc_html_e( 'Check this box to enable the "Find My Size" feature for this product.', 'virtual-ai-fit-and-style-assistant' ); ?>
				</p>
			</td>
		</tr>
		<tr>
			<th>
				<label for="aitnb_size_chart">
					<?php esc_html_e( 'Product Size Chart', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<textarea id="aitnb_size_chart" name="aitnb_size_chart" rows="5" class="large-text"><?php echo esc_textarea( $size_chart ); ?></textarea>
				<p class="description">
					<?php
					echo wp_kses(
						__( 'Enter the size chart data, one size per line. Use a colon (:) to separate the size name from its measurements.<br><b>Example:</b><br><code>Small: Chest 36-38", Waist 30-32"</code><br><code>Medium: Chest 39-41", Waist 32-34"</code>', 'virtual-ai-fit-and-style-assistant' ),
						array(
							'br'   => array(),
							'b'    => array(),
							'code' => array(),
						)
					);
					?>
				</p>
			</td>
		</tr>

		<tr>
			<th colspan="2">
				<hr>
				<h4><?php esc_html_e( 'AI Style Assistant', 'virtual-ai-fit-and-style-assistant' ); ?></h4>
			</th>
		</tr>
		<tr>
			<th>
				<label for="aitnb_enable_style_assistant">
					<?php esc_html_e( 'Enable AI Style Assistant', 'virtual-ai-fit-and-style-assistant' ); ?>
				</label>
			</th>
			<td>
				<input type="checkbox" id="aitnb_enable_style_assistant" name="aitnb_enable_style_assistant" value="yes" <?php checked( $enable_style_assistant, 'yes' ); ?> />
				<p class="description">
					<?php esc_html_e( 'Check this box to enable the "Complete The Look" feature for this product.', 'virtual-ai-fit-and-style-assistant' ); ?>
				</p>
			</td>
		</tr>
		<tr>
			<th>
				<label><?php esc_html_e( 'Recommendation Categories', 'virtual-ai-fit-and-style-assistant' ); ?></label>
			</th>
			<td>
				<div class="aitnb-category-checklist">
					<?php
					$all_categories = get_terms(
						array(
							'taxonomy'   => 'product_cat',
							'hide_empty' => false,
						)
					);

					if ( ! empty( $all_categories ) && ! is_wp_error( $all_categories ) ) {
						foreach ( $all_categories as $category ) {
							?>
							<label class="aitnb-checklist-label">
								<input type="checkbox" name="aitnb_style_assistant_categories[]" value="<?php echo esc_attr( $category->term_id ); ?>" <?php checked( in_array( $category->term_id, $style_assistant_cats, true ) ); ?> />
								<?php echo esc_html( $category->name ); ?>
							</label>
							<?php
						}
					} else {
						esc_html_e( 'No product categories found.', 'virtual-ai-fit-and-style-assistant' );
					}
					?>
				</div>
				<p class="description">
					<?php esc_html_e( 'Select the categories from which the AI should recommend complementary products.', 'virtual-ai-fit-and-style-assistant' ); ?>
				</p>
			</td>
		</tr>
	</tbody>
</table>

<template id="aitnb-asset-preview-image-template">
	<img src="" alt="<?php esc_attr_e( 'Asset Preview', 'virtual-ai-fit-and-style-assistant' ); ?>" />
</template>

<template id="aitnb-asset-preview-text-template">
	<strong><?php esc_html_e( 'Selected:', 'virtual-ai-fit-and-style-assistant' ); ?></strong><br/>
	<span class="aitnb-asset-filename"></span>
</template>