<template>
  <div class="creo-analysis-page">
    <div class="page-header">
      <h2>
        <i class="fas fa-layer-group"></i>
        模型分析
      </h2>
      <p>Creo模型分析功能</p>
    </div>

    <div class="page-content">
      <div class="analysis-tools-grid">
        <!-- 智能薄壳化分析 -->
        <AnalysisToolCard
          type="shell-analysis"
          title="智能薄壳化分析"
          description="智能识别内部特征，实现安全的模型轻量化"
          icon-class="fas fa-magic"
          :icon-color="'var(--color-creo-analysis-gradient-1)'"
          :features="['内部识别', '安全薄壳', '质量控制', '智能预览']"
          @start-analysis="handleShellAnalysis"
        />

        <!-- 按层级删除优化 -->
        <AnalysisToolCard
          type="hierarchy-deletion"
          title="按层级删除优化"
          description="基于特征层级关系的智能删除策略"
          icon-class="fas fa-layer-group"
          :icon-color="'var(--color-creo-analysis-gradient-2)'"
          :features="['从顶层开始', '安全删除', '关系分析', '批量操作']"
          @start-analysis="handleHierarchyDeletion"
        />

        <!-- 几何复杂度分析 -->
        <AnalysisToolCard
          type="geometry-complexity"
          title="几何复杂度分析"
          description="分析模型几何复杂度，识别优化目标零件"
          icon-class="fas fa-chart-line"
          :icon-color="'var(--color-creo-analysis-gradient-3)'"
          :features="['复杂度计算', '零件排序', '智能筛选', '评分系统']"
          @start-analysis="handleGeometryComplexity"
        />

        <!-- 几何优化分析 -->
        <AnalysisToolCard
          type="geometry-optimization"
          title="几何优化分析"
          description="优化模型几何结构，减少冗余面和曲线"
          icon-class="fas fa-shapes"
          :icon-color="'var(--color-creo-analysis-gradient-4)'"
          :features="['表面简化', '曲线优化', '质量保证', '结构分析']"
          @start-analysis="handleGeometryOptimization"
        />

        <!-- 批量处理 -->
        <AnalysisToolCard
          type="batch-processing"
          title="批量处理"
          description="对多个零件进行批量薄壳化处理"
          icon-class="fas fa-tasks"
          :icon-color="'var(--color-creo-analysis-gradient-5)'"
          :features="['多文件', '并行处理', '进度跟踪', '结果汇总']"
          button-text="开始处理"
          @start-analysis="handleBatchProcessing"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import AnalysisToolCard from '@/components/ui/AnalysisToolCard.vue'
import { creoApi } from '@/services/creoApi'
import { useCADStore } from '@/stores/cad'
import { ElNotification } from 'element-plus'
import { getNotificationConfig } from '@/config/cad'

const emit = defineEmits(['show-shell-analysis-result', 'show-hierarchy-analysis-result', 'show-geometry-complexity-result', 'show-geometry-optimization-params'])

// 获取CAD状态和通知配置
const cadStore = useCADStore()
const notificationConfig = getNotificationConfig()

// 检查Creo连接状态
const checkCreoConnection = () => {
  const creoConnection = cadStore.getCADConnection('creo')
  if (!creoConnection || !creoConnection.connected) {
    ElNotification({
      title: '连接提示',
      message: '请先连接Creo软件才能使用分析功能',
      type: 'warning',
      position: notificationConfig.POSITION,
      duration: notificationConfig.ERROR_DURATION
    })
    return false
  }
  return true
}

// 分析工具事件处理
const handleShellAnalysis = async () => {
  if (!checkCreoConnection()) return

  const result = await creoApi.startShellAnalysis()
  if (result.success) {
    // 将API数据传递给父组件
    emit('show-shell-analysis-result', result.data)
  }
}
const handleHierarchyDeletion = async () => {
  if (!checkCreoConnection()) return

  const result = await creoApi.startHierarchyAnalysis()
  if (result.success) {
    emit('show-hierarchy-analysis-result', result.data)
  }
}
const handleGeometryComplexity = async () => {
  if (!checkCreoConnection()) return

  const result = await creoApi.startGeometryComplexityAnalysis()
  if (result.success) {
    emit('show-geometry-complexity-result', result.data.data)
  }
}
const handleGeometryOptimization = () => {
  if (!checkCreoConnection()) return

  emit('show-geometry-optimization-params')
}
const handleBatchProcessing = () => {
  if (!checkCreoConnection()) return

  // TODO: 批量处理功能待实现
}
</script>

<style scoped>
.creo-analysis-page {
  padding: var(--spacing-2xl);
  height: 100%;
}

.page-header {
  margin-bottom: var(--spacing-2xl);
}

.page-header h2 {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  font-size: var(--font-size-3xl);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  margin: 0 0 var(--spacing-sm) 0;
}

.page-header h2 i {
  color: var(--color-primary);
}

.page-header p {
  color: var(--color-text-secondary);
  margin: 0;
}

.page-content {
  height: calc(100% - 100px);
}

.analysis-tools-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--spacing-2xl);
  padding: var(--spacing-sm);
}
</style>