import types from '../mutation-types'
import socket from '../../scripts/socket'

// State
const state = {
  scan: false,
  pairedDevices: [],
  notPairedDevices: [],
  controllers: [],
  rememberedBluetooth: {
    status: null,
    deviceMac: ''
  },
  forgetedBluetooth: {
    status: null,
    deviceMac: ''
  },
  rememberMac: ''
}

// Getters
const getters = {

}

// Actions
const actions = {

  scanBT ({ commit, state }) {
    state.scan = !state.scan
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.scan}`, state.scan)
  },

  // Get Devices
  getDevices ({ commit, state }) {
    state.rememberMac = this.state.config.bluetoothAutoConnectMac
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.getDevices}`)
  },

  // Get Bluetooth Controller
  getControllers ({ commit, state }) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.getControllers}`)
  },

  // Connect Device
  connect ({ commit, state }, deviceMac) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.connect}`, deviceMac)
  },

  // Disconnect Device
  disconnect ({ commit, state }, deviceMac) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.disconnect}`, deviceMac)
  },

  // Get Paired Devices
  getPairedDevices ({ commit, state }) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.getPairedDevices}`)
  },

  // Remove Device
  removeDevice ({commit, state}, deviceMac) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.removeDevice}`, deviceMac)
  },

  // Remember Device
  rememberBluetooth ({commit, state}, deviceMac) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.rememberBluetooth}`, deviceMac)
  },

  // Forget Device
  forgetBluetooth ({commit, state}, deviceMac) {
    socket.emit(`${types.bluetooth.namespace}/${types.bluetooth.events.forgetBluetooth}`, deviceMac)
  }

}

// Mutations
const mutations = {

  // Scan Off
  [types.bluetooth.events.scanOff] (state, data) {
    state.scan = false
  },

  // Devices GET
  [types.bluetooth.events.devices] (state, devices) {
    // Checking info before clear data
    state.pairedDevices = []
    state.notPairedDevices = []

    devices.forEach(device => {
      if (device.paired === 'yes') {
        state.pairedDevices.push(device)
      } else {
        state.notPairedDevices.push(device)
      }
    })
  },

  // Controller
  [types.bluetooth.events.controllers] (state, data) {
    state.controllers = data
  },

  // Remembered Bluetooth Device
  [types.bluetooth.events.rememberedBluetooth] (state, data) {
    state.rememberedBluetooth.status = data.status
    state.rememberedBluetooth.deviceMac = data.deviceMac
    this.state.config.bluetoothAutoConnectMac = data.deviceMac
  },

  // Forgeted Bluetooth Device
  [types.bluetooth.events.forgetedBluetooth] (state, data) {
    state.forgetedBluetooth.status = data.status
    state.forgetedBluetooth.deviceMac = data.deviceMac
    this.state.config.bluetoothAutoConnectMac = ''
  }

}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
