const {
  DEFAULT_DEVICE_FILTER,
  getState: getHomeFeatureState,
  init: initHomeFeature,
  subscribeState: subscribeHomeState,
  ...homeService
} = require('../../features/home/service.js')
const {
  createPageToast
} = require('../../utils/page-toast.js')

Page({
  data: getHomeFeatureState(),

  noop() {},

  onLoad() {
    this.pageToast = createPageToast(this, this.data)
    initHomeFeature()
    this.unsubscribeHomeState = subscribeHomeState(() => this.data.deviceFilterMode, (nextState) => {
      this.setData(nextState)
      this.pageToast.showFromState(nextState)
    })
  },

  onShow() {
    if (this.pageToast) {
      this.pageToast.setActive(true)
    }
  },

  onHide() {
    if (this.pageToast) {
      this.pageToast.setActive(false)
    }
  },

  onUnload() {
    if (this.pageToast) {
      this.pageToast.destroy()
      this.pageToast = null
    }

    if (this.unsubscribeHomeState) {
      this.unsubscribeHomeState()
      this.unsubscribeHomeState = null
    }
  },

  onCommandChange(event) {
    homeService.setCommandIndex(event.detail.value)
  },

  onSlaveAddressInput(event) {
    homeService.setProtocolInput({
      slaveAddress: event.detail.value
    })
  },

  onRegisterAddressInput(event) {
    homeService.setProtocolInput({
      registerAddress: event.detail.value
    })
  },

  onCommandValueInput(event) {
    homeService.setProtocolInput({
      commandValue: event.detail.value
    })
  },

  onCommandRegisterQuantityInput(event) {
    homeService.setProtocolMultipleQuantity(event.detail.value)
  },

  openProtocolMultipleDialog() {
    homeService.openProtocolMultipleDialog()
  },

  closeProtocolMultipleDialog() {
    homeService.closeProtocolMultipleDialog()
  },

  onProtocolMultipleTypeChange(event) {
    homeService.setProtocolMultipleType(
      event.currentTarget.dataset.index,
      event.detail.value
    )
  },

  onProtocolMultipleTextLengthInput(event) {
    homeService.setProtocolMultipleTextLength(
      event.currentTarget.dataset.index,
      event.detail.value
    )
  },

  onProtocolMultipleValueInput(event) {
    homeService.setProtocolMultipleValue(
      event.currentTarget.dataset.index,
      event.detail.value
    )
  },

  onProtocolMultipleValueBlur(event) {
    try {
      homeService.validateProtocolMultipleValue(
        event.currentTarget.dataset.index,
        event.detail.value
      )
    } catch (error) {
      if (this.pageToast) this.pageToast.show(error.message || '输入值无效', 'error')
    }
  },

  onCoilValueChange(event) {
    homeService.setProtocolInput({
      coilEnabled: !!event.detail.value
    })
  },

  sendGeneratedFrame() {
    if (!this.data.connectedDevice || !this.data.generatedHex) return

    homeService.sendGeneratedFrame()
  },

  onHexInput(event) {
    homeService.setSendHex(event.detail.value)
  },

  clearInput() {
    homeService.clearInput()
  },

  sendHexFrame() {
    if (!this.data.connectedDevice) return

    homeService.sendHexFrame()
  },

  startScan() {
    if (!this.data.canStartScan) return

    homeService.toggleScan(this.data.isDiscovering)
  },

  syncRegisters() {
    if (!this.data.canSyncRegisters) return

    homeService.syncRegisters()
  },

  clearDevices() {
    if (!this.data.canClearDevices) return

    homeService.clearDevices()
  },

  onDeviceFilterTap(event) {
    const deviceFilterMode = event.currentTarget.dataset.filter || DEFAULT_DEVICE_FILTER

    this.setData(getHomeFeatureState(deviceFilterMode))
  },

  connectDevice(event) {
    homeService.connectDeviceById(event.currentTarget.dataset.deviceId)
  },

  disconnectDevice() {
    if (!this.data.canDisconnectDevice) return

    homeService.disconnectDevice()
  },

  clearLogs() {
    homeService.clearLogs()
  }
})
