import 'package:flutter/material.dart';
import '../../core/constants/ui_constants.dart';

class ThemeToggle extends StatefulWidget {
  final bool isDarkMode;
  final ValueChanged<bool> onThemeChanged;
  final double size;
  final Color? backgroundColor;
  final Color? iconColor;

  const ThemeToggle({
    super.key,
    required this.isDarkMode,
    required this.onThemeChanged,
    this.size = 48.0,
    this.backgroundColor,
    this.iconColor,
  });

  @override
  State<ThemeToggle> createState() => _ThemeToggleState();
}

class _ThemeToggleState extends State<ThemeToggle>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _rotationAnimation;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: UIConstants.durationNormal,
      vsync: this,
    );

    _rotationAnimation = Tween<double>(
      begin: 0.0,
      end: 0.5,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: UIConstants.curveNormal,
    ));

    _scaleAnimation = Tween<double>(
      begin: 1.0,
      end: 0.8,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: UIConstants.curveNormal,
    ));
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  void _toggleTheme() {
    _animationController.forward().then((_) {
      widget.onThemeChanged(!widget.isDarkMode);
      _animationController.reverse();
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;

    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return Transform.scale(
          scale: _scaleAnimation.value,
          child: Container(
            width: widget.size,
            height: widget.size,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: widget.backgroundColor ??
                  (widget.isDarkMode
                      ? colorScheme.primaryContainer
                      : colorScheme.surfaceVariant),
              boxShadow: [
                BoxShadow(
                  color: colorScheme.shadow.withOpacity(0.1),
                  blurRadius: 8,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                onTap: _toggleTheme,
                borderRadius: BorderRadius.circular(widget.size / 2),
                child: Center(
                  child: Transform.rotate(
                    angle: _rotationAnimation.value * 2 * 3.14159,
                    child: Icon(
                      widget.isDarkMode ? Icons.light_mode : Icons.dark_mode,
                      size: widget.size * 0.5,
                      color: widget.iconColor ??
                          (widget.isDarkMode
                              ? colorScheme.onPrimaryContainer
                              : colorScheme.onSurfaceVariant),
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
