const MAX_MODBUS_ADDRESS = 0xFFFF
const MAX_STORAGE_ADDRESS = 0xFFFFFFFF
const MAX_PARAMETER_GROUP_ITEMS = 256
const DEFAULT_TEXT_BYTE_LENGTH = 32
const MAX_TEXT_BYTE_LENGTH = 32

const REGISTER_TYPE_OPTIONS = [
  {
    functionCode: 0x03,
    key: 'holding',
    label: '保持寄存器',
    writable: true
  },
  {
    functionCode: 0x01,
    key: 'coil',
    label: '线圈',
    writable: true
  },
  {
    functionCode: 0x02,
    key: 'discrete',
    label: '离散输入状态',
    writable: false
  },
  {
    functionCode: 0x04,
    key: 'input',
    label: '输入寄存器',
    writable: false
  }
]

const DATA_TYPE_OPTIONS = [
  {
    byteLength: 1,
    key: 'int8_t',
    label: 'int8_t',
    kind: 'number',
    wordCount: 1
  },
  {
    byteLength: 1,
    key: 'uint8_t',
    label: 'uint8_t',
    kind: 'number',
    wordCount: 1
  },
  {
    byteLength: 2,
    key: 'int16_t',
    label: 'int16_t',
    kind: 'number',
    wordCount: 1
  },
  {
    byteLength: 2,
    key: 'uint16_t',
    label: 'uint16_t',
    kind: 'number',
    wordCount: 1
  },
  {
    byteLength: 4,
    key: 'int32_t',
    label: 'int32_t',
    kind: 'number',
    wordCount: 2
  },
  {
    byteLength: 4,
    key: 'uint32_t',
    label: 'uint32_t',
    kind: 'number',
    wordCount: 2
  },
  {
    byteLength: 4,
    key: 'float',
    label: 'float',
    kind: 'number',
    wordCount: 2
  },
  {
    byteLength: 32,
    key: 'utf8',
    label: 'UTF-8',
    kind: 'text',
    maxByteLength: MAX_TEXT_BYTE_LENGTH,
    wordCount: 16
  },
  {
    byteLength: 32,
    key: 'ascii',
    label: 'ASCII',
    kind: 'text',
    maxByteLength: MAX_TEXT_BYTE_LENGTH,
    wordCount: 16
  },
  {
    byteLength: 2,
    key: 'hex',
    label: 'HEX',
    kind: 'hex',
    wordCount: 1
  },
  {
    byteLength: 1,
    key: 'raw',
    label: '未配置',
    kind: 'raw',
    wordCount: 1
  }
]

const DEFAULT_REGISTER_TYPE = REGISTER_TYPE_OPTIONS[0].key
const DEFAULT_DATA_TYPE = 'uint16_t'
const GROUP_LAYOUT_REGISTER = 'register'
const GROUP_LAYOUT_STRUCT = 'struct'
const BYTE_ADDRESS_MEMORY_AREAS = ['ADDR32', 'BIT', 'CODE', 'DATA', 'IDATA', 'XDATA']

const SOURCE_REGISTER_FIELDS = [
  'conversionFormula',
  'enumName',
  'enumOptions',
  'sourceAddress',
  'sourceAddressByteLength',
  'sourceAddressText',
  'sourceAddressWidth',
  'sourceByteLength',
  'sourceBitOffset',
  'sourceBitWidth',
  'sourceEntryKind',
  'sourceMemoryArea',
  'sourceMemoryClass',
  'sourceSymbolName',
  'sourceSymbolType'
]

const STRUCT_REGISTER_FIELDS = [
  'bitOffset',
  'bitWidth',
  'byteStart',
  'isPlaceholderByteField',
  'isBitField',
  'structByteLength'
]

const SOURCE_GROUP_FIELDS = [
  'addressUnit',
  'codeInfoContext',
  'sourceAddress',
  'sourceAddressByteLength',
  'sourceAddressText',
  'sourceAddressWidth',
  'sourceByteLength',
  'sourceEntryKind',
  'sourceMemoryArea',
  'sourceMemoryClass',
  'sourceSegment',
  'sourceSegmentModule',
  'sourceSymbolName',
  'sourceSymbolType'
]

module.exports = {
  BYTE_ADDRESS_MEMORY_AREAS,
  DATA_TYPE_OPTIONS,
  DEFAULT_DATA_TYPE,
  DEFAULT_REGISTER_TYPE,
  DEFAULT_TEXT_BYTE_LENGTH,
  GROUP_LAYOUT_REGISTER,
  GROUP_LAYOUT_STRUCT,
  MAX_MODBUS_ADDRESS,
  MAX_STORAGE_ADDRESS,
  MAX_PARAMETER_GROUP_ITEMS,
  MAX_TEXT_BYTE_LENGTH,
  REGISTER_TYPE_OPTIONS,
  SOURCE_GROUP_FIELDS,
  SOURCE_REGISTER_FIELDS,
  STRUCT_REGISTER_FIELDS
}
