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

class QuickActionButton extends StatefulWidget {
  final String label;
  final IconData icon;
  final VoidCallback? onTap;
  final Color? backgroundColor;
  final Color? iconColor;
  final Color? labelColor;
  final double? size;
  final bool isLoading;
  final bool isDisabled;

  const QuickActionButton({
    super.key,
    required this.label,
    required this.icon,
    this.onTap,
    this.backgroundColor,
    this.iconColor,
    this.labelColor,
    this.size,
    this.isLoading = false,
    this.isDisabled = false,
  });

  @override
  State<QuickActionButton> createState() => _QuickActionButtonState();
}

class _QuickActionButtonState extends State<QuickActionButton>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;
  late Animation<double> _elevationAnimation;

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

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

    _elevationAnimation = Tween<double>(
      begin: 1.0,
      end: 0.0,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: UIConstants.curveFast,
    ));
  }

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

  void _onTapDown(TapDownDetails details) {
    if (widget.onTap != null && !widget.isDisabled && !widget.isLoading) {
      _animationController.forward();
    }
  }

  void _onTapUp(TapUpDetails details) {
    if (widget.onTap != null && !widget.isDisabled && !widget.isLoading) {
      _animationController.reverse();
    }
  }

  void _onTapCancel() {
    if (widget.onTap != null && !widget.isDisabled && !widget.isLoading) {
      _animationController.reverse();
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;
    final buttonSize = widget.size ?? UIConstants.logoSizeMedium;
    final backgroundColor =
        widget.backgroundColor ?? colorScheme.primaryContainer;
    final iconColor = widget.iconColor ?? colorScheme.onPrimaryContainer;
    final labelColor = widget.labelColor ?? colorScheme.onSurface;

    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return Transform.scale(
          scale: _scaleAnimation.value,
          child: GestureDetector(
            onTapDown: _onTapDown,
            onTapUp: _onTapUp,
            onTapCancel: _onTapCancel,
            child: Container(
              width: buttonSize,
              height: buttonSize,
              decoration: BoxDecoration(
                color: backgroundColor,
                borderRadius: BorderRadius.circular(UIConstants.radius16),
                boxShadow: [
                  BoxShadow(
                    color: backgroundColor.withOpacity(0.3),
                    blurRadius: 8 * _elevationAnimation.value,
                    offset: Offset(0, 4 * _elevationAnimation.value),
                  ),
                ],
              ),
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  onTap: (widget.onTap != null &&
                          !widget.isDisabled &&
                          !widget.isLoading)
                      ? widget.onTap
                      : null,
                  borderRadius: BorderRadius.circular(UIConstants.radius16),
                  child: Padding(
                    padding: const EdgeInsets.all(UIConstants.spacing12),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        // Icon
                        if (widget.isLoading)
                          SizedBox(
                            width: buttonSize * 0.3,
                            height: buttonSize * 0.3,
                            child: CircularProgressIndicator(
                              strokeWidth: 2,
                              valueColor:
                                  AlwaysStoppedAnimation<Color>(iconColor),
                            ),
                          )
                        else
                          Icon(
                            widget.icon,
                            color: iconColor,
                            size: buttonSize * 0.4,
                          ),

                        const SizedBox(height: UIConstants.spacing8),

                        // Label
                        Text(
                          widget.label,
                          style: theme.textTheme.labelMedium?.copyWith(
                            color: labelColor,
                            fontWeight: FontWeight.w600,
                          ),
                          textAlign: TextAlign.center,
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
