import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'app_colors.dart';
import 'app_colors_extension.dart';

class AppTheme {
  AppTheme._();

  static Map<String, Font> _fontMap() => <String, Font>{
    'fontBodyMedium': Font(size: AppFontSizes.body.toInt(), lineHeight: 22),
    'fontBodySmall': Font(size: AppFontSizes.caption.toInt(), lineHeight: 20),
    'fontTitleMedium': Font(
      size: AppFontSizes.subtitle.toInt(),
      lineHeight: 24,
    ),
    'fontTitleLarge': Font(size: AppFontSizes.title.toInt(), lineHeight: 26),
  };

  /// TDesign 亮色主题
  static TDThemeData get tdThemeData {
    final base = TDThemeData.defaultData();
    return base.copyWithTDThemeData(
      'tbossLight',
      colorMap: <String, Color>{
        'brandNormalColor': AppColors.primary,
        'brandClickColor': AppColors.primaryActive,
        'successNormalColor': AppColors.success,
        'warningNormalColor': AppColors.warning,
        'errorNormalColor': AppColors.danger,
        'textColorPrimary': AppColors.textPrimary,
        'textColorSecondary': AppColors.textSecondary,
        'textColorPlaceholder': AppColors.gray400,
        'textDisabledColor': AppColors.textDisabled,
        'bgColorPage': AppColors.bgPage,
        'bgColorContainer': AppColors.bgCard,
        'bgColorSecondaryContainer': AppColors.bgDisabled,
        'borderColor': AppColors.border,
        'brandLightColor': AppColors.primaryLight,
        'componentStrokeColor': AppColors.border,
      },
      fontMap: _fontMap(),
    );
  }

  /// TDesign 深色主题
  static TDThemeData get darkTdThemeData {
    final base = TDThemeData.defaultData();
    return base.copyWithTDThemeData(
      'tbossDark',
      colorMap: <String, Color>{
        'brandNormalColor': AppColors.primary,
        'brandClickColor': AppColors.primaryActive,
        'successNormalColor': AppDarkColors.success,
        'warningNormalColor': AppDarkColors.warning,
        'errorNormalColor': AppDarkColors.danger,
        'textColorPrimary': AppDarkColors.textPrimary,
        'textColorSecondary': AppDarkColors.textSecondary,
        'textColorPlaceholder': AppDarkColors.textPlaceholder,
        'textDisabledColor': AppDarkColors.textDisabled,
        'bgColorPage': AppDarkColors.bgPage,
        'bgColorContainer': AppDarkColors.bgCard,
        'bgColorSecondaryContainer': AppDarkColors.bgSecondaryContainer,
        'borderColor': AppDarkColors.border,
        'brandLightColor': AppDarkColors.primaryLight,
        'componentStrokeColor': AppDarkColors.border,
      },
      fontMap: _fontMap(),
    );
  }

  static ThemeData get light {
    final colorScheme = ColorScheme.fromSeed(
      seedColor: AppColors.primary,
      brightness: Brightness.light,
    ).copyWith(surface: AppColors.bgPage);

    return ThemeData(
      useMaterial3: true,
      extensions: [AppColorsExtension.light, AppTheme.tdThemeData],
      colorScheme: colorScheme,
      canvasColor: AppColors.bgPage,
      scaffoldBackgroundColor: AppColors.bgPage,
      appBarTheme: const AppBarTheme(
        backgroundColor: AppColors.primary,
        foregroundColor: Colors.white,
        elevation: 0,
        centerTitle: false,
        titleTextStyle: TextStyle(
          fontSize: AppFontSizes.title,
          fontWeight: FontWeight.w600,
          color: Colors.white,
        ),
      ),
      cardTheme: CardThemeData(
        color: AppColors.bgCard,
        elevation: 1,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: AppColors.primary,
          foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(22),
          ),
          minimumSize: const Size(double.infinity, 44),
          textStyle: const TextStyle(
            fontSize: AppFontSizes.body,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          foregroundColor: AppColors.textPrimary,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(22),
          ),
          minimumSize: const Size(double.infinity, 44),
          side: const BorderSide(color: AppColors.border),
          textStyle: const TextStyle(fontSize: AppFontSizes.body),
        ),
      ),
      dividerColor: AppColors.border,
      dividerTheme: const DividerThemeData(
        color: AppColors.border,
        thickness: 1,
      ),
    );
  }

  static ThemeData get dark {
    final colorScheme = ColorScheme.fromSeed(
      seedColor: AppColors.primary,
      brightness: Brightness.dark,
    ).copyWith(surface: AppDarkColors.bgPage);

    return ThemeData(
      useMaterial3: true,
      extensions: [AppColorsExtension.dark, AppTheme.darkTdThemeData],
      colorScheme: colorScheme,
      canvasColor: AppDarkColors.bgPage,
      scaffoldBackgroundColor: AppDarkColors.bgPage,
      appBarTheme: const AppBarTheme(
        backgroundColor: AppDarkColors.bgCard,
        foregroundColor: AppDarkColors.textPrimary,
        elevation: 0,
        centerTitle: false,
        titleTextStyle: TextStyle(
          fontSize: AppFontSizes.title,
          fontWeight: FontWeight.w600,
          color: AppDarkColors.textPrimary,
        ),
      ),
      cardTheme: CardThemeData(
        color: AppDarkColors.bgCard,
        elevation: 1,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: AppColors.primary,
          foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(22),
          ),
          minimumSize: const Size(double.infinity, 44),
          textStyle: const TextStyle(
            fontSize: AppFontSizes.body,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          foregroundColor: AppDarkColors.textPrimary,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(22),
          ),
          minimumSize: const Size(double.infinity, 44),
          side: const BorderSide(color: AppDarkColors.border),
          textStyle: const TextStyle(fontSize: AppFontSizes.body),
        ),
      ),
      dividerColor: AppDarkColors.border,
      dividerTheme: const DividerThemeData(
        color: AppDarkColors.border,
        thickness: 1,
      ),
    );
  }
}
