import { defineStore } from 'pinia'

export const useDataStore = defineStore('data', {
  state: () => ({
    // 串口相关状态
    availablePorts: [],
    serialConnected: false,
    serialConfig: {
      port: '',
      baudrate: 9600
    },
    serialSendData: '',
    serialData: [],
    
    // MQTT相关状态
    mqttConnected: false,
    mqttConfig: {
      broker: '',
      port: 1883,
      username: '',
      password: '',
      clientId: '',
      topics: 'serial_gateway/in'
    },
    mqttPublishTopic: 'serial_gateway/out',
    mqttSendData: '',
    mqttData: [],
    
    // 转发配置
    forwardConfig: {
      serialToMQTT: false,
      mqttToSerial: false
    },

    // DTU相关状态
    dtuConnected: false,
    dtuConfig: {
      topic_prefix: '线架系统',
      customer_id: '',
      dtu_id: '',
      firmware_version: '1.0.0',
      heartbeat_interval: 30,
      enabled: true
    },
    dtuStatus: {
      registered: false,
      last_heartbeat: null,
      online: false
    }
  }),
  
  actions: {
    // 更新串口数据
    updateSerialData(data) {
      this.serialData.push(data)
      if (this.serialData.length > 100) {
        this.serialData = this.serialData.slice(this.serialData.length - 100)
      }
    },
    
    // 更新MQTT数据
    updateMQTTData(data) {
      this.mqttData.push(data)
      if (this.mqttData.length > 100) {
        this.mqttData = this.mqttData.slice(this.mqttData.length - 100)
      }
    },
    
    // 设置串口连接状态
    setSerialConnected(status) {
      this.serialConnected = status
    },
    
    // 设置MQTT连接状态
    setMQTTConnected(status) {
      this.mqttConnected = status
    },
    
    // 设置可用串口列表
    setAvailablePorts(ports) {
      this.availablePorts = ports
    },
    
    // 清空数据
    clearData() {
      this.serialData = []
      this.mqttData = []
    },

    // 设置DTU连接状态
    setDTUConnected(status) {
      this.dtuConnected = status
    },

    // 设置DTU配置
    setDTUConfig(config) {
      this.dtuConfig = { ...this.dtuConfig, ...config }
    },

    // 设置DTU状态
    setDTUStatus(status) {
      this.dtuStatus = { ...this.dtuStatus, ...status }
    }
  }
})