<template>
  <div class="revit-envelope-optimization revit-theme">
    <div class="header-actions">
      <button class="icon-btn" @click="goBack" title="返回">
        <i class="fas fa-arrow-left"></i> 返回分析中心
      </button>
    </div>

    <div class="optimization-header">
      <h2>
        <i class="fas fa-compress-arrows-alt"></i>
        Revit模型轻量化处理
      </h2>
      <p class="subtitle">保留建筑模型的外部轮廓，自动删除内部构件，显著减少文件体积并加速渲染。</p>
    </div>

    <!-- 任务参数设置区 -->
    <div class="settings-panel" v-if="taskStatus === 'idle' || taskStatus === 'failed' || taskStatus === 'cancelled'">
      <div class="setting-group">
        <label>处理模式 (Mode)</label>
        <div class="mode-options">
          <label class="mode-card" :class="{ active: formData.mode === 'Conservative' }">
            <input type="radio" v-model="formData.mode" value="Conservative" />
            <div class="mode-content">
              <h5>保守 保留</h5>
              <p>最大程度保留构件</p>
            </div>
          </label>
          <label class="mode-card" :class="{ active: formData.mode === 'Standard' }">
            <input type="radio" v-model="formData.mode" value="Standard" />
            <div class="mode-content">
              <h5>标准 (推荐)</h5>
              <p>平衡显示效果与体积</p>
            </div>
          </label>
          <label class="mode-card" :class="{ active: formData.mode === 'Aggressive' }">
            <input type="radio" v-model="formData.mode" value="Aggressive" />
            <div class="mode-content">
              <h5>激进 删除</h5>
              <p>尽可能多地移除内部细节</p>
            </div>
          </label>
          <label class="mode-card" :class="{ active: formData.mode === 'EnvelopeOnly' }">
            <input type="radio" v-model="formData.mode" value="EnvelopeOnly" />
            <div class="mode-content">
              <h5>仅保留轮廓</h5>
              <p>严格只保留最外层表皮</p>
            </div>
          </label>
        </div>
      </div>

      <div class="action-bar">
        <button class="primary-btn" @click="startTask" :disabled="isStarting">
          <i class="fas" :class="isStarting ? 'fa-spinner fa-spin' : 'fa-play'"></i>
          {{ isStarting ? '正在初始化...' : '开始执行轻量化' }}
        </button>
      </div>

      <div v-if="errorMessage" class="error-message">
        <i class="fas fa-exclamation-triangle"></i> {{ errorMessage }}
      </div>
    </div>

    <!-- 任务执行中进度区 -->
    <div class="execution-panel" v-else-if="taskStatus === 'Running' || taskStatus === 'Pending'">
      <h3>
        <i class="fas fa-cog fa-spin"></i>
        正在处理模型，请耐心等待...
      </h3>
      <p class="status-desc">当前状态: {{ taskStatus === 'Pending' ? '排队中' : '运行中' }}</p>
      
      <div class="progress-container">
        <div class="progress-bar">
          <div class="progress-fill active"></div>
        </div>
      </div>
      
      <div class="execution-actions">
        <button class="cancel-btn" @click="cancelTask">
          <i class="fas fa-times"></i> 取消任务
        </button>
      </div>
    </div>

    <!-- 任务结果区 -->
    <div class="result-panel" v-else-if="taskStatus === 'Completed' && taskResult">
      <div class="success-header">
        <i class="fas fa-check-circle success-icon"></i>
        <h3>模型轻量化完成</h3>
      </div>

      <div class="metrics-grid">
        <div class="metric-card">
          <div class="metric-icon"><i class="fas fa-trash-alt"></i></div>
          <div class="metric-info">
            <div class="metric-value">{{ taskResult.removedCount }}</div>
            <div class="metric-label">移除内部构件数</div>
          </div>
        </div>
        <div class="metric-card">
          <div class="metric-icon"><i class="fas fa-compress"></i></div>
          <div class="metric-info">
            <div class="metric-value highlight">{{ taskResult.reduction }}</div>
            <div class="metric-label">体积减小比例</div>
          </div>
        </div>
        <div class="metric-card">
          <div class="metric-icon"><i class="fas fa-stopwatch"></i></div>
          <div class="metric-info">
            <div class="metric-value">{{ taskResult.processingTimeSeconds }}s</div>
            <div class="metric-label">处理耗时</div>
          </div>
        </div>
      </div>

      <div class="details-box">
        <h4>文件大小对比</h4>
        <div class="size-comparison">
          <div class="size-item original">
            <span>原始大小</span>
            <strong>{{ taskResult.originalSize }}</strong>
          </div>
          <div class="size-arrow">
            <i class="fas fa-long-arrow-alt-right"></i>
          </div>
          <div class="size-item optimized">
            <span>优化后大小</span>
            <strong>{{ taskResult.optimizedSize }}</strong>
          </div>
        </div>
        
        <div v-if="taskResult.backupPath" class="backup-info">
          <i class="fas fa-info-circle"></i>
          原文件已备份至: <span>{{ taskResult.backupPath }}</span>
        </div>
      </div>

      <div class="result-actions">
        <button class="primary-btn" @click="resetTask">
          <i class="fas fa-redo"></i> 再次处理其他参数
        </button>
        <button class="secondary-btn" @click="goBack">
          <i class="fas fa-home"></i> 返回分析中心
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onUnmounted } from 'vue'
import { ElNotification } from 'element-plus'
import revitApi from '@/services/revitApi'

