import 'package:flutter/material.dart';

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

/// 主题样式快捷封装 — 字体 + 预组合样式
///
/// `context.styles` 返回此对象，build 方法里一行获取字体样式，
/// 之后直接用 `s.bodySmall`、`s.sectionLabel`，不再写 Theme.of(context)。
///
/// 颜色访问走 `context.colors`（见下方 [AppColorsX] 扩展），
/// 本类只负责字体和「字体+颜色」的预组合。
///
/// ```dart
/// final s = context.styles;
/// final c = context.colors;
///
/// Text('标题', style: s.titleMedium)
/// Text('描述', style: s.bodySmall)
/// Text('分组', style: s.sectionLabel)   // 预组合：字体 + 品牌色
/// Icon(Icons.home, color: c.brandPrimary)
/// ```
class AppStyles {
  AppStyles(BuildContext context)
      : _t = Theme.of(context).textTheme,
        _colors = Theme.of(context).brightness == Brightness.dark
            ? const DarkColors()
            : const LightColors();

  final TextTheme _t;
  final AppColors _colors;

  // ── 亮暗判断 ──────────────────────────────────────────────────────────────

  bool get isDark => _colors is DarkColors;
  Brightness get brightness => isDark ? Brightness.dark : Brightness.light;

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

  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;

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

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

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

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

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

/// 语义颜色入口 — 按亮暗模式返回对应的 [AppColors] 实例
///
/// ```dart
/// final c = context.colors;
/// Container(color: c.bgSecondary)
/// Text('hello', style: TextStyle(color: c.textPrimary))
/// ```
extension AppColorsX on BuildContext {
  AppColors get colors => Theme.of(this).brightness == Brightness.dark
      ? const DarkColors()
      : const LightColors();
}

/// 字体样式入口
///
/// ```dart
/// final s = context.styles;
/// Text('标题', style: s.headlineMedium)
/// ```
extension AppThemeX on BuildContext {
  AppStyles get styles => AppStyles(this);
}

/// 阴影入口
///
/// ```dart
/// Container(
///   decoration: BoxDecoration(boxShadow: context.shadows.bs8),
/// )
/// ```
extension AppShadowsX on BuildContext {
  AppShadows get shadows => AppShadows(this);
}
