<template>
  <div class="model-display-container">
    <!-- 初始化蒙层进度条 -->
    <div 
      v-if="showInitializationOverlay" 
      class="initialization-overlay"
      :class="{ 'fade-out': !showInitializationOverlay }"
    >
      <div class="overlay-content">
        <div class="initialization-spinner"></div>
        <div class="initialization-text">{{ $t('model.initializing') }}</div>
        <div class="progress-container">
          <div 
            class="progress-bar"
            :style="{ width: initializationProgress + '%' }"
          ></div>
        </div>
        <div class="progress-text">{{ initializationProgress }}%</div>
      </div>
    </div>

    <!-- 模型展示区域 -->
    <div class="model-content" :class="{ 'blur': showInitializationOverlay }">
      <!-- 模型加载进度条 -->
      <!-- 模型视图 -->
      <div v-if="modelUrl" class="model-viewer-wrapper">
        <ModelViewer 
          :model-url="modelUrl"
          :width="width"
          :height="height"
          :show-export="false"
          :show-info="false"
          @load="onModelLoad"
          @error="onModelError"
        />
      </div>
      <!-- 模型操作按钮 -->
      <div class="model-actions" v-if="!isLoading && !showInitializationOverlay">
        <el-button 
          type="primary" 
          size="small" 
          circle
          @click="emit('preview',modelUrl)"
          :title="$t('model.preview')"
        >
          <el-icon><View /></el-icon>
        </el-button>
        <el-button 
          v-if="showDelete"
          type="danger" 
          size="small" 
          circle
          @click="handleDelete"
          :title="$t('model.delete')"
        >
          <el-icon><Close /></el-icon>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch,onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import { View, Close } from '@element-plus/icons-vue'
import ModelViewer from '@/components/common/ModelViewer.vue'
import { MeshyServer } from '@deotaland/utils'
const meshServer = new MeshyServer()
const props = defineProps({
  imgUrl: {
    type: String,
    required: true
  },
  width: {
    type: [String, Number],
    default: '100%'
  },
  height: {
    type: [String, Number],
    default: '150px'
  },
  showDelete: {
    type: Boolean,
    default: false
  }
})
// 模型URL
const modelUrl = ref('')
const emit = defineEmits(['preview', 'delete', 'load', 'error'])//preview事件用于预览模型
const { t } = useI18n()
// 状态管理
// 是否显示初始化蒙层
const showInitializationOverlay = ref(false)
// 初始化进度
const initializationProgress = ref(0)
// 模型加载完成
const onModelLoad = () => {
  isLoading.value = false
  loadingProgress.value = 100
  emit('load')
}

// 模型加载错误
const onModelError = (error) => {
  isLoading.value = false
  ElMessage.error(t('model.loadError'))
  emit('error', error)
}
// 删除模型
const handleDelete = () => {
  emit('delete')
}

// 对外暴露的方法，用于控制内部状态
const setLoading = (loading, progress = 0) => {
  isLoading.value = loading
  loadingProgress.value = progress
}

const setInitialization = (show, progress = 0) => {
  showInitializationOverlay.value = show
  initializationProgress.value = progress
}

const setLoadingProgress = (progress) => {
  loadingProgress.value = progress
}

const setInitializationProgress = (progress) => {
  initializationProgress.value = progress
}

// 初始化完成时隐藏蒙层
watch(() => initializationProgress.value, (newVal) => {
  if (newVal >= 100) {
    setTimeout(() => {
      showInitializationOverlay.value = false
    }, 500)
  }
})
const loadmodelUrl = ()=>{
  meshServer.createModelTask({
    image_url:props.imgUrl
  },(result)=>{
    showInitializationOverlay.value = true;
    meshServer.getModelTaskStatus(result,(modelurl)=>{
      setInitializationProgress(100)
    modelUrl.value = modelurl;
    },(error)=>{
      emit('delete')
    },(progress)=>{
      setInitializationProgress(progress)
    })
  },()=>{
    emit('delete')
  },{
    
  })
}
onMounted(()=>{
  if(props.imgUrl){
    loadmodelUrl()
  }
})
// 暴露方法给父组件
defineExpose({
  setLoading,
  setInitialization,
  setLoadingProgress,
  setInitializationProgress
})
</script>

<style scoped>
.model-display-container {
  position: relative;
  width: 100%;
  height: 150px;
  border-radius: 8px;
  overflow: hidden;
  background-color: #f3f4f6;
  border: 1px solid #e5e7eb;
  transition: all 0.3s ease;
}

.model-display-container:hover {
  border-color: #6B46C1;
  box-shadow: 0 4px 12px rgba(107, 70, 193, 0.1);
}

/* 初始化蒙层 */
.initialization-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #6B46C1 0%, #9333EA 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  transition: opacity 0.5s ease;
}

.initialization-overlay.fade-out {
  opacity: 0;
  pointer-events: none;
}

.overlay-content {
  text-align: center;
  color: white;
  padding: 24px;
}

.initialization-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

.initialization-text {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 16px;
}

/* 加载状态 */
.loading-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f4f6;
  border-top: 3px solid #6B46C1;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 12px;
}

.loading-text {
  font-size: 14px;
  color: #6b7280;
  margin-bottom: 12px;
}

/* 进度条样式 */
.progress-container {
  width: 200px;
  height: 4px;
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  overflow: hidden;
  margin: 12px auto;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #6B46C1 0%, #A78BFA 100%);
  border-radius: 2px;
  transition: width 0.3s ease;
}

.progress-bar.loading {
  background: linear-gradient(90deg, #6B46C1 0%, #A78BFA 100%);
}

.progress-text {
  font-size: 12px;
  color: #6b7280;
  text-align: center;
  margin-top: 4px;
}

.initialization-overlay .progress-text {
  color: white;
}

/* 模型内容 */
.model-content {
  width: 100%;
  height: 100%;
  transition: filter 0.3s ease;
}

.model-content.blur {
  filter: blur(2px);
}

.model-viewer-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 模型操作按钮 */
.model-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 8px;
  z-index: 10;
}

.model-actions .el-button {
  opacity: 0.8;
  transition: all 0.2s ease;
}

.model-actions .el-button:hover {
  opacity: 1;
  transform: scale(1.1);
}

/* 动画 */
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .model-display-container {
    height: 120px;
  }
  
  .progress-container {
    width: 160px;
  }
  
  .model-actions {
    top: 4px;
    right: 4px;
    gap: 4px;
  }
  
  .model-actions .el-button {
    width: 24px;
    height: 24px;
  }
}

@media (min-width: 1024px) {
  .model-display-container {
    height: 180px;
  }
  
  .progress-container {
    width: 220px;
  }
}

/* 深色主题支持 */
@media (prefers-color-scheme: dark) {
  .model-display-container {
    background-color: #1f2937;
    border-color: #374151;
  }
  
  .loading-container {
    background: rgba(31, 41, 55, 0.95);
  }
  
  .loading-text {
    color: #d1d5db;
  }
  
  .progress-text {
    color: #d1d5db;
  }
}
</style>