const emit = defineEmits(['page-change'])

// 状态定义
// idle | Pending | Running | Completed | Failed | Cancelled
const taskStatus = ref('idle')
const currentTaskId = ref(null)
const isStarting = ref(false)
const errorMessage = ref('')
const taskResult = ref(null)

// 轮询定时器
let pollingTimer = null

// 单文件常量配置
const DEFAULT_REVIT_CONFIG = {
  MODE: 'EnvelopeOnly',
  BACKUP_ORIGINAL: false
};

// 表单数据
const formData = reactive({
  mode: DEFAULT_REVIT_CONFIG.MODE,
  backupOriginal: DEFAULT_REVIT_CONFIG.BACKUP_ORIGINAL
})

// 返回函数
const goBack = () => {
  emit('page-change', 'analysis-tools')
}

// 重置状态
const resetTask = () => {
  taskStatus.value = 'idle'
  currentTaskId.value = null
  taskResult.value = null
  errorMessage.value = ''
}

// 停止轮询
const stopPolling = () => {
  if (pollingTimer) {
    clearInterval(pollingTimer)
    pollingTimer = null
  }
}

// 启动任务
const startTask = async () => {
  isStarting.value = true
  errorMessage.value = ''
  
  try {
    const res = await revitApi.executeShellOptimization(formData.mode, formData.backupOriginal)
    if (res.success && res.data && res.data.code === 202) {
      currentTaskId.value = res.data.data.taskId
      taskStatus.value = 'Pending'
      // 启动轮询
      startPolling()
      ElNotification({
        title: '任务已创建',
        message: '轻量化任务已提交，后台开始处理。',
        type: 'success'
      })
    } else {
      taskStatus.value = 'failed'
      errorMessage.value = res.error || '创建任务失败，返回数据异常。'
    }
  } catch (err) {
    taskStatus.value = 'failed'
    errorMessage.value = err.message || '网络请求错误'
  } finally {
    isStarting.value = false
  }
}

