// 网络配置相关API

// 模拟API响应数据 (开发环境使用)
const mockNetworkConfig = {
  ipMethod: 'dhcp',
  dhcpIp: '192.168.1.100',
  dhcpSubnetMask: '255.255.255.0',
  dhcpGateway: '192.168.1.1',
  staticIp: '192.168.1.100',
  staticSubnetMask: '255.255.255.0',
  staticGateway: '192.168.1.1',
  primaryDns: '8.8.8.8',
  secondaryDns: '8.8.4.4'
}

const mockNetworkStatus = {
  connected: true,
  macAddress: '00:11:22:33:44:55',
  interfaceName: 'eth0',
  speed: '100Mbps'
}

/**
 * 获取网络配置
 * @returns {Promise} 网络配置数据
 */
export const getNetworkConfig = async () => {
  try {
    const response = await fetch('/api/network/config', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    })
    
    if (!response.ok) {
      throw new Error('Network response was not ok')
    }
    
    return await response.json()
  } catch (error) {
    console.error('获取网络配置失败:', error)
    // 在开发环境返回模拟数据
    return {
      success: true,
      data: mockNetworkConfig,
      message: '使用模拟数据'
    }
  }
}

/**
 * 更新网络配置
 * @param {Object} config 网络配置数据
 * @returns {Promise} 更新结果
 */
export const updateNetworkConfig = async (config) => {
  try {
    const response = await fetch('/api/network/config', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(config)
    })
    
    if (!response.ok) {
      throw new Error('Network response was not ok')
    }
    
    return await response.json()
  } catch (error) {
    console.error('更新网络配置失败:', error)
    // 在开发环境返回模拟成功响应
    return {
      success: true,
      message: '网络配置已保存（模拟数据）'
    }
  }
}

/**
 * 获取网络状态
 * @returns {Promise} 网络状态数据
 */
export const getNetworkStatus = async () => {
  try {
    const response = await fetch('/api/network/status', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    })
    
    if (!response.ok) {
      throw new Error('Network response was not ok')
    }
    
    return await response.json()
  } catch (error) {
    console.error('获取网络状态失败:', error)
    // 在开发环境返回模拟数据
    return {
      success: true,
      data: mockNetworkStatus,
      message: '使用模拟数据'
    }
  }
}

/**
 * 重启网络服务
 * @returns {Promise} 重启结果
 */
export const restartNetworkService = async () => {
  try {
    const response = await fetch('/api/network/restart', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      }
    })
    
    if (!response.ok) {
      throw new Error('Network response was not ok')
    }
    
    return await response.json()
  } catch (error) {
    console.error('重启网络服务失败:', error)
    // 在开发环境返回模拟成功响应
    return {
      success: true,
      message: '网络服务已重启（模拟操作）'
    }
  }
}