<template>
  <MainLayout
    :show-info-panel="showInfoPanel"
    @toggle-info-panel="toggleInfoPanel"
  >
    <!-- 顶部固定头部 -->
    <template #header>
      <AppHeader
        :current-page="currentPage"
        @page-change="handlePageChange"
      />
    </template>

    <!-- 左侧导航栏 -->
    <template #sidebar>
      <CadSidebar @show-model-viewer="handleShowModelViewer" />
    </template>

    <!-- 右侧内容区域 -->
    <template #content>
      <KeepAlive :include="['ModelGalleryPage']">
        <component
          :is="currentComponent"
          v-bind="currentComponentProps"
          @show-model-viewer="handleShowModelViewer"
          @show-shell-analysis-result="handleShowShellAnalysisResult"
          @show-hierarchy-analysis-result="handleShowHierarchyAnalysisResult"
          @show-geometry-complexity-result="handleShowGeometryComplexityResult"
          @show-geometry-optimization-params="handleShowGeometryOptimizationParams"
          @show-hierarchy-deletion-params="handleShowHierarchyDeletionParams"
          @close="handleCloseHierarchyDeletionParams"
          @back-to-analysis="handleBackToAnalysis"
          @start-optimization="handleGeometryOptimizationComplete"
          @page-change="handlePageChange"
        />
      </KeepAlive>
    </template>
  </MainLayout>

  <!-- 右侧信息管理面板 -->
  <InfoManagementPanel
    :is-visible="showInfoPanel"
    @close="closeInfoPanel"
  />
</template>

<script setup>
import { ref, computed } from 'vue'
import { useCADStore } from '@/stores/cad'
import MainLayout from '@/components/layout/MainLayout.vue'
import AppHeader from '@/components/layout/AppHeader.vue'
import CadSidebar from '@/components/layout/CadSidebar.vue'
import WelcomePage from '@/components/pages/WelcomePage.vue'
import ConnectionPage from '@/components/pages/ConnectionPage.vue'
import SettingsPage from '@/components/pages/SettingsPage.vue'
import MonitoringPage from '@/components/pages/MonitoringPage.vue'
import ModelViewerPage from '@/components/pages/ModelViewerPage.vue'
import AnalysisToolsPage from '@/components/pages/AnalysisToolsPage.vue'
import ExportToolsPage from '@/components/pages/ExportToolsPage.vue'
import UniversalConverter from '@/components/pages/UniversalConverter.vue'
import ModelGalleryPage from '@/components/pages/ModelGalleryPage.vue'
import ShellAnalysisResult from '@/components/pages/ShellAnalysisResult.vue'
import HierarchyAnalysisResult from '@/components/pages/HierarchyAnalysisResult.vue'
import HierarchyDeletionParamsPage from '@/components/pages/HierarchyDeletionParamsPage.vue'
import GeometryComplexityResult from '@/components/pages/GeometryComplexityResult.vue'
import GeometryOptimizationParams from '@/components/pages/GeometryOptimizationParams.vue'
import GeometryOptimizationResult from '@/components/pages/GeometryOptimizationResult.vue'
import InfoManagementPanel from '@/components/layout/InfoManagementPanel.vue'
import { PAGE_TYPES } from '@/config/pages'

const cadStore = useCADStore()

// 当前页面状态
const currentPage = ref(PAGE_TYPES.HOME)

// 计算当前连接的软件
const currentConnectedSoftware = computed(() => {
  return cadStore.currentCAD?.id || null
})

// 模型查看器状态
const showModelViewer = ref(false)
const currentModelData = ref(null)

// 分析结果数据存储
const shellAnalysisData = ref(null)
const hierarchyAnalysisData = ref(null)
const geometryComplexityData = ref(null)
const geometryOptimizationResultData = ref(null)

// 信息面板控制
const showInfoPanel = ref(false)

// 页面组件映射表
const pageComponentMap = {
  'connection': ConnectionPage,
  'settings': SettingsPage,
  'monitoring': MonitoringPage,
  [PAGE_TYPES.HOME]: WelcomePage,
  [PAGE_TYPES.MODEL_VIEWER]: ModelViewerPage,
  [PAGE_TYPES.ANALYSIS_TOOLS]: AnalysisToolsPage,
  [PAGE_TYPES.EXPORT_TOOLS]: ExportToolsPage,
  [PAGE_TYPES.FORMAT_CONVERTER]: UniversalConverter,
  [PAGE_TYPES.MODEL_GALLERY]: ModelGalleryPage,
  [PAGE_TYPES.SHELL_ANALYSIS_RESULT]: ShellAnalysisResult,
  [PAGE_TYPES.HIERARCHY_ANALYSIS_RESULT]: HierarchyAnalysisResult,
  [PAGE_TYPES.HIERARCHY_DELETION_PARAMS]: HierarchyDeletionParamsPage,
  [PAGE_TYPES.GEOMETRY_COMPLEXITY_RESULT]: GeometryComplexityResult,
  [PAGE_TYPES.GEOMETRY_OPTIMIZATION_PARAMS]: GeometryOptimizationParams,
  [PAGE_TYPES.GEOMETRY_OPTIMIZATION_RESULT]: GeometryOptimizationResult
}

