import type { MockMethod, MockRequest } from './types'

// 模拟采集地址数据
const mockAddresses = [
  {
    dbId: 1,
    name: 'FANUC-1号',
    url: 'http://localhost:9001/',
    machineCount: 32,
    machines: Array.from({ length: 32 }, (_, i) => ({
      id: i + 1,
      deviceCode: `fanake_1.${i + 2}`,
      name: `西-1.${i + 2}`
    })),
    isRunning: true,
    runningPort: 9001
  },
  {
    dbId: 2,
    name: 'FANUC-2号',
    url: 'http://localhost:9002/',
    machineCount: 16,
    machines: Array.from({ length: 16 }, (_, i) => ({
      id: i + 33,
      deviceCode: `fanake_2.${i + 1}`,
      name: `东-2.${i + 1}`
    })),
    isRunning: false,
    runningPort: 0
  }
]

// 模拟状态汇总
const mockStatusList = [
  {
    dbAddressId: 1,
    name: 'FANUC-1号模拟',
    port: 9001,
    isRunning: true,
    totalDevices: 32,
    onlineDevices: 28,
    requestCount: 1560,
    dataChangeInterval: 10,
    totalParts: 128
  }
]

// 模拟设备状态
const mockDevices = [
  { deviceCode: 'fanake_1.2', desc: '西-1.2', scenario: 'machining', isOnline: true, programName: 'O504', partCount: 14, runStatus: 3, operateMode: 10, spindleSpeedSet: 3000, spindleSpeedActual: 2980, feedSpeedSet: 500, feedSpeedActual: 490, spindleLoad: 65, machiningStatus: 'cutting', scenarioTick: 45, scenarioDuration: 120 },
  { deviceCode: 'fanake_1.3', desc: '西-1.3', scenario: 'idle', isOnline: true, programName: 'O1', partCount: 53, runStatus: 1, operateMode: 10, spindleSpeedSet: 0, spindleSpeedActual: 0, feedSpeedSet: 0, feedSpeedActual: 0, spindleLoad: 5, machiningStatus: 'idle', scenarioTick: 12, scenarioDuration: 60 },
  { deviceCode: 'fanake_1.4', desc: '西-1.4', scenario: 'offline', isOnline: false, programName: 'O200', partCount: 0, runStatus: 0, operateMode: 0, spindleSpeedSet: 0, spindleSpeedActual: 0, feedSpeedSet: 0, feedSpeedActual: 0, spindleLoad: 0, machiningStatus: 'offline', scenarioTick: 0, scenarioDuration: 0 }
]

// 模拟请求日志
const mockLogs = Array.from({ length: 10 }, (_, i) => ({
  index: 10 - i,
  timestamp: `${String(14 + Math.floor(i / 6)).padStart(2, '0')}:${String(30 - i * 2).padStart(2, '0')}:${String(15 + i).padStart(2, '0')}`,
  deviceCount: 28 + Math.floor(Math.random() * 5),
  keyData: `fanake_1.2(P=14,Prog=O504,Run=3) fanake_1.3(P=53,Prog=O1,Run=1)`,
  duration: 12 + Math.floor(Math.random() * 20),
  fullJson: `[{"device":"fanake_1.2","desc":"西-1.2","tags":[{"id":"Tag5","value":"O504"}]}]`
}))

const mocks: MockMethod[] = [
  // 探测模拟器
  { url: '/api/admin/simulator/ping', method: 'get', response: () => ({ code: 0, message: 'success', data: { running: true } }) },

  // 获取采集地址列表
  { url: '/api/admin/simulator/addresses', method: 'get', response: () => ({ code: 0, message: 'success', data: mockAddresses }) },

  // 获取模拟状态汇总
  { url: '/api/admin/simulator/status', method: 'get', response: () => ({ code: 0, message: 'success', data: mockStatusList }) },

  // 启动模拟
  { url: '/api/admin/simulator/start', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true, port: 9001 } }) },

  // 停止模拟
  { url: '/api/admin/simulator/stop', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },

  // 全部启动
  { url: '/api/admin/simulator/start-all', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },

  // 全部停止
  { url: '/api/admin/simulator/stop-all', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },

  // 重新加载
  { url: '/api/admin/simulator/reload', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true, count: 2 } }) },

  // 单地址状态（参数化路由 :port 匹配动态端口）
  { url: '/api/admin/simulator/address/:port/status', method: 'get', response: () => ({
    code: 0, message: 'success', data: {
      name: 'FANUC-1号模拟', port: 9001, isRunning: true,
      requestCount: 1560, successCount: 1540, failCount: 20,
      totalDevices: 32, onlineDevices: 28, dataChangeInterval: 10,
      scenarioMode: 'auto', networkError: 'normal',
      startTime: '2026-05-06 10:00:00', uptime: '04:32:15',
      devices: mockDevices
    }
  })},

  // 单地址启动/停止/事件/设置（POST类，统返回ok）
  { url: '/api/admin/simulator/address/:port/start', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/stop', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/event', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/interval', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/network', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/mode', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/add-device', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },
  { url: '/api/admin/simulator/address/:port/remove-device', method: 'post', response: () => ({ code: 0, message: 'success', data: { ok: true } }) },

  // 日志
  { url: '/api/admin/simulator/address/:port/logs', method: 'get', response: () => ({ code: 0, message: 'success', data: mockLogs }) },

  // 统计
  { url: '/api/admin/simulator/address/:port/stats', method: 'get', response: () => ({
    code: 0, message: 'success', data: {
      totalDevices: 32, onlineDevices: 28, totalParts: 128,
      partsByDevice: {
        'fanake_1.2': { desc: '西-1.2', totalParts: 14, currentProgram: 'O504', currentPartCount: 14, programs: { 'O504': 14 } },
        'fanake_1.3': { desc: '西-1.3', totalParts: 53, currentProgram: 'O1', currentPartCount: 53, programs: { 'O1': 53 } }
      }
    }
  })},

  // 事件历史
  { url: '/api/admin/simulator/address/:port/event-history', method: 'get', response: () => ({ code: 0, message: 'success', data: [
    { timestamp: '2026-05-06 14:30:00', deviceCode: 'fanake_1.2', eventType: 'change_program', oldProgram: 'O200', newProgram: 'O504', partCountBefore: 10, partCountAfter: 14, detail: '程序切换' },
    { timestamp: '2026-05-06 14:25:00', deviceCode: 'fanake_1.3', eventType: 'part_count_increase', oldProgram: 'O1', newProgram: 'O1', partCountBefore: 52, partCountAfter: 53, detail: '零件数+1' }
  ] })},

  // 完整汇总
  { url: '/api/admin/simulator/address/:port/full-summary', method: 'get', response: () => ({ code: 0, message: 'success', data: { exportTime: '2026-05-06 14:35:00', addressName: 'FANUC-1号模拟', port: 9001, totalDevices: 32, onlineDevices: 28, totalParts: 128 } }) },

  // 异常日志
  { url: '/api/admin/simulator/address/:port/error-log', method: 'get', response: () => ({ code: 0, message: 'success', data: [] }) },
]

export default mocks