// 轮询逻辑
const startPolling = () => {
  stopPolling() // 确保没有重复的定时器
  pollingTimer = setInterval(async () => {
    if (!currentTaskId.value) return
    
    try {
      const res = await revitApi.getTaskStatus(currentTaskId.value)
      if (res.success && res.data && res.data.data) {
        const data = res.data.data
        taskStatus.value = data.status
        
        if (data.status === 'Completed') {
          stopPolling()
          taskResult.value = data.result
          ElNotification({
            title: '处理完成',
            message: '模型轻量化处理已成功执行完成！',
            type: 'success'
          })
        } else if (data.status === 'Failed') {
          stopPolling()
          errorMessage.value = data.errorMessage || '处理过程中出现未知错误'
          ElNotification({
            title: '处理失败',
            message: errorMessage.value,
            type: 'error'
          })
        } else if (data.status === 'Cancelled') {
          stopPolling()
        }
      }
    } catch (err) {
      console.error('Task status polling failed:', err)
      // 如果网络短暂断开，可以允许几次失败，这里为简单直接报错
      stopPolling()
      taskStatus.value = 'failed'
      errorMessage.value = '轮询状态失败: ' + err.message
    }
  }, 2000) // 每2秒查询一次
}

// 取消任务
const cancelTask = async () => {
  if (!currentTaskId.value) return
  
  try {
    const res = await revitApi.cancelTask(currentTaskId.value)
    if (res.success) {
      taskStatus.value = 'Cancelled'
      stopPolling()
      ElNotification({
        title: '已取消',
        message: '任务已被成功取消。',
        type: 'info'
      })
    }
  } catch (err) {
    ElNotification({
      title: '取消失败',
      message: err.message,
      type: 'warning'
    })
  }
}

// 页面销毁时清理状态
onUnmounted(() => {
  stopPolling()
})
</script>

<style scoped>
.revit-envelope-optimization {
  background: linear-gradient(135deg, var(--color-bg-secondary) 0%, var(--color-bg-primary) 100%);
  border: 1px solid var(--color-border-primary);
  border-radius: 16px;
  padding: 24px;
  backdrop-filter: blur(20px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  animation: slideInUp 0.4s ease-out;
  color: var(--color-text-primary);
  min-height: 100%;
}

.header-actions {
  margin-bottom: 20px;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--color-primary);
  font-size: 1.1em;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
}
.icon-btn:hover {
  transform: translateX(-4px);
  color: #5580ff;
}

.optimization-header {
  text-align: center;
  margin-bottom: 32px;
}

.optimization-header h2 {
  color: var(--color-primary);
  font-size: 1.8em;
  font-weight: 600;
  margin: 0 0 12px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.optimization-header .subtitle {
  color: var(--color-text-secondary);
  font-size: 1em;
  max-width: 600px;
  margin: 0 auto;
  line-height: 1.6;
}

/* 设定区 */
.settings-panel {
  background: var(--color-bg-card);
  border-radius: 12px;
  padding: 32px;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid var(--color-border-primary);
}

.setting-group {
  margin-bottom: 32px;
}

.setting-group > label {
  display: block;
  font-size: 1.1em;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--color-text-primary);
}

.mode-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.mode-card {
  display: block;
  position: relative;
  border: 2px solid var(--color-border-primary);
  border-radius: 12px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s;
  background: var(--color-bg-secondary);
}
.mode-card:hover {
  border-color: var(--color-primary);
  transform: translateY(-2px);
}
.mode-card.active {
  border-color: var(--color-primary);
  background: linear-gradient(135deg, rgba(58, 114, 255, 0.1) 0%, rgba(58, 114, 255, 0.05) 100%);
  box-shadow: 0 4px 15px rgba(58, 114, 255, 0.2);
}
.mode-card input[type="radio"] {
  display: none;
}
.mode-content h5 {
  margin: 0 0 8px 0;
  font-size: 1.1em;
  color: var(--color-text-primary);
}
.mode-content p {
  margin: 0;
  font-size: 0.85em;
  color: var(--color-text-secondary);
}

.toggle-group {
  background: var(--color-bg-secondary);
  padding: 16px;
  border-radius: 12px;
}