// 当前组件computed
const currentComponent = computed(() => {
  return pageComponentMap[currentPage.value] || WelcomePage
})

// 当前组件props computed
const currentComponentProps = computed(() => {
  const props = {}

  // 根据不同页面传递不同的props
  if (currentPage.value === PAGE_TYPES.MODEL_VIEWER) {
    props.modelData = currentModelData.value
    props.currentConnectedSoftware = currentConnectedSoftware.value
  } else if (currentPage.value === PAGE_TYPES.ANALYSIS_TOOLS) {
    props.currentConnectedSoftware = currentConnectedSoftware.value
    props.modelData = currentModelData.value
  } else if (currentPage.value === PAGE_TYPES.EXPORT_TOOLS) {
    props.currentConnectedSoftware = currentConnectedSoftware.value
  } else if (currentPage.value === PAGE_TYPES.SHELL_ANALYSIS_RESULT) {
    props.analysisData = shellAnalysisData.value
  } else if (currentPage.value === PAGE_TYPES.HIERARCHY_ANALYSIS_RESULT) {
    props.analysisData = hierarchyAnalysisData.value
  } else if (currentPage.value === PAGE_TYPES.HIERARCHY_DELETION_PARAMS) {
    props.hierarchyData = hierarchyAnalysisData.value
  } else if (currentPage.value === PAGE_TYPES.GEOMETRY_COMPLEXITY_RESULT) {
    props.analysisData = geometryComplexityData.value
  } else if (currentPage.value === PAGE_TYPES.GEOMETRY_OPTIMIZATION_RESULT) {
    props.resultData = geometryOptimizationResultData.value
  }

  return props
})

// 处理显示模型查看器
const handleShowModelViewer = (modelData) => {
  currentModelData.value = modelData
  showModelViewer.value = true
  currentPage.value = PAGE_TYPES.MODEL_VIEWER
}

// 处理页面切换
const handlePageChange = (page) => {
  currentPage.value = page
}

// 处理跳转到分析结果页面
const handleShowShellAnalysisResult = (analysisData) => {
  shellAnalysisData.value = analysisData
  currentPage.value = PAGE_TYPES.SHELL_ANALYSIS_RESULT
}

// 处理跳转到层级分析结果页面
const handleShowHierarchyAnalysisResult = (analysisData) => {
  hierarchyAnalysisData.value = analysisData
  currentPage.value = PAGE_TYPES.HIERARCHY_ANALYSIS_RESULT
}

// 处理跳转到几何复杂度分析结果页面
const handleShowGeometryComplexityResult = (data) => {
  geometryComplexityData.value = data
  currentPage.value = PAGE_TYPES.GEOMETRY_COMPLEXITY_RESULT
}

// 处理跳转到几何优化分析参数设置页面
const handleShowGeometryOptimizationParams = () => {
  currentPage.value = PAGE_TYPES.GEOMETRY_OPTIMIZATION_PARAMS
}

// 处理几何优化分析完成
const handleGeometryOptimizationComplete = (data) => {
  // 检查API调用是否成功
  if (data.result && data.result.success) {
    geometryOptimizationResultData.value = data.result
    currentPage.value = PAGE_TYPES.GEOMETRY_OPTIMIZATION_RESULT
  }
}

// 处理跳转到层级删除参数设置页面
const handleShowHierarchyDeletionParams = () => {
  currentPage.value = PAGE_TYPES.HIERARCHY_DELETION_PARAMS
}

// 处理关闭层级删除参数设置页面
const handleCloseHierarchyDeletionParams = () => {
  currentPage.value = PAGE_TYPES.HIERARCHY_ANALYSIS_RESULT
}

// 处理返回分析页面
const handleBackToAnalysis = () => {
  currentPage.value = PAGE_TYPES.ANALYSIS_TOOLS
}

// 信息面板控制方法
const toggleInfoPanel = () => {
  showInfoPanel.value = !showInfoPanel.value
}

const closeInfoPanel = () => {
  showInfoPanel.value = false
}
</script>

<style scoped>
/* 不需要额外样式，所有样式都在各个页面组件中 */
</style>
