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

class PdmsApiService {
  constructor() {
    this.softwareName = 'pdms'
  }

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

    return await apiClient.get(url, {
      operationContext: {
        software: 'PDMS',
        operation: '连接测试'
      }
    })
  }

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

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

  /**
   * 执行PDMS模型轻量化处理
   * @param {Object} params 轻量化参数
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async executeShrinkwrap(params) {
    const url = buildApiUrl(this.softwareName, 'shrinkwrap')

    return await apiClient.post(url, params, {
      timeout: 600000, // 超时600s
      operationContext: {
        software: 'PDMS',
        operation: '模型轻量化处理'
      }
    })
  }

  /**
   * 执行PDMS模型简化处理
   * @param {Object} params 简化参数
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async executeSimplify(params) {
    const url = buildApiUrl(this.softwareName, 'simplify')

    return await apiClient.post(url, params, {
      timeout: 660000, // 超时660s
      operationContext: {
        software: 'PDMS',
        operation: '模型简化处理'
      }
    })
  }

  /**
   * 打开 PDMS 项目
   * @param {Object} params - 项目参数 { ProjectName, UserName, Password }
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async openProject(params) {
    const url = buildApiUrl(this.softwareName, 'openProject')
    
    // 如果只传了字符串，默认为 ProjectName
    const payload = typeof params === 'string'
      ? { ProjectName: params, UserName: 'SYSTEM', Password: '' }
      : {
          ProjectName: params.ProjectName,
          UserName: params.UserName || 'SYSTEM',
          Password: params.Password || ''
        }
        
    return await apiClient.post(url, payload, {
      operationContext: {
        software: 'PDMS',
        operation: '打开项目'
      }
    })
  }

  /**
   * 打开 PDMS MDB
   * @param {Object} params - MDB参数
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async openMdb(params) {
    const url = buildApiUrl(this.softwareName, 'openMdb')
    
    // 如果只传了字符串，默认为 MdbName
    const payload = typeof params === 'string'
      ? {
          MdbName: params,
          ReadOnly: false,
          DefaultType: 'Design',
          ReadTypes: ['Design', 'Catalog'],
          WriteTypes: ['Design'],
          Stamp: null,
          Subtype: null
        }
      : {
          MdbName: params.MdbName,
          ReadOnly: params.ReadOnly ?? false,
          DefaultType: params.DefaultType || 'Design',
          ReadTypes: params.ReadTypes || ['Design', 'Catalog'],
          WriteTypes: params.WriteTypes || ['Design'],
          Stamp: params.Stamp || null,
          Subtype: params.Subtype || null
        }

    return await apiClient.post(url, payload, {
      operationContext: {
        software: 'PDMS',
        operation: '打开MDB数据库'
      }
    })
  }

  /**
   * 导出为RVM格式(与后台exportIfc接口对应)
   * @param {Object} params - 导出参数 { ExportPath, FileName }
   * @returns {Promise<{success: boolean, data?: any, error?: string}>}
   */
  async exportIfc(params) {
    const url = buildApiUrl(this.softwareName, 'exportIfc')
    
    return await apiClient.post(url, params || {}, {
      timeout: 600000, 
      operationContext: {
        software: 'PDMS',
        operation: '模型导出'
      }
    })
  }
}

export const pdmsApi = new PdmsApiService()

export default pdmsApi