// Revit API服务 - 完全复制creo结构
// 严格遵循开发规范：核心需求优先，杜绝过度工程

import apiClient from './apiClient'
import { buildApiUrl } from '@/config/cad'

// 重复使用的常量
const SOFTWARE_TYPE = "revit"

// Revit API服务类
class RevitApiService {
  constructor() {
    this.softwareName = 'revit'
  }

  /**
   * 测试Revit连接
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async testConnection() {
    // 构建连接测试URL
    const url = buildApiUrl(this.softwareName, 'connect')

    // 调用统一API客户端（错误处理和通知由apiClient统一管理）
    return await apiClient.get(url, {
      operationContext: {
        software: 'Revit',
        operation: '连接测试'
      }
    })
  }

  /**
   * 获取当前模型总览
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async getCurrentModel() {
    const url = buildApiUrl(this.softwareName, 'overview')

    return await apiClient.get(url, {
      operationContext: {
        software: 'Revit',
        operation: '获取当前模型'
      }
    })
  }

  /**
   * 开始薄壳化分析
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async startShellAnalysis() {
    const url = buildApiUrl(this.softwareName, 'shellAnalysis')

    return await apiClient.post(url, {
      "software_type": SOFTWARE_TYPE,
      "mode": "Standard"
    }, {
      operationContext: {
        software: 'Revit',
        operation: '薄壳化分析'
      }
    })
  }

  /**
   * 导出IFC文件
   * @param {Object} exportOptions - 导出选项
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async exportIFC(exportOptions) {
    const url = buildApiUrl(this.softwareName, 'exportIfc')

    return await apiClient.post(url, exportOptions, {
      operationContext: {
        software: 'Revit',
        operation: 'IFC导出'
      }
    })
  }

  /**
   * 发起删除任务(保留轮廓)
   * @param {string} mode - Conservative | Standard | Aggressive | EnvelopeOnly
   * @param {boolean} backupOriginal - 是否备份原文件（true/false）
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async executeShellOptimization(mode = 'EnvelopeOnly', backupOriginal = true) {
    const url = buildApiUrl(this.softwareName, 'shellExecute')
    return await apiClient.post(url, {
      mode,
      backupOriginal
    }, {
      operationContext: {
        software: 'Revit',
        operation: '模型轻量化处理'
      }
    })
  }

  /**
   * 轮询任务状态
   * @param {string} taskId - 任务ID
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async getTaskStatus(taskId) {
    const baseUrl = buildApiUrl(this.softwareName, 'taskStatus')
    const url = `${baseUrl}/${taskId}`
    return await apiClient.get(url, {
      operationContext: {
        software: 'Revit',
        operation: '获取当前任务状态',
        silent: true
      }
    })
  }

  /**
   * 取消任务
   * @param {string} taskId - 任务ID
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async cancelTask(taskId) {
    const baseUrl = buildApiUrl(this.softwareName, 'taskStatus')
    const url = `${baseUrl}/${taskId}`
    return await apiClient.delete(url, null, {
      operationContext: {
        software: 'Revit',
        operation: '取消轻量化任务'
      }
    })
  }

  /**
   * 打开模型文件
   * @param {string} filePath - 模型文件绝对路径
   * @param {boolean} detached - 是否分离打开（默认false）
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async openModelFile(filePath, detached = false) {
    const url = buildApiUrl(this.softwareName, 'open')

    return await apiClient.post(url, {
      filePath,
      detached
    }, {
      operationContext: {
        software: 'Revit',
        operation: '打开模型文件'
      }
    })
  }

  /**
   * 自定义删除指定类别的构件
   * @param {number[]} categoryIds - 要删除的类别 ID 列表 (BuiltInCategory 的 int 值)
   * @param {boolean} backupOriginal - 是否备份原文件（默认 true）
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async executeCustomDelete(categoryIds, backupOriginal = true) {
    const url = buildApiUrl(this.softwareName, 'executeCustom')

    return await apiClient.post(url, {
      categoryIds,
      backupOriginal
    }, {
      operationContext: {
        software: 'Revit',
        operation: '自定义类别删除'
      }
    })
  }
}

// 导出单例实例
export const revitApi = new RevitApiService()

// 默认导出
export default revitApi