import 'package:flutter/material.dart';

class AppTheme {
  // Coffee-themed colors
  static const Color primaryBrown = Color(0xFF8B4513);
  static const Color lightBrown = Color(0xFFD2B48C);
  static const Color darkBrown = Color(0xFF654321);
  static const Color cream = Color(0xFFF5F5DC);
  static const Color warmWhite = Color(0xFFFFFDF7);
  static const Color coffeeBrown = Color(0xFF6F4E37);
  static const Color espresso = Color(0xFF3C2415);

  // Accent colors
  static const Color accent = Color(0xFFFF8C42);
  static const Color success = Color(0xFF4CAF50);
  static const Color warning = Color(0xFFFFC107);
  static const Color error = Color(0xFFF44336);
  static const Color info = Color(0xFF2196F3);

  // Neutral colors
  static const Color grey50 = Color(0xFFFAFAFA);
  static const Color grey100 = Color(0xFFF5F5F5);
  static const Color grey200 = Color(0xFFEEEEEE);
  static const Color grey300 = Color(0xFFE0E0E0);
  static const Color grey400 = Color(0xFFBDBDBD);
  static const Color grey500 = Color(0xFF9E9E9E);
  static const Color grey600 = Color(0xFF757575);
  static const Color grey700 = Color(0xFF616161);
  static const Color grey800 = Color(0xFF424242);
  static const Color grey900 = Color(0xFF212121);

  static ThemeData get lightTheme {
    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.light,
      primarySwatch: _createMaterialColor(primaryBrown),
      primaryColor: primaryBrown,
      scaffoldBackgroundColor: warmWhite,
      fontFamily: 'Poppins', // Enable Poppins font

      colorScheme: const ColorScheme.light(
        primary: primaryBrown,
        secondary: accent,
        surface: warmWhite,
        background: cream,
        error: error,
        onPrimary: Colors.white,
        onSecondary: Colors.white,
        onSurface: grey900,
        onBackground: grey900,
        onError: Colors.white,
      ),

      pageTransitionsTheme: const PageTransitionsTheme(
        builders: {
          TargetPlatform.android: ZoomPageTransitionsBuilder(),
          TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
        },
      ),

      appBarTheme: const AppBarTheme(
        backgroundColor: primaryBrown,
        foregroundColor: Colors.white,
        elevation: 0,
        centerTitle: true,
        titleTextStyle: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 20,
          fontWeight: FontWeight.w600,
          color: Colors.white,
        ),
      ),

      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: primaryBrown,
          foregroundColor: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
          textStyle: const TextStyle(
            // fontFamily: 'Poppins', // Using system font
            fontSize: 16,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),

      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          foregroundColor: primaryBrown,
          side: const BorderSide(color: primaryBrown, width: 2),
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
          textStyle: const TextStyle(
            // fontFamily: 'Poppins', // Using system font
            fontSize: 16,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),

      textButtonTheme: TextButtonThemeData(
        style: TextButton.styleFrom(
          foregroundColor: primaryBrown,
          textStyle: const TextStyle(
            // fontFamily: 'Poppins', // Using system font
            fontSize: 16,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),

      inputDecorationTheme: InputDecorationTheme(
        filled: true,
        fillColor: grey50,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: const BorderSide(color: grey300),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: const BorderSide(color: grey300),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: const BorderSide(color: primaryBrown, width: 2),
        ),
        errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: const BorderSide(color: error),
        ),
        focusedErrorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(12),
          borderSide: const BorderSide(color: error, width: 2),
        ),
        labelStyle: const TextStyle(
          // // fontFamily: 'Poppins', // Using system font
          color: grey600,
        ),
        hintStyle: const TextStyle(
          // // fontFamily: 'Poppins', // Using system font
          color: grey500,
        ),
      ),

      cardTheme: CardThemeData(
        elevation: 2,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        color: Colors.white,
      ),

      bottomNavigationBarTheme: const BottomNavigationBarThemeData(
        backgroundColor: Colors.white,
        selectedItemColor: primaryBrown,
        unselectedItemColor: grey500,
        type: BottomNavigationBarType.fixed,
        elevation: 8,
      ),

      textTheme: const TextTheme(
        displayLarge: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 32,
          fontWeight: FontWeight.bold,
          color: grey900,
        ),
        displayMedium: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 28,
          fontWeight: FontWeight.bold,
          color: grey900,
        ),
        displaySmall: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 24,
          fontWeight: FontWeight.w600,
          color: grey900,
        ),
        headlineLarge: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 22,
          fontWeight: FontWeight.w600,
          color: grey900,
        ),
        headlineMedium: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 20,
          fontWeight: FontWeight.w600,
          color: grey900,
        ),
        headlineSmall: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 18,
          fontWeight: FontWeight.w600,
          color: grey900,
        ),
        titleLarge: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 16,
          fontWeight: FontWeight.w600,
          color: grey900,
        ),
        titleMedium: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: grey900,
        ),
        titleSmall: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 12,
          fontWeight: FontWeight.w500,
          color: grey900,
        ),
        bodyLarge: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 16,
          fontWeight: FontWeight.normal,
          color: grey800,
        ),
        bodyMedium: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 14,
          fontWeight: FontWeight.normal,
          color: grey800,
        ),
        bodySmall: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 12,
          fontWeight: FontWeight.normal,
          color: grey600,
        ),
        labelLarge: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: grey700,
        ),
        labelMedium: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 12,
          fontWeight: FontWeight.w500,
          color: grey700,
        ),
        labelSmall: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 10,
          fontWeight: FontWeight.w500,
          color: grey600,
        ),
      ),
    );
  }

  static ThemeData get darkTheme {
    return ThemeData(
      useMaterial3: true,
      brightness: Brightness.dark,
      primarySwatch: _createMaterialColor(lightBrown),
      primaryColor: lightBrown,
      scaffoldBackgroundColor: grey900,

      // // fontFamily: 'Poppins', // Using system font // Disabled until Poppins fonts are added
      colorScheme: const ColorScheme.dark(
        primary: lightBrown,
        secondary: accent,
        surface: grey800,
        background: grey900,
        error: error,
        onPrimary: grey900,
        onSecondary: Colors.white,
        onSurface: Colors.white,
        onBackground: Colors.white,
        onError: Colors.white,
      ),

      appBarTheme: const AppBarTheme(
        backgroundColor: grey900,
        foregroundColor: Colors.white,
        elevation: 0,
        centerTitle: true,
        titleTextStyle: TextStyle(
          // fontFamily: 'Poppins', // Using system font
          fontSize: 20,
          fontWeight: FontWeight.w600,
          color: Colors.white,
        ),
      ),

      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          backgroundColor: lightBrown,
          foregroundColor: grey900,
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
          textStyle: const TextStyle(
            // fontFamily: 'Poppins', // Using system font
            fontSize: 16,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),

      cardTheme: CardThemeData(
        elevation: 2,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        color: grey800,
      ),
    );
  }

  static MaterialColor _createMaterialColor(Color color) {
    List strengths = <double>[.05];
    Map<int, Color> swatch = {};
    final int r = color.red, g = color.green, b = color.blue;

    for (int i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }
    for (var strength in strengths) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        r + ((ds < 0 ? r : (255 - r)) * ds).round(),
        g + ((ds < 0 ? g : (255 - g)) * ds).round(),
        b + ((ds < 0 ? b : (255 - b)) * ds).round(),
        1,
      );
    }
    return MaterialColor(color.value, swatch);
  }
}
