import 'package:flutter/material.dart';

class BellAnimatedSwitch extends StatefulWidget {
  final bool initialValue;
  final ValueChanged<bool>? onChanged;
  final Color? activeColor;
  final Color? inactiveColor;
  final Color? activeIconColor;
  final Color? inactiveIconColor;
  final Color? activeCircleColor;
  final Color? inactiveCircleColor;
  final Widget? activeIcon;
  final Widget? inactiveIcon;
  final double width;
  final double height;
  final Duration animationDuration;

  const BellAnimatedSwitch({
    super.key,
    this.initialValue = false,
    this.onChanged,
    this.activeColor,
    this.inactiveColor,
    this.activeIconColor,
    this.inactiveIconColor,
    this.activeCircleColor,
    this.inactiveCircleColor,
    this.activeIcon,
    this.inactiveIcon,
    this.width = 48,
    this.height = 30,
    this.animationDuration = const Duration(milliseconds: 300),
  });

  @override
  State<BellAnimatedSwitch> createState() => BellAnimatedSwitchState();
}

class BellAnimatedSwitchState extends State<BellAnimatedSwitch> with SingleTickerProviderStateMixin {
  late bool _isActive;
  late AnimationController _animationController;
  late Animation<Alignment> _circleAlignment;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    _isActive = widget.initialValue;
    _animationController = AnimationController(
      duration: widget.animationDuration,
      vsync: this,
    );

    _circleAlignment = AlignmentTween(begin: Alignment.centerLeft, end: Alignment.centerRight).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeInOut),
    );

    // Scale animation for tap feedback
    _scaleAnimation = Tween<double>(begin: 1.0, end: 0.9).animate(
      CurvedAnimation(
        parent: _animationController,
        curve: const Interval(0.0, 0.5, curve: Curves.easeOut),
      ),
    );

    if (_isActive) {
      _animationController.value = 1.0;
    }
  }

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

  /// Public method to set switch value programmatically without triggering onChanged callback
  void setSwitchValue(bool value, {bool animate = true}) {
    if (_isActive == value) return; // No change needed

    setState(() {
      _isActive = value;
      if (animate) {
        if (_isActive) {
          _animationController.forward();
        } else {
          _animationController.reverse();
        }
      } else {
        _animationController.value = _isActive ? 1.0 : 0.0;
      }
    });
  }

  void _toggle() {
    setState(() {
      _isActive = !_isActive;
      if (_isActive) {
        _animationController.forward();
      } else {
        _animationController.reverse();
      }
      widget.onChanged?.call(_isActive);
    });
  }

  Color _getBackgroundColor() {
    if (_isActive) {
      return widget.activeColor ?? Colors.green.shade400;
    }
    return widget.inactiveColor ?? Colors.grey.shade300;
  }

  Color _getCircleColor() {
    if (_isActive) {
      return widget.activeCircleColor ?? Colors.white;
    }
    return widget.inactiveCircleColor ?? Colors.white;
  }

  Widget _getIcon() {
    if (_isActive) {
      return widget.activeIcon ??
          Icon(
            Icons.check,
            size: 18,
            color: widget.activeIconColor ?? Colors.green.shade400,
          );
    }
    return widget.inactiveIcon ??
        Icon(
          Icons.close,
          size: 18,
          color: widget.inactiveIconColor ?? Colors.grey.shade600,
        );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _toggle,
      child: AnimatedBuilder(
        animation: _animationController,
        builder: (context, child) {
          return Transform.scale(
            scale: _scaleAnimation.value,
            child: Container(
              width: widget.width,
              height: widget.height,
              decoration: BoxDecoration(
                color: _getBackgroundColor(),
                borderRadius: BorderRadius.circular(widget.height / 2),
                // boxShadow: [
                //   BoxShadow(
                //     color: Colors.black.withAlpha(26),
                //     blurRadius: 4,
                //     offset: const Offset(0, 2),
                //   ),
                // ],
              ),
              child: Stack(
                children: [
                  // Animated circle with icon inside
                  Align(
                    alignment: _circleAlignment.value,
                    child: Container(
                      margin: const EdgeInsets.all(4),
                      width: widget.height - 8,
                      height: widget.height - 8,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: _getCircleColor(),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black.withAlpha(51),
                            blurRadius: 2,
                            offset: const Offset(0, 1),
                          ),
                        ],
                      ),
                      child: Center(child: _getIcon()),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
