using System;
using System.Text.RegularExpressions;
namespace ClassNotify.Shared.Utils;
///
/// 教室大屏配对码权威格式化工具
/// 核心红线约束:
/// 1. 任何时候配对码严禁包含横杠 '-'
/// 2. 配对码严禁分为明显的两组4个字符,必须严格等间距排列 (例如 '5 8 2 9 1 0 3 4')
/// 3. 占位状态严禁出现 '--------',应显示为空白或提示文本
///
public static class PairingCodeFormatter
{
public const string DefaultPlaceholder = "等待获取";
///
/// 标准化配对码(移除所有空格、横杠、换行符)
///
public static string Normalize(string? input)
{
if (string.IsNullOrWhiteSpace(input)) return string.Empty;
return input.Replace("-", "").Replace(" ", "").Trim();
}
///
/// 校验配对码是否为合规的 8 位纯数字
///
public static bool IsValid(string? input)
{
var clean = Normalize(input);
return clean.Length == 8 && Regex.IsMatch(clean, @"^\d{8}$");
}
///
/// 转换为大屏与终端 4+4 空格格式化的字符串(严格无横杠,例如 '5829 1034')
///
public static string FormatPairingCode(string? code, string placeholder = DefaultPlaceholder)
{
if (string.IsNullOrWhiteSpace(code)) return placeholder;
var digits = new string(System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Where(code, char.IsDigit)));
if (digits.Length == 8)
return $"{digits[..4]} {digits[4..]}";
return digits.Length > 0 ? digits : placeholder;
}
///
/// 转换为大屏与终端显示的字符串
///
public static string FormatEqualSpacing(string? code, string placeholder = DefaultPlaceholder)
{
return FormatPairingCode(code, placeholder);
}
}