import 'package:flutter/material.dart';
import 'package:vbvs_app/common/util/FitTool.dart';

/// 枚举控制渐变方向
enum GradientDirection {
  horizontal,
  vertical,
}

class CustomCard extends StatefulWidget {
  final double borderRadius; // 圆角
  final VoidCallback onTap; // 点击回调
  final List<Color> colors; // 背景颜色列表
  final Widget child; // 子组件
  final bool enableAnimation; // 是否启用动画效果
  final bool enableGradient; // 是否启用渐变
  final GradientDirection gradientDirection; // 渐变方向
  final EdgeInsetsGeometry? margin; // 外部间距

  const CustomCard({
    Key? key,
    required this.borderRadius,
    required this.onTap,
    required this.colors,
    required this.child,
    this.enableAnimation = true,
    this.enableGradient = true,
    this.gradientDirection = GradientDirection.horizontal,
    this.margin,
  }) : super(key: key);

  @override
  State<CustomCard> createState() => _CustomCardState();
}

class _CustomCardState extends State<CustomCard>
    with SingleTickerProviderStateMixin {
  double _scale = 1.0;
  final Duration _animationDuration = const Duration(milliseconds: 50);

  Future<void> _handleTap() async {
    if (widget.enableAnimation) {
      setState(() {
        _scale = 0.95;
      });

      await Future.delayed(_animationDuration);

      setState(() {
        _scale = 1.0;
      });
    }

    widget.onTap();
  }

  @override
  Widget build(BuildContext context) {
    final bool isGradient = widget.enableGradient && widget.colors.length > 1;
    final Color baseColor = widget.colors.first;

    return Container(
      margin: widget.margin, // 应用外部间距
      child: Material(
        color: Colors.transparent,
        borderRadius: BorderRadius.circular(widget.borderRadius),
        child: InkWell(
          onTap: _handleTap,
          borderRadius: BorderRadius.circular(widget.borderRadius),
          splashColor: widget.colors.first.withOpacity(0.2),
          child: widget.enableAnimation
              ? AnimatedScale(
                  scale: _scale,
                  duration: _animationDuration,
                  curve: Curves.easeInOut,
                  child: _buildContent(isGradient, baseColor),
                )
              : _buildContent(isGradient, baseColor),
        ),
      ),
    );
  }

  Widget _buildContent(bool isGradient, Color baseColor) {
    return Container(
      decoration: BoxDecoration(
        color: isGradient ? null : baseColor,
        gradient: isGradient
            ? LinearGradient(
                colors: widget.colors,
                begin: widget.gradientDirection == GradientDirection.vertical
                    ? Alignment.topCenter
                    : Alignment.centerLeft,
                end: widget.gradientDirection == GradientDirection.vertical
                    ? Alignment.bottomCenter
                    : Alignment.centerRight,
              )
            : null,
        borderRadius: BorderRadius.circular(widget.borderRadius),
      ),
      child: Padding(
        padding: EdgeInsets.fromLTRB(0.rpx, 0.rpx, 0.rpx, 0.rpx),
        child: widget.child,
      ),
    );
  }
}
