import 'package:flutter/material.dart';

import 'package:im_app/core/ui/base/font.dart';

/// 主题样式快捷封装
///
/// `context.styles` 返回此对象，build 方法里一行获取所有样式，
/// 之后直接用 `s.bodySmall`、`s.primary`，不再写 Theme.of(context)。
///
/// ```dart
/// final s = context.styles;
///
/// Text('标题', style: s.titleMedium)
/// Text('描述', style: s.bodySmall)
/// Icon(Icons.home, color: s.primary)
/// Text('改色', style: s.bodySmall?.copyWith(color: s.primary))
/// ```
class AppStyles {
  AppStyles(BuildContext context)
      : _t = Theme.of(context).textTheme,
        _c = Theme.of(context).colorScheme;

  final TextTheme _t;
  final ColorScheme _c;

  // ── 字体 ──────────────────────────────────────────────────────────────────

  TextStyle? get displayLarge => _t.displayLarge;
  TextStyle? get displayMedium => _t.displayMedium;
  TextStyle? get displaySmall => _t.displaySmall;

  TextStyle? get headlineLarge => _t.headlineLarge;
  TextStyle? get headlineMedium => _t.headlineMedium;
  TextStyle? get headlineSmall => _t.headlineSmall;

  TextStyle? get titleLarge => _t.titleLarge;
  TextStyle? get titleMedium => _t.titleMedium;
  TextStyle? get titleSmall => _t.titleSmall;

  TextStyle? get bodyLarge => _t.bodyLarge;
  TextStyle? get bodyMedium => _t.bodyMedium;
  TextStyle? get bodySmall => _t.bodySmall;

  TextStyle? get labelLarge => _t.labelLarge;
  TextStyle? get labelMedium => _t.labelMedium;
  TextStyle? get labelSmall => _t.labelSmall;

  // ── 颜色 + 亮暗 ───────────────────────────────────────────────────────────

  Brightness get brightness => _c.brightness;
  bool get isDark => _c.brightness == Brightness.dark;

  Color get primary => _c.primary;
  Color get onPrimary => _c.onPrimary;
  Color get secondary => _c.secondary;
  Color get onSecondary => _c.onSecondary;
  Color get error => _c.error;
  Color get onError => _c.onError;
  Color get surface => _c.surface;
  Color get onSurface => _c.onSurface;
  Color get outline => _c.outline;
  Color get outlineVariant => _c.outlineVariant;

  // ── 预组合样式（字体 + 颜色，开箱即用）──────────────────────────────────────
  //
  // 与 AppButton 变体理念一致：按语义选用，无需手动拼 TextStyle 或 copyWith。
  // 新增场景时在此扩展，保持全局一致。

  /// 分组标题 — 列表 Section、设置分组等（sectionLabel 字体 + primary 色）
  TextStyle get sectionLabel => AppFont.sectionLabel.copyWith(color: primary);

  /// 辅助文字 — 元数据、次要信息、时间戳等（labelMedium + outline 色）
  TextStyle? get labelMuted => labelMedium?.copyWith(color: outline);

  /// 正文次要 — 描述、提示等（bodySmall + outline 色）
  TextStyle? get bodyMuted => bodySmall?.copyWith(color: outline);

  /// 错误提示 — 表单错误、警告等（bodySmall + error 色）
  TextStyle? get bodyError => bodySmall?.copyWith(color: error);
}

/// BuildContext 主题入口
///
/// ```dart
/// final s = context.styles;
/// ```
extension AppThemeX on BuildContext {
  AppStyles get styles => AppStyles(this);
}
