const settingsService = require('../../utils/settings-service')
const themeService = require('../../utils/theme-service')
const toolNavigation = require('../../utils/tool-navigation')
const {
  createPageToast
} = require('../../utils/page-toast')
const {
  createToolInitialState,
  toolPageHandlers
} = require('../../utils/tool-page')
const {
  getSettingsPageState
} = require('../../utils/settings-view-model')

Page({
  data: {
    ...getSettingsPageState(),
    ...createToolInitialState(),
    activeSettingsTitle: '',
    activeSettingsView: ''
  },

  onLoad() {
    this.pageToast = createPageToast(this, this.data)
    this.crcFileBytes = null
    settingsService.init()
    themeService.init()

    this.unsubscribeSettings = settingsService.subscribe((settingsState) => {
      this.setData(getSettingsPageState(settingsState, themeService.getState()))
    })
    this.unsubscribeTheme = themeService.subscribe((themeState) => {
      this.setData(getSettingsPageState(settingsService.getState(), themeState))
    })
  },

  onTabItemTap() {
    this.backToSettingsHome()
  },

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

    this.setData(getSettingsPageState())
  },

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

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

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

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

  onNightModeEnabledChange(event) {
    settingsService.setNightModeEnabled(!!event.detail.value)
  },

  onNightModeFollowSystemChange(event) {
    settingsService.setNightModeFollowSystem(!!event.detail.value)
  },

  onModbusSlaveAddressBlur(event) {
    settingsService.setModbusSlaveAddress(event.detail.value)
  },

  onModbusProtocolChange(event) {
    const option = this.data.modbusProtocolOptions[Number(event.detail.value)]
    if (!option) return

    settingsService.setModbusProtocolFilter(option.key)
  },

  onGenericModbusAutoPollChange(event) {
    settingsService.setGenericModbusAutoPollEnabled(!!event.detail.value)
  },

  onGenericModbusPollIntervalBlur(event) {
    settingsService.setGenericModbusPollInterval(event.detail.value)
  },

  onGenericModbusMaxPacketLengthBlur(event) {
    settingsService.setGenericModbusMaxPacketLength(event.detail.value)
  },

  onUserStatusCountBlur(event) {
    settingsService.setUserStatusCount(event.detail.value, this.data.maxUserStatusCount)
  },

  onStatusPollIntervalBlur(event) {
    settingsService.setStatusPollInterval(event.detail.value)
  },

  openToolEntry(event) {
    const view = event.currentTarget.dataset.view
    if (!toolNavigation.isToolView(view)) return

    if (this.pageToast) this.pageToast.clear()
    this.setData({
      activeSettingsTitle: toolNavigation.getToolTitle(view),
      activeSettingsView: view
    })
  },

  backToSettingsHome() {
    if (this.pageToast) this.pageToast.clear()
    this.setData({
      activeSettingsTitle: '',
      activeSettingsView: ''
    })
  },

  ...toolPageHandlers
})
