import axios from 'axios'
import { request } from '@/api/_service'

const EDGE_BASE = process.env.VUE_APP_HSLSERVER_API
const EDGE_PASSWORD = process.env.VUE_APP_HSLSERVER_PASSWORD

function edgeUrl (query = '') {
  return query ? `${EDGE_BASE}?${query}` : EDGE_BASE
}

export function edgeGet (url) {
  return axios({ method: 'get', url }).then(res => res.data)
}

export function edgePost (url, data) {
  return axios({ method: 'post', url, data }).then(res => res.data)
}

export function addNode (data) { return edgePost(EDGE_BASE, data) }
export function updateNode (data) { return edgePost(EDGE_BASE, data) }
export function removeNode (data) { return edgePost(EDGE_BASE, data) }

export function getQueryNode (data = {}) {
  const params = new URLSearchParams()
  params.set('query', 'nodes')
  params.set('currentPage', data.currentPage || data.page_no || 1)
  params.set('pageSize', data.pageSize || data.page_size || 10)
  params.set('working_subclass', data.working_subclass || '')
  params.set('category', data.categoryName || data.category || '')
  return edgeGet(edgeUrl(params.toString()))
}

export function getQueryWorkingSubclasses (data = {}) {
  const params = new URLSearchParams()
  params.set('query', 'working_subclasses')
  if (data.device_code) params.set('device_code', data.device_code)
  return edgeGet(edgeUrl(params.toString()))
}

export function getQueryCodes (workingSubclass, deviceCode) {
  const params = new URLSearchParams()
  params.set('query', 'codes')
  params.set('working_subclass', workingSubclass || '')
  if (deviceCode) params.set('device_code', deviceCode)
  return edgeGet(edgeUrl(params.toString()))
}

export function getNodeValue () { return edgeGet(edgeUrl('query=nodes_value')) }
export function getNodeAll () { return edgeGet(edgeUrl('query=nodes_all')) }

export function getNodeMappingDeviceList (data = {}) {
  const params = new URLSearchParams()
  params.set('query', 'node_mapping_device_list')
  if (data.page_no !== undefined) params.set('currentPage', data.page_no)
  if (data.page_size !== undefined) params.set('pageSize', data.page_size)
  if (data.scada_data_capture_node_code) params.set('scada_data_capture_node_code', data.scada_data_capture_node_code)
  if (data.device_code) params.set('device_code', data.device_code)
  return edgeGet(edgeUrl(params.toString()))
}

export function addNodeMappingDevice (data) { return edgePost(EDGE_BASE, data) }
export function editNodeMappingDevice (data) { return edgePost(EDGE_BASE, data) }
export function delNodeMappingDevice (data) { return edgePost(EDGE_BASE, data) }

export function getExecLog (data = {}) {
  const params = new URLSearchParams()
  params.set('query', 'get_exec_log')
  params.set('page_no', data.page_no || 1)
  params.set('page_size', data.page_size || 10)
  if (data.create_date) params.set('create_date', data.create_date)
  if (data.command) params.set('command', data.command)
  if (data.server_name) params.set('server_name', data.server_name)
  if (data.device_name) params.set('device_name', data.device_name)
  return edgeGet(edgeUrl(params.toString()))
}

export function getQueryNodeData (data = {}) {
  const params = new URLSearchParams()
  params.set('query', 'node_data')
  params.set('startRow', data.startRow || 0)
  params.set('endRow', data.endRow || 50)
  params.set('working_subclass', data.workingSubclass || data.working_subclass || '')
  params.set('wipCode', data.wipCode || '')
  params.set('start_time', data.startTime || '')
  params.set('end_time', data.endTime || '')
  params.set('dedup', data.dedup === undefined ? 1 : data.dedup)
  if (data.code) params.set('code', Array.isArray(data.code) ? data.code.join(',') : data.code)
  if (data.device_code) params.set('device_code', data.device_code)
  return edgeGet(edgeUrl(params.toString()))
}

export function queryServers () { return request({ url: `${EDGE_BASE}?query=servers` }) }
export function addServer (data) { return request({ url: EDGE_BASE, method: 'post', data }) }
export function updateServer (data) { return request({ url: EDGE_BASE, method: 'post', data }) }
export function removeServer (data) { return request({ url: EDGE_BASE, method: 'post', data }) }
export function setServerExec (data) { return request({ url: EDGE_BASE, method: 'post', data }) }

export function verifyServer (url) {
  return request({ auth: { username: 'admin', password: EDGE_PASSWORD }, method: 'post', url: `${url}/Admin/ServerSettingsRequest` })
}

export function modifyServer (url, data) {
  return request({ auth: { username: 'admin', password: EDGE_PASSWORD }, method: 'post', url: `${url}/Admin/ServerSettingsModify`, data })
}

export function getServeDeviceMonitoring (url) {
  return request({ auth: { username: 'admin', password: EDGE_PASSWORD }, url: `${url}/Edge/DeviceData?data=/` })
}
