const controlService = require('../../utils/control-service')
const themeService = require('../../utils/theme-service')
const {
  getControlPageState
} = require('../../utils/control-view-model')
const {
  createPageToast
} = require('../../utils/page-toast')

Page({
  data: getControlPageState(),

  onLoad() {
    this.pageToast = createPageToast(this, this.data)
    controlService.init()
    themeService.init()
    this.unsubscribeControl = controlService.subscribe((controlState) => {
      const nextState = getControlPageState(controlState)

      this.setData(nextState)
      this.pageToast.showFromState(nextState)
    })
    this.unsubscribeTheme = themeService.subscribe((themeState) => {
      this.setData(getControlPageState(controlService.getState(), themeState))
    })
  },

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

    controlService.syncSharedInputs()
    this.setData(getControlPageState())
  },

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

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

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

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

  readStatus() {
    if (!this.data.canReadStatus) return

    controlService.readStatus()
  },

  onAutoReadStatusTap() {
    if (!this.data.autoReadStatus && !this.data.canReadStatus) return

    controlService.setAutoReadStatus(!this.data.autoReadStatus)
  },

  onSpeedCommandInput(event) {
    controlService.updateSpeedCommandInput(event.detail.value)
  },

  onSpeedCommandBlur(event) {
    controlService.updateSpeedCommandBlur(event.detail.value)
  },

  readControlStatus() {
    if (!this.data.connectedDevice || this.data.isBootloaderBusy) return

    controlService.readControlStatus()
  },

  onControlButtonTap(event) {
    if (!this.data.connectedDevice || this.data.isBootloaderBusy) return

    controlService.sendControlCommand(event.currentTarget.dataset.key)
  },

  chooseFirmwareFile() {
    if (this.data.isBootloaderBusy) return

    controlService.chooseFirmwareFile('message')
  },

  openFirmwareFile() {
    if (this.data.isBootloaderBusy) return

    controlService.chooseFirmwareFile('local')
  },

  startFirmwareUpgrade() {
    if (!this.data.connectedDevice || !this.data.isFirmwareReady || this.data.isBootloaderBusy) return

    controlService.startFirmwareUpgrade()
  },

  readProgramChecksum() {
    if (!this.data.connectedDevice || this.data.isBootloaderBusy) return

    controlService.readProgramChecksum()
  },

  handshakeBootloader() {
    if (!this.data.connectedDevice || this.data.isBootloaderBusy) return

    controlService.handshakeBootloader()
  },

  exitBootloader() {
    if (!this.data.connectedDevice || this.data.isBootloaderBusy) return

    controlService.exitBootloader()
  }
})
