import 'package:flutter/material.dart';

/// 间距系统
///
/// 使用 4px 基础单位，确保一致性
class AppSpacing {
  AppSpacing._();

  // ============================================
  // 基础间距 (Base Spacing)
  // ============================================

  /// 极小 - 4px
  static const double xs = 4.0;

  /// 小 - 8px
  static const double sm = 8.0;

  /// 中 - 16px (默认)
  static const double md = 16.0;

  /// 大 - 24px
  static const double lg = 24.0;

  /// 特大 - 32px
  static const double xl = 32.0;

  /// 超大 - 48px
  static const double xxl = 48.0;

  // ============================================
  // 预设 EdgeInsets
  // ============================================

  /// 页面内边距
  static const EdgeInsets pagePadding = EdgeInsets.all(md);

  /// 卡片内边距
  static const EdgeInsets cardPadding = EdgeInsets.all(md);

  /// 列表项内边距
  static const EdgeInsets listItemPadding = EdgeInsets.symmetric(
    horizontal: md,
    vertical: sm,
  );

  /// 按钮内边距
  static const EdgeInsets buttonPadding = EdgeInsets.symmetric(
    horizontal: lg,
    vertical: 14,
  );

  /// 输入框内边距
  static const EdgeInsets inputPadding = EdgeInsets.symmetric(
    horizontal: md,
    vertical: 14,
  );

  // ============================================
  // 便捷方法
  // ============================================

  /// 获取水平间距
  static SizedBox horizontal(double spacing) => SizedBox(width: spacing);

  /// 获取垂直间距
  static SizedBox vertical(double spacing) => SizedBox(height: spacing);
}

/// 圆角系统 - "The Kinetic Vault" & "The Ethereal Terminal" 规范
///
/// 设计规则：
/// - 按钮: xxl (24px / 1.5rem)
/// - 卡片: xl (16px)
/// - 输入框: md (8px)
/// - 标签: sm (4px)
class AppRadius {
  AppRadius._();

  // ============================================
  // 基础圆角 (Base Radius)
  // ============================================

  /// 小圆角 - 4px (标签、小组件)
  static const double sm = 4.0;

  /// 中圆角 - 8px (输入框)
  static const double md = 8.0;

  /// 大圆角 - 12px (卡片 - 旧版)
  static const double lg = 12.0;

  /// 特大圆角 - 16px (大卡片)
  static const double xl = 16.0;

  /// 超大圆角 - 24px (按钮、模态框、底部抽屉)
  static const double xxl = 24.0;

  /// 圆形 - 9999px (Pill buttons)
  static const double full = 9999.0;

  // ============================================
  // 预设 BorderRadius
  // ============================================

  /// 小圆角
  static BorderRadius get radiusSm => BorderRadius.circular(sm);

  /// 中圆角
  static BorderRadius get radiusMd => BorderRadius.circular(md);

  /// 大圆角
  static BorderRadius get radiusLg => BorderRadius.circular(lg);

  /// 特大圆角
  static BorderRadius get radiusXl => BorderRadius.circular(xl);

  /// 超大圆角
  static BorderRadius get radiusXxl => BorderRadius.circular(xxl);

  /// 圆形
  static BorderRadius get radiusFull => BorderRadius.circular(full);
}

/// 响应式断点
class AppBreakpoints {
  AppBreakpoints._();

  /// 手机竖屏
  static const double mobile = 360;

  /// 手机横屏/小平板
  static const double tablet = 768;

  /// 平板/桌面
  static const double desktop = 1024;

  /// 检查是否为手机
  static bool isMobile(BuildContext context) {
    return MediaQuery.of(context).size.width < tablet;
  }

  /// 检查是否为平板
  static bool isTablet(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    return width >= tablet && width < desktop;
  }

  /// 检查是否为桌面
  static bool isDesktop(BuildContext context) {
    return MediaQuery.of(context).size.width >= desktop;
  }

  /// 根据屏幕宽度获取响应式值
  static T responsive<T>(
    BuildContext context, {
    required T mobile,
    T? tablet,
    T? desktop,
  }) {
    final width = MediaQuery.of(context).size.width;
    if (width >= AppBreakpoints.desktop) {
      return desktop ?? tablet ?? mobile;
    } else if (width >= AppBreakpoints.tablet) {
      return tablet ?? mobile;
    }
    return mobile;
  }
}

/// 触摸目标尺寸
class AppTouchTarget {
  AppTouchTarget._();

  /// 最小触摸目标 - 44x44 (iOS HIG 标准)
  static const double minSize = 44.0;

  /// 确保触摸目标足够大
  static Widget ensureMinSize({
    required Widget child,
    double minSize = AppTouchTarget.minSize,
  }) {
    return ConstrainedBox(
      constraints: BoxConstraints(
        minWidth: minSize,
        minHeight: minSize,
      ),
      child: child,
    );
  }
}
