import request from '@/utils/request'

const DEFAULT_SCREEN_CODE = 'WORKSHOP_BOARD_DEFAULT'

function createEmptyScreenBindingConfig(screenCode = DEFAULT_SCREEN_CODE) {
  return {
    screenCode,
    workshopId: null,
    workshopCode: '',
    workshopName: '',
    updatedAt: '',
    basicBindings: [],
    machineConfigs: []
  }
}

function toUiBinding(binding) {
  return {
    bindingId: binding.bindingId,
    screenCode: binding.screenCode,
    workshopId: binding.workshopId,
    workshopCode: binding.workshopCode,
    workshopName: binding.workshopName,
    sourceType: binding.sourceType || 'FIXED',
    pointId: binding.pointId,
    pointCode: binding.pointCode || '',
    pointName: binding.pointName || '',
    fixedValue: binding.fixedValue || '',
    defaultValue: binding.defaultValue || '',
    displayUnit: binding.displayUnit || '',
    precisionDigit: binding.precisionDigit,
    sortNum: binding.sortNum,
    statusClass: binding.statusClass || 'is-running',
    enableFlag: binding.enableFlag || 'Y',
    key: binding.bindingKey,
    label: binding.bindingName
  }
}

function toUiConfig(data) {
  const emptyConfig = createEmptyScreenBindingConfig(data.screenCode || DEFAULT_SCREEN_CODE)
  return {
    ...emptyConfig,
    screenCode: data.screenCode || emptyConfig.screenCode,
    workshopId: data.workshopId === undefined ? emptyConfig.workshopId : data.workshopId,
    workshopCode: data.workshopCode || emptyConfig.workshopCode,
    workshopName: data.workshopName || emptyConfig.workshopName,
    updatedAt: data.updatedAt || emptyConfig.updatedAt,
    basicBindings: (data.basicBindings || []).map(toUiBinding),
    machineConfigs: (data.machineConfigs || []).map(item => ({
      id: item.id,
      statusClass: item.statusClass || 'is-running',
      bindings: (item.bindings || []).map(toUiBinding)
    }))
  }
}

function toApiBinding(binding, scope, machineId, statusClass) {
  return {
    bindingId: binding.bindingId,
    screenCode: binding.screenCode,
    workshopId: binding.workshopId,
    workshopCode: binding.workshopCode,
    workshopName: binding.workshopName,
    bindingScope: scope,
    machineId,
    bindingKey: binding.key,
    bindingName: binding.label,
    sourceType: binding.sourceType,
    pointId: binding.pointId,
    pointCode: binding.pointCode,
    pointName: binding.pointName,
    fixedValue: binding.fixedValue,
    defaultValue: binding.defaultValue,
    displayUnit: binding.displayUnit,
    precisionDigit: binding.precisionDigit,
    statusClass: statusClass || binding.statusClass,
    sortNum: binding.sortNum,
    enableFlag: binding.enableFlag,
    remark: binding.remark
  }
}

function toApiConfig(config) {
  return {
    screenCode: config.screenCode,
    workshopId: config.workshopId,
    workshopCode: config.workshopCode,
    workshopName: config.workshopName,
    basicBindings: (config.basicBindings || []).map(item => toApiBinding(item, 'BASIC', 0, item.statusClass)),
    machineConfigs: (config.machineConfigs || []).map(machine => ({
      id: machine.id,
      statusClass: machine.statusClass,
      bindings: (machine.bindings || []).map(item => toApiBinding(item, 'MACHINE', machine.id, machine.statusClass))
    }))
  }
}

export function getScreenBindingConfig(screenCode) {
  return request({
    url: '/mes/md/screenbinding/config',
    method: 'get',
    params: { screenCode }
  }).then(response => ({
    ...response,
    data: toUiConfig(response.data || {})
  }))
}

export function saveScreenBindingConfig(data) {
  return request({
    url: '/mes/md/screenbinding/config',
    method: 'post',
    data: toApiConfig(data)
  }).then(response => ({
    ...response,
    data: toUiConfig(response.data || {})
  }))
}

export function resetScreenBindingConfig(screenCode) {
  return request({
    url: '/mes/md/screenbinding/config',
    method: 'delete',
    params: { screenCode }
  }).then(response => ({
    ...response,
    data: toUiConfig(response.data || {})
  }))
}
