import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'theme_notifier.dart';

/// Seasonal theme system for ZenTap
enum SeasonalTheme {
  default_('default'),
  spring('spring'),
  summer('summer'),
  autumn('autumn'),
  winter('winter');

  const SeasonalTheme(this.id);
  final String id;

  static SeasonalTheme fromId(String id) {
    return SeasonalTheme.values.firstWhere(
      (theme) => theme.id == id,
      orElse: () => SeasonalTheme.default_,
    );
  }
}

/// Theme color scheme for each season
class SeasonalColors {
  final Color appBackground;
  final Color secondaryBackground;
  final Color primaryText;
  final Color secondaryText;
  final Color mutedText;
  final Color buttonBackground;
  final Color buttonText;
  final Color uiElements;
  final Color gameBoardBorder;
  final Color bubbleDefault;
  final Color bubblePopEffect;
  final Color scoreText;
  final Color timerText;
  final Color links;
  final Color linkHover;
  final Color selectedMenuItem;
  
  // Animation colors
  final List<Color> animationLayer1;
  final List<Color> animationLayer2;
  final List<Color> animationLayer3;
  final List<Color> zenModeColors;

  const SeasonalColors({
    required this.appBackground,
    required this.secondaryBackground,
    required this.primaryText,
    required this.secondaryText,
    required this.mutedText,
    required this.buttonBackground,
    required this.buttonText,
    required this.uiElements,
    required this.gameBoardBorder,
    required this.bubbleDefault,
    required this.bubblePopEffect,
    required this.scoreText,
    required this.timerText,
    required this.links,
    required this.linkHover,
    required this.selectedMenuItem,
    required this.animationLayer1,
    required this.animationLayer2,
    required this.animationLayer3,
    required this.zenModeColors,
  });
}

class ThemeManager {
  static const String _keySelectedTheme = 'selected_theme';
  static SharedPreferences? _prefs;
  static SeasonalTheme _currentTheme = SeasonalTheme.default_;