.toggle-control {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.action-bar {
  text-align: center;
  margin-top: 32px;
}

.primary-btn {
  background: linear-gradient(135deg, #3a72ff 0%, #2f5ce6 100%);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 14px 40px;
  font-size: 1.1em;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(58, 114, 255, 0.4);
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 auto;
}
.primary-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(58, 114, 255, 0.6);
}
.primary-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.secondary-btn {
  background: var(--color-bg-secondary);
  color: var(--color-text-primary);
  border: 1px solid var(--color-border-primary);
  border-radius: 8px;
  padding: 14px 40px;
  font-size: 1.1em;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 10px;
}
.secondary-btn:hover {
  background: var(--color-white-rgb-1);
}

.error-message {
  margin-top: 20px;
  padding: 12px;
  background: rgba(255, 73, 73, 0.1);
  color: #ff4949;
  border-left: 4px solid #ff4949;
  border-radius: 4px;
}

/* 执行进度区 */
.execution-panel {
  text-align: center;
  padding: 60px 20px;
}

.execution-panel h3 {
  color: var(--color-primary);
  font-size: 1.6em;
  margin-bottom: 16px;
}

.status-desc {
  color: var(--color-text-secondary);
  font-size: 1.1em;
  margin-bottom: 40px;
}

.progress-container {
  max-width: 500px;
  margin: 0 auto 40px auto;
}

.progress-bar {
  height: 8px;
  background: var(--color-border-primary);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #3a72ff, #00d2ff);
  width: 50%;
  border-radius: 4px;
}
.progress-fill.active {
  animation: progressIndeterminate 2s infinite ease-in-out;
  transform-origin: 0% 50%;
}

@keyframes progressIndeterminate {
  0% { transform: translateX(-100%) scaleX(0.2); }
  50% { transform: translateX(0) scaleX(1); }
  100% { transform: translateX(200%) scaleX(0.2); }
}

.cancel-btn {
  background: transparent;
  color: #ff4949;
  border: 1px solid #ff4949;
  border-radius: 6px;
  padding: 10px 24px;
  font-size: 1em;
  cursor: pointer;
  transition: all 0.2s;
}
.cancel-btn:hover {
  background: rgba(255, 73, 73, 0.1);
}

/* 结果区 */
.result-panel {
  max-width: 900px;
  margin: 0 auto;
}

.success-header {
  text-align: center;
  margin-bottom: 40px;
}
.success-icon {
  font-size: 64px;
  color: #13ce66;
  margin-bottom: 16px;
  text-shadow: 0 4px 15px rgba(19, 206, 102, 0.3);
}
.success-header h3 {
  font-size: 1.8em;
  color: var(--color-text-primary);
  margin: 0;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.metric-card {
  background: var(--color-bg-card);
  border: 1px solid var(--color-border-primary);
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
}
.metric-icon {
  font-size: 32px;
  color: var(--color-primary);
  width: 60px;
  height: 60px;
  background: rgba(58, 114, 255, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.metric-info {
  flex: 1;
}
.metric-value {
  font-size: 1.8em;
  font-weight: 700;
  color: var(--color-text-primary);
}
.metric-value.highlight {
  color: #13ce66;
}
.metric-label {
  color: var(--color-text-secondary);
  font-size: 0.9em;
  margin-top: 4px;
}

.details-box {
  background: var(--color-bg-card);
  border: 1px solid var(--color-border-primary);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 40px;
}
.details-box h4 {
  margin: 0 0 20px 0;
  color: var(--color-text-primary);
  font-size: 1.2em;
}

.size-comparison {
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: var(--color-bg-secondary);
  padding: 24px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.size-item {
  text-align: center;
}
.size-item span {
  display: block;
  color: var(--color-text-secondary);
  margin-bottom: 8px;
}
.size-item strong {
  font-size: 1.6em;
  color: var(--color-text-primary);
}
.size-item.optimized strong {
  color: #13ce66;
}

.size-arrow i {
  font-size: 24px;
  color: var(--color-border-primary);
}

.backup-info {
  color: var(--color-text-secondary);
  font-size: 0.95em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.backup-info span {
  color: var(--color-primary);
  word-break: break-all;
}

.result-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>
