import { ref } from 'vue'

export type CollectorOnlineStatus = '在线' | '离线'
export type CollectorTestStatus = '待检测' | '测试中' | '空闲'
export type CollectorStatus = '启用' | '禁用'

export interface DeviceCollectorRecord {
  id: number
  deviceCode: string
  ip: string
  mac: string
  name: string
  manufacturer: string
  model: string
  channelCount: number
  remark: string
  onlineStatus: CollectorOnlineStatus
  testStatus: CollectorTestStatus
  currentTest: string
  status: CollectorStatus
  createdAt: string
}

export type CollectorPayload = Pick<DeviceCollectorRecord, 'deviceCode' | 'ip' | 'mac' | 'name' | 'manufacturer' | 'model' | 'channelCount' | 'remark' | 'onlineStatus' | 'testStatus' | 'currentTest' | 'status'>

export const discoveredCollectors: CollectorPayload[] = [
  { deviceCode: '45643176', ip: '192.1.1.162', mac: '41:F5:D7:A1:00:01', name: '', manufacturer: '', model: '', channelCount: 1, remark: '', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用' },
  { deviceCode: '45643177', ip: '192.1.1.163', mac: '41:F5:D7:A2:00:02', name: '', manufacturer: '', model: '', channelCount: 1, remark: '', onlineStatus: '在线', testStatus: '待检测', currentTest: '-', status: '启用' },
  { deviceCode: '45643178', ip: '192.1.1.164', mac: '41:F5:D7:A3:00:03', name: '', manufacturer: '', model: '', channelCount: 1, remark: '', onlineStatus: '离线', testStatus: '空闲', currentTest: '-', status: '启用' },
  { deviceCode: '45643179', ip: '192.1.1.165', mac: '41:F5:D7:A4:00:04', name: '', manufacturer: '', model: '', channelCount: 1, remark: '', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用' },
  { deviceCode: '45643180', ip: '192.1.1.166', mac: '41:F5:D7:A5:00:05', name: '', manufacturer: '', model: '', channelCount: 1, remark: '', onlineStatus: '在线', testStatus: '待检测', currentTest: '-', status: '启用' },
  { deviceCode: '45643181', ip: '192.1.1.167', mac: '41:F5:D7:A6:00:06', name: '', manufacturer: '', model: '', channelCount: 1, remark: '', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用' },
]

const STORAGE_KEY = 'battery_detection_device_collectors_v1'

const seedRecords: DeviceCollectorRecord[] = [
  { id: 4012, deviceCode: '45643166', ip: '192.1.1.152', mac: '41:F5:D6:N1', name: '采集器1', manufacturer: 'XXX', model: 'A1', channelCount: 12, remark: '一号实验区', onlineStatus: '在线', testStatus: '待检测', currentTest: 'SP2电池寿命循环', status: '启用', createdAt: '2026-08-05 09:26' },
  { id: 4011, deviceCode: '45643167', ip: '192.1.1.153', mac: '41:F5:D6:N2', name: '采集器2', manufacturer: 'XXX', model: 'A1', channelCount: 12, remark: '一号实验区', onlineStatus: '在线', testStatus: '测试中', currentTest: '高温循环性能测试', status: '启用', createdAt: '2026-08-04 16:18' },
  { id: 4010, deviceCode: '45643168', ip: '192.1.1.154', mac: '41:F5:D6:N3', name: '采集器3', manufacturer: 'XXX', model: 'A2', channelCount: 24, remark: '二号实验区', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用', createdAt: '2026-08-04 10:42' },
  { id: 4009, deviceCode: '45643169', ip: '192.1.1.155', mac: '41:F5:D6:N4', name: '采集器4', manufacturer: 'XXX', model: 'A2', channelCount: 24, remark: '二号实验区', onlineStatus: '离线', testStatus: '空闲', currentTest: '-', status: '禁用', createdAt: '2026-08-03 15:06' },
  { id: 4008, deviceCode: '45643170', ip: '192.1.1.156', mac: '41:F5:D6:N5', name: '采集器5', manufacturer: 'YYY', model: 'B1', channelCount: 8, remark: '三号实验区', onlineStatus: '在线', testStatus: '待检测', currentTest: '-', status: '启用', createdAt: '2026-08-02 11:35' },
  { id: 4007, deviceCode: '45643171', ip: '192.1.1.157', mac: '41:F5:D6:N6', name: '采集器6', manufacturer: 'YYY', model: 'B1', channelCount: 8, remark: '三号实验区', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用', createdAt: '2026-08-01 09:18' },
  { id: 4006, deviceCode: '45643172', ip: '192.1.1.158', mac: '41:F5:D6:N7', name: '采集器7', manufacturer: 'YYY', model: 'B2', channelCount: 16, remark: '四号实验区', onlineStatus: '离线', testStatus: '待检测', currentTest: '-', status: '禁用', createdAt: '2026-07-30 14:20' },
  { id: 4005, deviceCode: '45643173', ip: '192.1.1.159', mac: '41:F5:D6:N8', name: '采集器8', manufacturer: 'YYY', model: 'B2', channelCount: 16, remark: '四号实验区', onlineStatus: '在线', testStatus: '测试中', currentTest: '低温充放电测试', status: '启用', createdAt: '2026-07-29 17:12' },
  { id: 4004, deviceCode: '45643174', ip: '192.1.1.160', mac: '41:F5:D6:N9', name: '采集器9', manufacturer: 'ZZZ', model: 'C1', channelCount: 32, remark: '五号实验区', onlineStatus: '在线', testStatus: '空闲', currentTest: '-', status: '启用', createdAt: '2026-07-28 13:46' },
  { id: 4003, deviceCode: '45643175', ip: '192.1.1.161', mac: '41:F5:D6:N0', name: '采集器10', manufacturer: 'ZZZ', model: 'C1', channelCount: 32, remark: '五号实验区', onlineStatus: '离线', testStatus: '空闲', currentTest: '-', status: '禁用', createdAt: '2026-07-26 10:16' },
]

function loadRecords() {
  try {
    const stored = localStorage.getItem(STORAGE_KEY)
    if (stored) return JSON.parse(stored) as DeviceCollectorRecord[]
  } catch {
    localStorage.removeItem(STORAGE_KEY)
  }
  return seedRecords
}

const records = ref<DeviceCollectorRecord[]>(loadRecords())

function persist() {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(records.value))
}

function nowString() {
  const date = new Date()
  const pad = (value: number) => String(value).padStart(2, '0')
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`
}

export function useDeviceCollectors() {
  function createCollector(data: CollectorPayload) {
    const id = Math.max(4000, ...records.value.map(item => item.id)) + 1
    records.value.unshift({ ...data, id, createdAt: nowString() })
    persist()
  }

  function updateCollector(id: number, data: CollectorPayload) {
    const target = records.value.find(item => item.id === id)
    if (!target) return false
    Object.assign(target, data)
    persist()
    return true
  }

  function removeCollector(id: number) {
    records.value = records.value.filter(item => item.id !== id)
    persist()
  }

  function saveChanges() {
    persist()
  }

  return { records, createCollector, updateCollector, removeCollector, saveChanges }
}


