const crcTool = require('../../tools/crc-hash/crc-tool.js')
const filterCalculator = require('../../tools/filter/index.js')
const smdCodeCalculator = require('../../tools/smd-code/index.js')
const refrigerationCalculator = require('../../tools/refrigeration/index.js')
const reactanceCalculator = require('../../tools/reactance/index.js')
const threePhasePowerCalculator = require('../../tools/three-phase-power/index.js')
const {
  getWxApi
} = require('../../utils/base-utils.js')

const crcHandlers = require('./handlers/crc.js')
const filterHandlers = require('./handlers/filter.js')
const reactanceHandlers = require('./handlers/reactance.js')
const refrigerationHandlers = require('./handlers/refrigeration.js')
const smdCodeHandlers = require('./handlers/smd-code.js')
const threePhasePowerHandlers = require('./handlers/three-phase-power.js')

const TOOL_ENTRIES = [
  { view: 'bootloader', label: 'BootLoader升级', icon: 'icon-chip', iconSrc: '/assets/icons/chip-white.png' },
  { view: 'crc', label: 'CRC与哈希计算', icon: 'icon-crc', iconSrc: '/assets/icons/hash-white.png' },
  { view: 'filter', label: '滤波器计算', icon: 'icon-filter', iconSrc: '/assets/icons/funnel-white.png' },
  { view: 'reactance', label: '电抗计算', icon: 'icon-reactance', iconSrc: '/assets/icons/audio-waveform-white.png' },
  { view: 'smdCode', label: '贴片电阻/容代码', icon: 'icon-smd', iconSrc: '/assets/icons/microchip-white.png' },
  { view: 'refrigeration', label: '制冷计算', icon: 'icon-snow', iconSrc: '/assets/icons/snowflake-white.png' },
  { view: 'threePhasePower', label: '三相功率计算', icon: 'icon-three-phase', iconSrc: '/assets/icons/zap-white.png' }
]

function getToolEntries() {
  return TOOL_ENTRIES.map((item) => ({ ...item }))
}

function isToolView(view) {
  return TOOL_ENTRIES.some((item) => item.view === view)
}

function getToolEntry(view) {
  return TOOL_ENTRIES.find((item) => item.view === view) || null
}

function getToolTitle(view) {
  const entry = getToolEntry(view)
  return entry ? entry.label : ''
}

function createToolInitialState() {
  return {
    ...crcTool.createInitialState(),
    ...filterCalculator.createInitialState(),
    ...smdCodeCalculator.createInitialState(),
    ...refrigerationCalculator.createInitialState(),
    ...reactanceCalculator.createInitialState(),
    ...threePhasePowerCalculator.createInitialState()
  }
}

const toolNavigation = {
  getToolEntry,
  getToolEntries,
  getToolTitle,
  isToolView
}

const toolPageHandlers = {
  copyToolResult(event) {
    const value = event && event.currentTarget && event.currentTarget.dataset
      ? event.currentTarget.dataset.value
      : ''
    const text = String(value === undefined || value === null ? '' : value).trim()

    if (!text || text === '--') return

    const wxApi = getWxApi()
    if (typeof wxApi.setClipboardData !== 'function') {
      if (this.pageToast) this.pageToast.show('当前环境不支持复制', 'error')
      return
    }

    wxApi.setClipboardData({
      data: text,
      fail: () => {
        if (this.pageToast) this.pageToast.show('复制失败', 'error')
      },
      success: () => {
        if (this.pageToast) this.pageToast.show('已复制')
      }
    })
  },
  ...crcHandlers.handlers,
  ...filterHandlers.handlers,
  ...reactanceHandlers.handlers,
  ...smdCodeHandlers.handlers,
  ...refrigerationHandlers.handlers,
  ...threePhasePowerHandlers.handlers
}

module.exports = {
  createToolInitialState,
  toolNavigation,
  toolPageHandlers
}
