export function getPathConflictDisplayEnabled(settings = {}) {
  return settings.pathConflictDisplayEnabled === undefined
    ? true
    : settings.pathConflictDisplayEnabled !== false
}

export function normalizePathConflictMessage(message) {
  const payload = message?.payload || {}

  if (message?.type === 'path_conflict_alert' && payload.messageType === 'PATH_CONFLICT_RESUME') {
    return {
      kind: 'resume',
      key: getConflictKey(payload) || payload.vehicleName || payload.object1?.objectName || '',
      aircraftName: payload.aircraftName || payload.object1?.objectName || '',
      vehicleName: payload.vehicleName || payload.object2?.objectName || payload.object1?.objectName || '',
      rawData: payload,
    }
  }

  if (message?.type === 'path_conflict_status') {
    return buildConflict('status', payload, {
      title: '计算中',
      level: 'STATUS',
    })
  }

  if (message?.type === 'path_conflict_alert') {
    const level = payload.alertLevel || (payload.alertType === 'CONFLICT_ALERT' || payload.alert === true ? 'CRITICAL' : 'WARNING')
    return buildConflict('alert', payload, {
      title: getAlertTitle(level),
      level,
    })
  }

  return null
}

export function isNewPathConflictPayload(payload) {
  if (!payload) return false
  return Boolean(
    payload.messageType === 'PATH_CONFLICT_RESUME' ||
    payload.aircraftName ||
    payload.vehicleName ||
    payload.aircraftDistanceToConflictMeters !== undefined ||
    payload.vehicleDistanceToConflictMeters !== undefined ||
    payload.aircraftAlertThresholdMeters !== undefined ||
    payload.vehicleAlertThresholdMeters !== undefined ||
    payload.vehicleMovingTowardConflictPoint !== undefined ||
    payload.conflictPoint ||
    payload.position
  )
}

function buildConflict(kind, payload, extra) {
  const aircraftName = payload.aircraftName || payload.object1?.objectName || ''
  const vehicleName = payload.vehicleName || payload.object2?.objectName || payload.object1?.objectName || ''

  return {
    kind,
    ...extra,
    key: getConflictKey(payload) || [aircraftName, vehicleName].filter(Boolean).join('::'),
    conflictPoint: payload.conflictPoint || payload.position || null,
    aircraftName,
    vehicleName,
    aircraftDistance: payload.aircraftDistanceToConflictMeters,
    vehicleDistance: payload.vehicleDistanceToConflictMeters || payload.object2Distance,
    aircraftThreshold: payload.aircraftAlertThresholdMeters,
    vehicleThreshold: payload.vehicleAlertThresholdMeters,
    vehicleMovingToward: payload.vehicleMovingTowardConflictPoint,
    directionLockStatus: payload.directionLockStatus,
    calculationStatus: payload.calculationStatus,
    directionLockText: getDirectionLockText(payload),
    directionLockFailed: isDirectionLockFailed(payload),
    directionLockReason: payload.directionLockReason || '',
    object1ForwardDistance: payload.object1ForwardDistanceMeters,
    object2ForwardDistance: payload.object2ForwardDistanceMeters,
    rawData: payload,
  }
}

function getConflictKey(payload) {
  const aircraftName = payload.aircraftName || ''
  const vehicleName = payload.vehicleName || payload.object2?.objectName || payload.object1?.objectName || ''
  return [aircraftName, vehicleName].filter(Boolean).join('::')
}

function getAlertTitle(level) {
  if (level === 'EMERGENCY') return '紧急告警'
  if (level === 'CRITICAL') return '冲突告警'
  return '冲突预警'
}

function isDirectionLockFailed(payload) {
  return payload.directionLockStatus === 'DIRECTION_INVALID' ||
    payload.calculationStatus === 'DIRECTION_LOCK_FAILED'
}

function getDirectionLockText(payload) {
  if (payload.directionLockStatus === undefined && payload.calculationStatus === undefined) return ''
  if (isDirectionLockFailed(payload)) return '方向无法锁定'
  if (payload.directionLockStatus === 'DIRECTION_LOCKED') return '方向已锁定'
  return ''
}
