import 'package:flutter/material.dart';

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

/// 主题组装 -- 将 AppColors / AppFont 组装为 ThemeData
///
/// 同时提供 Light / Dark 双主题，按钮形状/颜色/字体统一在此定义，
/// AppButton 只负责变体切换和 loading 逻辑，不硬编码颜色和字体。
///
/// ## 数据流位置
///
/// ```
/// AppColors + AppFont (L1 常量)
///   → ★ AppTheme ★ (L1 组装) ← 你在这里
///     → MaterialApp(theme: AppTheme.theme, darkTheme: AppTheme.darkTheme)
///       → Theme.of(context) → 所有 Widget 自动响应主题变化
/// ```
///
/// ## 使用
///
/// ```dart
/// // app/app.dart
/// MaterialApp(
///   theme: AppTheme.theme,           // getter 名与 MaterialApp 参数名一一对应
///   darkTheme: AppTheme.darkTheme,
/// )
/// ```
class AppTheme {
  AppTheme._();

  /// 亮色主题 — 对应 MaterialApp `theme:` 参数
  static ThemeData get theme => _build(Brightness.light);

  /// 暗色主题 — 对应 MaterialApp `darkTheme:` 参数
  static ThemeData get darkTheme => _build(Brightness.dark);

  static ThemeData _build(Brightness brightness) {
    final isDark = brightness == Brightness.dark;
    final primary = isDark ? AppColors.primaryLight : AppColors.primary;

    return ThemeData(
      useMaterial3: true,
      brightness: brightness,
      colorScheme: ColorScheme(
        brightness: brightness,
        primary: primary,
        onPrimary: AppColors.white,
        secondary: primary,
        onSecondary: AppColors.white,
        error: AppColors.error,
        onError: AppColors.white,
        surface: isDark ? AppColors.gray800 : AppColors.white,
        onSurface: isDark ? AppColors.white : AppColors.gray900,
      ),
      scaffoldBackgroundColor: isDark ? AppColors.gray900 : AppColors.gray50,

      // 字体
      textTheme: AppFont.textTheme(brightness),

      // ElevatedButton → AppButton.primary
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: AppColors.primary,
          foregroundColor: AppColors.white,
          disabledBackgroundColor: AppColors.gray400,
          minimumSize: const Size.fromHeight(48),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
      ),

      // OutlinedButton → AppButton.secondary
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          foregroundColor: primary,
          side: BorderSide(color: primary),
          minimumSize: const Size.fromHeight(48),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
      ),

      // TextButton → AppButton.text
      textButtonTheme: TextButtonThemeData(
        style: TextButton.styleFrom(
          foregroundColor: primary,
          minimumSize: const Size.fromHeight(48),
        ),
      ),
    );
  }
}
