<?php
    if ( ! defined( 'ABSPATH' ) ) exit;

    $model_src = get_post_meta($post->ID, '_u3dmv_model_src', true);
    $model_height = get_post_meta($post->ID, '_u3dmv_model_height', true);
    $model_display = get_post_meta($post->ID, '_u3dmv_model_display', true);
    $model_position = get_post_meta($post->ID, '_u3dmv_model_position', true);
    $model_dimensions = get_post_meta($post->ID, '_u3dmv_model_dimensions', true);
    $model_dimensions_scale_factor = get_post_meta($post->ID, '_u3dmv_model_dimensions_scale_factor', true);
    $model_dimensions_custom = get_post_meta($post->ID, '_u3dmv_model_dimensions_custom', true);
    $model_dimensions_x = get_post_meta($post->ID, '_u3dmv_model_dimensions_x', true);
    $model_dimensions_y = get_post_meta($post->ID, '_u3dmv_model_dimensions_y', true);
    $model_dimensions_z = get_post_meta($post->ID, '_u3dmv_model_dimensions_z', true);
    $model_poster = get_post_meta($post->ID, '_u3dmv_model_poster', true);
    $model_env = get_post_meta($post->ID, '_u3dmv_model_env', true);
    $model_skybox = get_post_meta($post->ID, '_u3dmv_model_skybox', true);
    $model_tone_mapping = get_post_meta($post->ID, '_u3dmv_model_tone_mapping', true);
    $model_exposure = get_post_meta($post->ID, '_u3dmv_model_exposure', true);
    $model_shadow_int = get_post_meta($post->ID, '_u3dmv_model_shadow_int', true);
    $model_shadow_soft = get_post_meta($post->ID, '_u3dmv_model_shadow_soft', true);
    $model_camera_orbit = get_post_meta($post->ID, '_u3dmv_model_camera_orbit', true);
    $model_camera_orbit_max = get_post_meta($post->ID, '_u3dmv_model_camera_orbit_max', true);
    $model_camera_orbit_min = get_post_meta($post->ID, '_u3dmv_model_camera_orbit_min', true);
    $model_camera_target =  get_post_meta($post->ID, '_u3dmv_model_camera_target', true);
    $model_camera_fov = get_post_meta($post->ID, '_u3dmv_model_camera_fov', true);
    $model_camera_fov_max = get_post_meta($post->ID, '_u3dmv_model_camera_fov_max', true);
    $model_camera_fov_min = get_post_meta($post->ID, '_u3dmv_model_camera_fov_min', true);
    $model_loading = get_post_meta($post->ID, '_u3dmv_model_loading', true);
    $model_rotation = get_post_meta($post->ID, '_u3dmv_model_rotation', true);
    $model_ar = get_post_meta($post->ID, '_u3dmv_model_ar', true);
    $model_ios_src = get_post_meta($post->ID, '_u3dmv_model_ios_src', true);

    $style = [];

    if (!empty($model_height)) {
        $style[] = 'height:' . esc_attr($model_height) . 'px';
    }

    $is_half_width = ($model_display === '1' || $model_position === "replace") && $model_position !== 'under';
    $is_side = in_array($model_position, ['left', 'right'], true);

    if ($is_half_width || $is_side) {
        $style[] = 'width: 50%';
    }

    if ($model_position === 'right') {
        $style[] = 'float: right';
    } elseif ($model_position === 'left') {
        $style[] = 'float: left';
    } elseif ($model_position === 'replace') {
        $style[] = 'position: absolute';
    }
    
    $style_attr = !empty($style) ? 'style="' . implode('; ', $style) . ';"' : '';

    $model_dimensions_active = $model_dimensions !== '0' && $model_dimensions !== '';
    $model_dimensions_custom_active =  $model_dimensions_custom === "1";
?>

