const transport = require('../../utils/ble-transport')
const syncService = require('../../utils/sync-service')
const themeService = require('../../utils/theme-service')
const {
  DEFAULT_DEVICE_FILTER,
  getHomePageState
} = require('../../utils/home-view-model')
const {
  createPageToast
} = require('../../utils/page-toast')

Page({
  data: getHomePageState(),

  onLoad() {
    this.pageToast = createPageToast(this, this.data)
    transport.init()
    themeService.init()
    this.unsubscribeTransport = transport.subscribe((transportState) => {
      const nextState = getHomePageState(transportState, this.data.deviceFilterMode, syncService.getState())

      this.setData(nextState)
      this.pageToast.showFromState(nextState)
    })
    this.unsubscribeSync = syncService.subscribe((syncState) => {
      this.setData(getHomePageState(transport.getState(), this.data.deviceFilterMode, syncState))
    })
    this.unsubscribeTheme = themeService.subscribe((themeState) => {
      this.setData(themeState)
    })
  },

  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.unsubscribeTransport) {
      this.unsubscribeTransport()
      this.unsubscribeTransport = null
    }

    if (this.unsubscribeSync) {
      this.unsubscribeSync()
      this.unsubscribeSync = null
    }

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

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

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

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

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

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

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

    transport.sendGeneratedFrame()
  },

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

  clearInput() {
    transport.clearInput()
  },

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

    transport.sendHexFrame()
  },

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

    if (this.data.isDiscovering) {
      transport.stopScan()
      return
    }

    transport.startScan()
  },

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

    syncService.syncAllRegisters()
  },

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

    transport.clearDevices()
  },

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

    this.setData(getHomePageState(transport.getState(), deviceFilterMode))
  },

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

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

    transport.disconnectDevice()
  },

  clearLogs() {
    transport.clearLogs()
  }
})
