import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/dynamic_theme_provider.dart';
import '../providers/theme_provider.dart';

class AppTheme {
  static ThemeData getLightTheme(BuildContext context) {
    final dynamicThemeProvider =
        Provider.of<DynamicThemeProvider>(context, listen: false);
    final themeProvider = Provider.of<ThemeProvider>(context, listen: false);

    // Get color scheme (dynamic or default)
    final colorScheme = dynamicThemeProvider.getCurrentColorScheme(false);

    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.light,
      colorScheme: colorScheme,
      textTheme: _getTextTheme(colorScheme),
      appBarTheme: _getAppBarTheme(colorScheme),
      cardTheme: _getCardTheme(colorScheme),
      elevatedButtonTheme: _getElevatedButtonTheme(colorScheme),
      outlinedButtonTheme: _getOutlinedButtonTheme(colorScheme),
      textButtonTheme: _getTextButtonTheme(colorScheme),
      inputDecorationTheme: _getInputDecorationTheme(colorScheme),
      bottomNavigationBarTheme: _getBottomNavigationBarTheme(colorScheme),
      floatingActionButtonTheme: _getFloatingActionButtonTheme(colorScheme),
      dividerTheme: _getDividerTheme(colorScheme),
      iconTheme: _getIconTheme(colorScheme),
      chipTheme: _getChipTheme(colorScheme),
      switchTheme: _getSwitchTheme(colorScheme),
      checkboxTheme: _getCheckboxTheme(colorScheme),
      radioTheme: _getRadioTheme(colorScheme),
      sliderTheme: _getSliderTheme(colorScheme),
      progressIndicatorTheme: _getProgressIndicatorTheme(colorScheme),
      snackBarTheme: _getSnackBarTheme(colorScheme),
      dialogTheme: _getDialogTheme(colorScheme),
      bottomSheetTheme: _getBottomSheetTheme(colorScheme),
      tooltipTheme: _getTooltipTheme(colorScheme),
      popupMenuTheme: _getPopupMenuTheme(colorScheme),
      drawerTheme: _getDrawerTheme(colorScheme),
      listTileTheme: _getListTileTheme(colorScheme),
      tabBarTheme: _getTabBarTheme(colorScheme),
      dataTableTheme: _getDataTableTheme(colorScheme),
      expansionTileTheme: _getExpansionTileTheme(colorScheme),
      timePickerTheme: _getTimePickerTheme(colorScheme),
      datePickerTheme: _getDatePickerTheme(colorScheme),
      pageTransitionsTheme: _getPageTransitionsTheme(),
    );
  }

  static ThemeData getDarkTheme(BuildContext context) {
    final dynamicThemeProvider =
        Provider.of<DynamicThemeProvider>(context, listen: false);
    final themeProvider = Provider.of<ThemeProvider>(context, listen: false);

    // Get color scheme (dynamic or default)
    final colorScheme = dynamicThemeProvider.getCurrentColorScheme(true);

    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.dark,
      colorScheme: colorScheme,
      textTheme: _getTextTheme(colorScheme),
      appBarTheme: _getAppBarTheme(colorScheme),
      cardTheme: _getCardTheme(colorScheme),
      elevatedButtonTheme: _getElevatedButtonTheme(colorScheme),
      outlinedButtonTheme: _getOutlinedButtonTheme(colorScheme),
      textButtonTheme: _getTextButtonTheme(colorScheme),
      inputDecorationTheme: _getInputDecorationTheme(colorScheme),
      bottomNavigationBarTheme: _getBottomNavigationBarTheme(colorScheme),
      floatingActionButtonTheme: _getFloatingActionButtonTheme(colorScheme),
      dividerTheme: _getDividerTheme(colorScheme),
      iconTheme: _getIconTheme(colorScheme),
      chipTheme: _getChipTheme(colorScheme),
      switchTheme: _getSwitchTheme(colorScheme),
      checkboxTheme: _getCheckboxTheme(colorScheme),
      radioTheme: _getRadioTheme(colorScheme),
      sliderTheme: _getSliderTheme(colorScheme),
      progressIndicatorTheme: _getProgressIndicatorTheme(colorScheme),
      snackBarTheme: _getSnackBarTheme(colorScheme),
      dialogTheme: _getDialogTheme(colorScheme),
      bottomSheetTheme: _getBottomSheetTheme(colorScheme),
      tooltipTheme: _getTooltipTheme(colorScheme),
      popupMenuTheme: _getPopupMenuTheme(colorScheme),
      drawerTheme: _getDrawerTheme(colorScheme),
      listTileTheme: _getListTileTheme(colorScheme),
      tabBarTheme: _getTabBarTheme(colorScheme),
      dataTableTheme: _getDataTableTheme(colorScheme),
      expansionTileTheme: _getExpansionTileTheme(colorScheme),
      timePickerTheme: _getTimePickerTheme(colorScheme),
      datePickerTheme: _getDatePickerTheme(colorScheme),
      pageTransitionsTheme: _getPageTransitionsTheme(),
    );
  }

  // Get theme based on current mode

  // Get theme based on current mode
  static ThemeData getTheme(BuildContext context) {
    final themeProvider = Provider.of<ThemeProvider>(context, listen: false);
    final dynamicThemeProvider =
        Provider.of<DynamicThemeProvider>(context, listen: false);

    // Force rebuild when dynamic theme changes
    dynamicThemeProvider.isUsingDynamicTheme;

    return themeProvider.isDarkMode
        ? getDarkTheme(context)
        : getLightTheme(context);
  }

  // Text Theme
  static TextTheme _getTextTheme(ColorScheme colorScheme) {
    return TextTheme(
      displayLarge: TextStyle(
        fontSize: 57,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: -0.25,
      ),
      displayMedium: TextStyle(
        fontSize: 45,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0,
      ),
      displaySmall: TextStyle(
        fontSize: 36,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0,
      ),
      headlineLarge: TextStyle(
        fontSize: 32,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0,
      ),
      headlineMedium: TextStyle(
        fontSize: 28,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0,
      ),
      headlineSmall: TextStyle(
        fontSize: 24,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0,
      ),
      titleLarge: TextStyle(
        fontSize: 22,
        fontWeight: FontWeight.w500,
        color: colorScheme.onSurface,
        letterSpacing: 0,
      ),
      titleMedium: TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.w500,
        color: colorScheme.onSurface,
        letterSpacing: 0.15,
      ),
      titleSmall: TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.w500,
        color: colorScheme.onSurface,
        letterSpacing: 0.1,
      ),
      bodyLarge: TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0.5,
      ),
      bodyMedium: TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0.25,
      ),
      bodySmall: TextStyle(
        fontSize: 12,
        fontWeight: FontWeight.w400,
        color: colorScheme.onSurface,
        letterSpacing: 0.4,
      ),
      labelLarge: TextStyle(
        fontSize: 14,
        fontWeight: FontWeight.w500,
        color: colorScheme.onSurface,
        letterSpacing: 0.1,
      ),
      labelMedium: TextStyle(
        fontSize: 12,
        fontWeight: FontWeight.w500,
        color: colorScheme.onSurface,
        letterSpacing: 0.5,
      ),
      labelSmall: TextStyle(
        fontSize: 11,
        fontWeight: FontWeight.w500,
        color: colorScheme.onSurface,
        letterSpacing: 0.5,
      ),
    );
  }

  // AppBar Theme
  static AppBarTheme _getAppBarTheme(ColorScheme colorScheme) {
    return AppBarTheme(
      backgroundColor: colorScheme.surface,
      foregroundColor: colorScheme.onSurface,
      elevation: 0,
      centerTitle: true,
      titleTextStyle: TextStyle(
        color: colorScheme.onSurface,
        fontSize: 20,
        fontWeight: FontWeight.w600,
      ),
      iconTheme: IconThemeData(
        color: colorScheme.onSurface,
        size: 24,
      ),
      actionsIconTheme: IconThemeData(
        color: colorScheme.onSurface,
        size: 24,
      ),
    );
  }

  // Card Theme
  static CardThemeData _getCardTheme(ColorScheme colorScheme) {
    return CardThemeData(
      color: colorScheme.surface,
      elevation: 2,
      shadowColor: colorScheme.shadow,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
      margin: const EdgeInsets.all(8),
    );
  }

  // Elevated Button Theme
  static ElevatedButtonThemeData _getElevatedButtonTheme(
      ColorScheme colorScheme) {
    return ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: colorScheme.primary,
        foregroundColor: colorScheme.onPrimary,
        elevation: 2,
        shadowColor: colorScheme.shadow,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
        textStyle: const TextStyle(
          fontSize: 16,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

  // Outlined Button Theme
  static OutlinedButtonThemeData _getOutlinedButtonTheme(
      ColorScheme colorScheme) {
    return OutlinedButtonThemeData(
      style: OutlinedButton.styleFrom(
        foregroundColor: colorScheme.primary,
        side: BorderSide(color: colorScheme.outline, width: 1),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
        textStyle: const TextStyle(
          fontSize: 16,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

  // Text Button Theme
  static TextButtonThemeData _getTextButtonTheme(ColorScheme colorScheme) {
    return TextButtonThemeData(
      style: TextButton.styleFrom(
        foregroundColor: colorScheme.primary,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
        textStyle: const TextStyle(
          fontSize: 16,
          fontWeight: FontWeight.w600,
        ),
      ),
    );
  }

  // Input Decoration Theme
  static InputDecorationTheme _getInputDecorationTheme(
      ColorScheme colorScheme) {
    return InputDecorationTheme(
      filled: true,
      fillColor: colorScheme.surfaceVariant,
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: colorScheme.outline),
      ),
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: colorScheme.outline),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: colorScheme.primary, width: 2),
      ),
      errorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: colorScheme.error),
      ),
      focusedErrorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(12),
        borderSide: BorderSide(color: colorScheme.error, width: 2),
      ),
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
      labelStyle: TextStyle(
        color: colorScheme.onSurfaceVariant,
        fontSize: 16,
      ),
      hintStyle: TextStyle(
        color: colorScheme.onSurfaceVariant,
        fontSize: 16,
      ),
    );
  }

  // Bottom Navigation Bar Theme
  static BottomNavigationBarThemeData _getBottomNavigationBarTheme(
      ColorScheme colorScheme) {
    return BottomNavigationBarThemeData(
      backgroundColor: colorScheme.surface,
      selectedItemColor: colorScheme.primary,
      unselectedItemColor: colorScheme.onSurfaceVariant,
      type: BottomNavigationBarType.fixed,
      elevation: 8,
    );
  }

  // Floating Action Button Theme
  static FloatingActionButtonThemeData _getFloatingActionButtonTheme(
      ColorScheme colorScheme) {
    return FloatingActionButtonThemeData(
      backgroundColor: colorScheme.primary,
      foregroundColor: colorScheme.onPrimary,
      elevation: 6,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
    );
  }

  // Divider Theme
  static DividerThemeData _getDividerTheme(ColorScheme colorScheme) {
    return DividerThemeData(
      color: colorScheme.outlineVariant,
      thickness: 1,
      space: 1,
    );
  }

  // Icon Theme
  static IconThemeData _getIconTheme(ColorScheme colorScheme) {
    return IconThemeData(
      color: colorScheme.onSurface,
      size: 24,
    );
  }

  // Chip Theme
  static ChipThemeData _getChipTheme(ColorScheme colorScheme) {
    return ChipThemeData(
      backgroundColor: colorScheme.surfaceVariant,
      selectedColor: colorScheme.primaryContainer,
      labelStyle: TextStyle(
        color: colorScheme.onSurface,
        fontSize: 14,
      ),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20),
      ),
    );
  }

  // Switch Theme
  // Switch Theme
  static SwitchThemeData _getSwitchTheme(ColorScheme colorScheme) {
    return SwitchThemeData(
      thumbColor: MaterialStateProperty.all<Color?>(colorScheme.primary),
      trackColor: MaterialStateProperty.all<Color?>(colorScheme.surfaceVariant),
    );
  }

  // Checkbox Theme
  // Checkbox Theme
  static CheckboxThemeData _getCheckboxTheme(ColorScheme colorScheme) {
    return CheckboxThemeData(
      fillColor: MaterialStateProperty.resolveWith<Color?>((states) {
        if (states.contains(MaterialState.selected)) {
          return colorScheme.primary;
        }
        return colorScheme.surfaceVariant;
      }),
      checkColor: MaterialStateProperty.all<Color?>(colorScheme.onPrimary),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(4),
      ),
    );
  }

  // Radio Theme
  static RadioThemeData _getRadioTheme(ColorScheme colorScheme) {
    return RadioThemeData(
      fillColor: MaterialStateProperty.resolveWith<Color?>((states) {
        if (states.contains(MaterialState.selected)) {
          return colorScheme.primary;
        }
        return colorScheme.surfaceVariant;
      }),
    );
  }

  // Slider Theme
  static SliderThemeData _getSliderTheme(ColorScheme colorScheme) {
    return SliderThemeData(
      activeTrackColor: colorScheme.primary,
      inactiveTrackColor: colorScheme.surfaceVariant,
      thumbColor: colorScheme.primary,
      overlayColor: colorScheme.primary.withOpacity(0.2),
    );
  }

  // Progress Indicator Theme
  static ProgressIndicatorThemeData _getProgressIndicatorTheme(
      ColorScheme colorScheme) {
    return ProgressIndicatorThemeData(
      color: colorScheme.primary,
      linearTrackColor: colorScheme.surfaceVariant,
    );
  }

  // SnackBar Theme
  static SnackBarThemeData _getSnackBarTheme(ColorScheme colorScheme) {
    return SnackBarThemeData(
      backgroundColor: colorScheme.surface,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
    );
  }

  // Dialog Theme

  static DialogThemeData _getDialogTheme(ColorScheme colorScheme) {
    return DialogThemeData(
      backgroundColor: colorScheme.surface,
      elevation: 24,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
    );
  }

  // Bottom Sheet Theme
  static BottomSheetThemeData _getBottomSheetTheme(ColorScheme colorScheme) {
    return BottomSheetThemeData(
      backgroundColor: colorScheme.surface,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
    );
  }

  // Tooltip Theme
  static TooltipThemeData _getTooltipTheme(ColorScheme colorScheme) {
    return TooltipThemeData(
      decoration: BoxDecoration(
        color: colorScheme.surface,
        borderRadius: BorderRadius.circular(8),
      ),
      textStyle: TextStyle(
        color: colorScheme.onSurface,
        fontSize: 14,
      ),
    );
  }

  // Popup Menu Theme
  static PopupMenuThemeData _getPopupMenuTheme(ColorScheme colorScheme) {
    return PopupMenuThemeData(
      color: colorScheme.surface,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
    );
  }

  // Drawer Theme
  static DrawerThemeData _getDrawerTheme(ColorScheme colorScheme) {
    return DrawerThemeData(
      backgroundColor: colorScheme.surface,
      elevation: 16,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.horizontal(right: Radius.circular(16)),
      ),
    );
  }

  // List Tile Theme
  static ListTileThemeData _getListTileTheme(ColorScheme colorScheme) {
    return ListTileThemeData(
      tileColor: colorScheme.surface,
      textColor: colorScheme.onSurface,
      iconColor: colorScheme.onSurfaceVariant,
      // shape: RoundedRectangleBorder(
      //   borderRadius: BorderRadius.circular(12),
      // ),
    );
  }

  // Tab Bar Theme
  // Tab Bar Theme
  static TabBarThemeData _getTabBarTheme(ColorScheme colorScheme) {
    return TabBarThemeData(
      labelColor: colorScheme.primary,
      unselectedLabelColor: colorScheme.onSurfaceVariant,
      indicatorColor: colorScheme.primary,
    );
  }

  // Data Table Theme
  static DataTableThemeData _getDataTableTheme(ColorScheme colorScheme) {
    return DataTableThemeData(
      dataTextStyle: TextStyle(
        color: colorScheme.onSurface,
        fontSize: 14,
      ),
      headingTextStyle: TextStyle(
        color: colorScheme.onSurface,
        fontSize: 14,
        fontWeight: FontWeight.w600,
      ),
      dividerThickness: 1,
      dataRowColor: MaterialStateProperty.all<Color?>(colorScheme.surface),
      headingRowColor:
          MaterialStateProperty.all<Color?>(colorScheme.surfaceVariant),
    );
  }

  // Expansion Tile Theme
  static ExpansionTileThemeData _getExpansionTileTheme(
      ColorScheme colorScheme) {
    return ExpansionTileThemeData(
      backgroundColor: colorScheme.surface,
      collapsedBackgroundColor: colorScheme.surfaceVariant,
      textColor: colorScheme.onSurface,
      collapsedTextColor: colorScheme.onSurfaceVariant,
      iconColor: colorScheme.primary,
      collapsedIconColor: colorScheme.onSurfaceVariant,
    );
  }

  // Time Picker Theme
  static TimePickerThemeData _getTimePickerTheme(ColorScheme colorScheme) {
    return TimePickerThemeData(
      backgroundColor: colorScheme.surface,
      hourMinuteTextColor: colorScheme.onSurface,
      hourMinuteColor: colorScheme.surfaceVariant,
      dayPeriodTextColor: colorScheme.onSurface,
      dayPeriodColor: colorScheme.surfaceVariant,
      dialHandColor: colorScheme.primary,
      dialBackgroundColor: colorScheme.surfaceVariant,
      dialTextColor: colorScheme.onSurface,
      entryModeIconColor: colorScheme.onSurfaceVariant,
    );
  }

  // Date Picker Theme
  static DatePickerThemeData _getDatePickerTheme(ColorScheme colorScheme) {
    return DatePickerThemeData(
      backgroundColor: colorScheme.surface,
      headerBackgroundColor: colorScheme.primary,
      headerForegroundColor: colorScheme.onPrimary,
      dayForegroundColor:
          MaterialStateProperty.all<Color?>(colorScheme.onSurface),
      dayBackgroundColor:
          MaterialStateProperty.all<Color?>(colorScheme.surface),
      todayForegroundColor:
          MaterialStateProperty.all<Color?>(colorScheme.primary),
      todayBackgroundColor:
          MaterialStateProperty.all<Color?>(colorScheme.primaryContainer),
      yearForegroundColor:
          MaterialStateProperty.all<Color?>(colorScheme.onSurface),
      yearBackgroundColor:
          MaterialStateProperty.all<Color?>(colorScheme.surface),
    );
  }

  // Page Transitions Theme
  static PageTransitionsTheme _getPageTransitionsTheme() {
    return const PageTransitionsTheme(
      builders: {
        TargetPlatform.android: CupertinoPageTransitionsBuilder(),
        TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
        TargetPlatform.windows: CupertinoPageTransitionsBuilder(),
        TargetPlatform.macOS: CupertinoPageTransitionsBuilder(),
        TargetPlatform.linux: CupertinoPageTransitionsBuilder(),
      },
    );
  }
}