<div class="u3dmv-model-viewer-wrapper" <?php echo esc_attr($style_attr); ?>>
    <model-viewer class="model-viewer" id="u3dmv-model-viewer-woo"
        <?php echo $model_dimensions_active ? 'data-dim-mode="' . ($model_dimensions_custom_active ? 'custom' : 'real') . '"' : '' ?>
        <?php echo $model_dimensions_active && $model_dimensions_scale_factor !== null && $model_dimensions_scale_factor !== "" ? "data-scale-factor='" . esc_attr($model_dimensions_scale_factor) . "'" : "" ?>
        <?php echo ($model_dimensions_active && $model_dimensions_custom_active && $model_dimensions_x !== null) ? "data-dim-x='" . esc_attr($model_dimensions_x) . "'" : "" ?>
        <?php echo ($model_dimensions_active && $model_dimensions_custom_active && $model_dimensions_y !== null) ? "data-dim-y='" . esc_attr($model_dimensions_y) . "'" : "" ?>
        <?php echo ($model_dimensions_active && $model_dimensions_custom_active && $model_dimensions_z !== null) ? "data-dim-z='" . esc_attr($model_dimensions_z) . "'" : "" ?>
        src="<?php echo esc_attr($model_src);?>"
        poster="<?php echo $model_poster !== null ? esc_attr($model_poster) : ""  ?>"
        environment-image="<?php echo $model_env !== null ? esc_attr($model_env) : "" ?>"
        skybox-image="<?php echo $model_skybox !== null ? esc_attr($model_skybox) : "" ?>"
        tone-mapping="<?php echo $model_tone_mapping !== null ? esc_attr($model_tone_mapping) : "none" ?>"
        exposure="<?php echo $model_exposure !== null ? esc_attr($model_exposure) : "" ?>"
        shadow-intensity="<?php echo $model_shadow_int !== null ? esc_attr($model_shadow_int) : "" ?>"
        shadow-softness="<?php echo $model_shadow_soft !== null ? esc_attr($model_shadow_soft) : "" ?>"
        camera-orbit="<?php echo $model_camera_orbit !== null ? esc_attr($model_camera_orbit) : "" ?>"
        max-camera-orbit="<?php echo $model_camera_orbit_max !== null ? esc_attr($model_camera_orbit_max) : "" ?>"
        min-camera-orbit="<?php echo $model_camera_orbit_min !== null ? esc_attr($model_camera_orbit_min) : "" ?>"
        camera-target="<?php echo $model_camera_target !== null ? esc_attr($model_camera_target) : "" ?>"
        field-of-view="<?php echo $model_camera_fov !== null ? esc_attr($model_camera_fov) : "" ?>"
        max-field-of-view="<?php echo $model_camera_fov_max !== null ? esc_attr($model_camera_fov_max) : "" ?>"
        min-field-of-view="<?php echo $model_camera_fov_min !== null ? esc_attr($model_camera_fov_min) : "" ?>"
        loading="<?php echo $model_loading !== null ? esc_attr($model_loading) : 'auto' ?>"
        ios-src="<?php echo $model_ios_src !== null ? esc_attr($model_ios_src) : "" ?>"
        <?php echo $model_rotation !== '0' && $model_rotation !== '' ? 'auto-rotate' : "" ?>
        <?php echo $model_ar !== '0' && $model_ar !== '' ? 'ar ar-modes="webxr scene-viewer quick-look"' : "" ?>
        camera-controls touch-action="pan-y"
    >
    <?php if ($model_dimensions !== '0' && $model_dimensions !== ''): ?>
        <button slot="hotspot-dot+X-Y+Z" class="dot" data-position="1 -1 1" data-normal="1 0 0"></button>
        <button slot="hotspot-dim+X-Y" class="dim" data-position="1 -1 0" data-normal="1 0 0"></button>
        <button slot="hotspot-dot+X-Y-Z" class="dot" data-position="1 -1 -1" data-normal="1 0 0"></button>
        <button slot="hotspot-dim+X-Z" class="dim" data-position="1 0 -1" data-normal="1 0 0"></button>
        <button slot="hotspot-dot+X+Y-Z" class="dot" data-position="1 1 -1" data-normal="0 1 0"></button>
        <button slot="hotspot-dim+Y-Z" class="dim" data-position="0 -1 -1" data-normal="0 1 0"></button>
        <button slot="hotspot-dot-X+Y-Z" class="dot" data-position="-1 1 -1" data-normal="0 1 0"></button>
        <button slot="hotspot-dim-X-Z" class="dim" data-position="-1 0 -1" data-normal="-1 0 0"></button>
        <button slot="hotspot-dot-X-Y-Z" class="dot" data-position="-1 -1 -1" data-normal="-1 0 0"></button>
        <button slot="hotspot-dim-X-Y" class="dim" data-position="-1 -1 0" data-normal="-1 0 0"></button>
        <button slot="hotspot-dot-X-Y+Z" class="dot" data-position="-1 -1 1" data-normal="-1 0 0"></button>
        <svg id="dimLines" width="100%" height="100%" class="dimensionLineContainer">
            <line class="dimensionLine"></line>
            <line class="dimensionLine"></line>
            <line class="dimensionLine"></line>
            <line class="dimensionLine"></line>
            <line class="dimensionLine"></line>
        </svg>
    <?php endif; ?>
    </model-viewer>
</div>