  static Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
    final themeId = _prefs?.getString(_keySelectedTheme) ?? 'default';
    _currentTheme = SeasonalTheme.fromId(themeId);
    ThemeNotifier().initFromManager();
  }

  static SeasonalTheme get currentTheme => _currentTheme;

  static Future<void> setTheme(SeasonalTheme theme) async {
    _currentTheme = theme;
    await _prefs?.setString(_keySelectedTheme, theme.id);
    ThemeNotifier().setTheme(theme);
  }

  /// Get the current theme's color scheme
  static SeasonalColors get colors {
    switch (_currentTheme) {
      case SeasonalTheme.spring:
        return _springColors;
      case SeasonalTheme.summer:
        return _summerColors;
      case SeasonalTheme.autumn:
        return _autumnColors;
      case SeasonalTheme.winter:
        return _winterColors;
      case SeasonalTheme.default_:
        return _defaultColors;
    }
  }

  /// Get theme display name
  static String getThemeDisplayName(SeasonalTheme theme) {
    switch (theme) {
      case SeasonalTheme.default_:
        return 'Default (fSociety)';
      case SeasonalTheme.spring:
        return 'Spring Bloom';
      case SeasonalTheme.summer:
        return 'Summer Bright';
      case SeasonalTheme.autumn:
        return 'Autumn Leaves';
      case SeasonalTheme.winter:
        return 'Winter Frost';
    }
  }

  /// Get theme icon
  static IconData getThemeIcon(SeasonalTheme theme) {
    switch (theme) {
      case SeasonalTheme.default_:
        return Icons.contrast;
      case SeasonalTheme.spring:
        return Icons.local_florist;
      case SeasonalTheme.summer:
        return Icons.wb_sunny;
      case SeasonalTheme.autumn:
        return Icons.park;
      case SeasonalTheme.winter:
        return Icons.ac_unit;
    }
  }

  /// Default fSociety theme (current colors)
  static const SeasonalColors _defaultColors = SeasonalColors(
    appBackground: Color(0xFF000000),
    secondaryBackground: Color(0xFF0A0A0A),
    primaryText: Color(0xFFFFFFFF),
    secondaryText: Color(0xDEFFFFFF),
    mutedText: Color(0xFF808080),
    buttonBackground: Color(0xFF0BC2F9),
    buttonText: Color(0xFFFFFFFF),
    uiElements: Color(0xFF808080),
    gameBoardBorder: Color(0xFF808080),
    bubbleDefault: Color(0xFF646CFF),
    bubblePopEffect: Color(0xFFFF0000),
    scoreText: Color(0xFFFFFFFF),
    timerText: Color(0xDEFFFFFF),
    links: Color(0xFF000080),
    linkHover: Color(0xFFFF0000),
    selectedMenuItem: Color(0xFFFFFFFF),
    animationLayer1: [Color(0x33808080), Colors.transparent],
    animationLayer2: [Color(0x1AFF0000), Colors.transparent],
    animationLayer3: [Color(0x0D0BC2F9), Colors.transparent],
    zenModeColors: [Color(0x4D000080), Colors.transparent],
  );

  /// Spring theme - Fresh greens and soft pastels
  static const SeasonalColors _springColors = SeasonalColors(
    appBackground: Color(0xFF0A1A0A), // Dark forest green
    secondaryBackground: Color(0xFF1A2A1A),
    primaryText: Color(0xFFE8F5E8), // Light mint
    secondaryText: Color(0xDEE8F5E8),
    mutedText: Color(0xFF7FB069), // Soft green
    buttonBackground: Color(0xFF8FBC8F), // Dark sea green
    buttonText: Color(0xFF0A1A0A),
    uiElements: Color(0xFF556B2F), // Dark olive green
    gameBoardBorder: Color(0xFF6B8E23),
    bubbleDefault: Color(0xFF98FB98), // Pale green
    bubblePopEffect: Color(0xFFFFB6C1), // Light pink
    scoreText: Color(0xFFE8F5E8),
    timerText: Color(0xDEE8F5E8),
    links: Color(0xFF228B22), // Forest green
    linkHover: Color(0xFFFF69B4), // Hot pink
    selectedMenuItem: Color(0xFFE8F5E8),
    animationLayer1: [Color(0x3398FB98), Colors.transparent], // Pale green
    animationLayer2: [Color(0x1AFFB6C1), Colors.transparent], // Light pink
    animationLayer3: [Color(0x0D7FB069), Colors.transparent], // Soft green
    zenModeColors: [Color(0x4D228B22), Colors.transparent], // Forest green
  );

  /// Summer theme - Bright blues and warm yellows
  static const SeasonalColors _summerColors = SeasonalColors(
    appBackground: Color(0xFF0A0A2A), // Deep navy
    secondaryBackground: Color(0xFF1A1A3A),
    primaryText: Color(0xFFF0F8FF), // Alice blue
    secondaryText: Color(0xDEF0F8FF),
    mutedText: Color(0xFF87CEEB), // Sky blue
    buttonBackground: Color(0xFFFFD700), // Gold
    buttonText: Color(0xFF0A0A2A),
    uiElements: Color(0xFF4682B4), // Steel blue
    gameBoardBorder: Color(0xFF5F9EA0),
    bubbleDefault: Color(0xFF00BFFF), // Deep sky blue
    bubblePopEffect: Color(0xFFFF6347), // Tomato
    scoreText: Color(0xFFF0F8FF),
    timerText: Color(0xDEF0F8FF),
    links: Color(0xFF1E90FF), // Dodger blue
    linkHover: Color(0xFFFF4500), // Orange red
    selectedMenuItem: Color(0xFFF0F8FF),
    animationLayer1: [Color(0x3300BFFF), Colors.transparent], // Deep sky blue
    animationLayer2: [Color(0x1AFFD700), Colors.transparent], // Gold
    animationLayer3: [Color(0x0D87CEEB), Colors.transparent], // Sky blue
    zenModeColors: [Color(0x4D1E90FF), Colors.transparent], // Dodger blue
  );

  /// Autumn theme - Warm oranges, reds, and browns
  static const SeasonalColors _autumnColors = SeasonalColors(
    appBackground: Color(0xFF2A1A0A), // Dark brown
    secondaryBackground: Color(0xFF3A2A1A),
    primaryText: Color(0xFFFFF8DC), // Cornsilk
    secondaryText: Color(0xDEFFF8DC),
    mutedText: Color(0xFFDEB887), // Burlywood
    buttonBackground: Color(0xFFD2691E), // Chocolate
    buttonText: Color(0xFFFFF8DC),
    uiElements: Color(0xFF8B4513), // Saddle brown
    gameBoardBorder: Color(0xFFA0522D),
    bubbleDefault: Color(0xFFFF7F50), // Coral
    bubblePopEffect: Color(0xFFDC143C), // Crimson
    scoreText: Color(0xFFFFF8DC),
    timerText: Color(0xDEFFF8DC),
    links: Color(0xFFB22222), // Fire brick
    linkHover: Color(0xFFFF4500), // Orange red
    selectedMenuItem: Color(0xFFFFF8DC),
    animationLayer1: [Color(0x33FF7F50), Colors.transparent], // Coral
    animationLayer2: [Color(0x1ADC143C), Colors.transparent], // Crimson
    animationLayer3: [Color(0x0DDEB887), Colors.transparent], // Burlywood
    zenModeColors: [Color(0x4DB22222), Colors.transparent], // Fire brick
  );

  /// Winter theme - Cool blues and icy whites
  static const SeasonalColors _winterColors = SeasonalColors(
    appBackground: Color(0xFF0A1A2A), // Dark slate blue
    secondaryBackground: Color(0xFF1A2A3A),
    primaryText: Color(0xFFF0F8FF), // Alice blue
    secondaryText: Color(0xDEF0F8FF),
    mutedText: Color(0xFFB0C4DE), // Light steel blue
    buttonBackground: Color(0xFF6495ED), // Cornflower blue
    buttonText: Color(0xFF0A1A2A),
    uiElements: Color(0xFF708090), // Slate gray
    gameBoardBorder: Color(0xFF778899),
    bubbleDefault: Color(0xFF87CEFA), // Light sky blue
    bubblePopEffect: Color(0xFFFFFFFF), // White
    scoreText: Color(0xFFF0F8FF),
    timerText: Color(0xDEF0F8FF),
    links: Color(0xFF4169E1), // Royal blue
    linkHover: Color(0xFF00CED1), // Dark turquoise
    selectedMenuItem: Color(0xFFF0F8FF),
    animationLayer1: [Color(0x3387CEFA), Colors.transparent], // Light sky blue
    animationLayer2: [Color(0x1AFFFFFF), Colors.transparent], // White
    animationLayer3: [Color(0x0DB0C4DE), Colors.transparent], // Light steel blue
    zenModeColors: [Color(0x4D4169E1), Colors.transparent], // Royal blue
  );
}