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
///
/// 接收 [AppColors] 语义颜色实例，零 isDark 分支。
/// 亮暗色差异完全由 [LightColors] / [DarkColors] 实现决定，
/// 本类只做「语义 → ThemeData 属性」的映射。
///
/// ## 数据流位置
///
/// ```
/// ColorBases（颜色常量）→ AppColors（语义接口）
///   → LightColors / DarkColors（实现）
///     → ★ AppTheme._build(AppColors c) ★ ← 你在这里
///       → MaterialApp(theme: AppTheme.theme, darkTheme: AppTheme.darkTheme)
///         → Theme.of(context) → 所有 Widget 自动响应主题变化
/// ```
///
/// ## 使用
///
/// ```dart
/// // app/app.dart
/// MaterialApp(
///   theme: AppTheme.theme,
///   darkTheme: AppTheme.darkTheme,
/// )
/// ```
class AppTheme {
  AppTheme._();

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

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

  static ThemeData _build(AppColors c) {
    final brightness = c is DarkColors ? Brightness.dark : Brightness.light;

    return ThemeData(
      useMaterial3: true,
      brightness: brightness,

      // ── ColorScheme ────────────────────────────────────────────────────
      colorScheme: ColorScheme(
        brightness: brightness,
        primary: c.brandPrimary,
        onPrimary: c.textOnPrimary,
        secondary: c.brandPrimary,
        onSecondary: c.textOnPrimary,
        error: c.statusError,
        onError: c.textOnPrimary,
        surface: c.bgSecondary,
        onSurface: c.textPrimary,
      ),
      scaffoldBackgroundColor: c.bgPrimary,

      // ── Typography ─────────────────────────────────────────────────────
      textTheme: AppFont.textTheme(brightness),

      // ── AppBar ─────────────────────────────────────────────────────────
      appBarTheme: AppBarTheme(
        backgroundColor: c.bgPrimary,
        foregroundColor: c.textPrimary,
        elevation: 0,
        centerTitle: true,
      ),

      // ── ElevatedButton → AppButton.primary ─────────────────────────────
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: c.brandPrimary,
          foregroundColor: c.textOnPrimary,
          disabledBackgroundColor: c.borderDefault,
          disabledForegroundColor: c.textDisabled,
          minimumSize: const Size.fromHeight(48),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
          ),
        ),
      ),

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

      // ── TextButton → AppButton.text ────────────────────────────────────
      textButtonTheme: TextButtonThemeData(
        style: TextButton.styleFrom(
          foregroundColor: c.brandPrimary,
          minimumSize: const Size.fromHeight(48),
        ),
      ),

      // ── InputDecoration ────────────────────────────────────────────────
      inputDecorationTheme: InputDecorationTheme(
        fillColor: c.bgSecondary,
        filled: true,
        contentPadding: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 14,
        ),
        border: OutlineInputBorder(
          borderSide: BorderSide(color: c.borderDefault),
          borderRadius: BorderRadius.circular(8),
        ),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(color: c.borderDefault),
          borderRadius: BorderRadius.circular(8),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: c.borderFocused, width: 2),
          borderRadius: BorderRadius.circular(8),
        ),
        errorBorder: OutlineInputBorder(
          borderSide: BorderSide(color: c.statusError),
          borderRadius: BorderRadius.circular(8),
        ),
        hintStyle: TextStyle(color: c.textDisabled),
      ),

      // ── Card ───────────────────────────────────────────────────────────
      cardTheme: CardThemeData(
        color: c.bgSecondary,
        elevation: 0,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
          side: BorderSide(color: c.borderDefault),
        ),
      ),

      // ── Divider ────────────────────────────────────────────────────────
      dividerTheme: DividerThemeData(
        color: c.divider,
        thickness: 1,
      ),

      // ── BottomNavigationBar ────────────────────────────────────────────
      bottomNavigationBarTheme: BottomNavigationBarThemeData(
        backgroundColor: c.navBarBg,
        selectedItemColor: c.navBarSelected,
        unselectedItemColor: c.textDisabled,
        showUnselectedLabels: true,
        type: BottomNavigationBarType.fixed,
      ),
    );
  }
}
