import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
import '../core/theme/app_colors.dart';

class TiltCard extends StatefulWidget {
  final Widget child;
  final double borderRadius;
  final EdgeInsetsGeometry? padding;
  final bool highlighted;
  final VoidCallback? onTap;
  final double maxTilt;

  const TiltCard({
    super.key,
    required this.child,
    this.borderRadius = 20,
    this.padding,
    this.highlighted = false,
    this.onTap,
    this.maxTilt = 0.03,
  });

  @override
  State<TiltCard> createState() => _TiltCardState();
}

class _TiltCardState extends State<TiltCard> {
  double _rotateX = 0;
  double _rotateY = 0;
  double _lightX = 0.5;
  double _lightY = 0.5;
  bool _hovering = false;

  void _onPointerMove(PointerEvent event, BoxConstraints constraints) {
    final x = event.localPosition.dx / constraints.maxWidth;
    final y = event.localPosition.dy / constraints.maxHeight;
    setState(() {
      _rotateY = (x - 0.5) * widget.maxTilt;
      _rotateX = -(y - 0.5) * widget.maxTilt;
      _lightX = x;
      _lightY = y;
      _hovering = true;
    });
  }

  void _onPointerExit() {
    setState(() {
      _rotateX = 0;
      _rotateY = 0;
      _lightX = 0.5;
      _lightY = 0.5;
      _hovering = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return Listener(
          onPointerMove: (e) => _onPointerMove(e, constraints),
          onPointerUp: (_) => _onPointerExit(),
          onPointerCancel: (_) => _onPointerExit(),
          child: GestureDetector(
            onTap: widget.onTap,
            child: AnimatedContainer(
              duration: const Duration(milliseconds: 200),
              curve: Curves.easeOutCubic,
              transform: Matrix4.identity()
                ..setEntry(3, 2, 0.001)
                ..rotateX(_rotateX)
                ..rotateY(_rotateY)
                ..scale(_hovering ? 1.02 : 1.0),
              transformAlignment: Alignment.center,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(widget.borderRadius),
                child: BackdropFilter(
                  filter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),
                  child: Container(
                    padding: widget.padding ?? const EdgeInsets.all(20),
                    decoration: BoxDecoration(
                      color: Colors.white.withValues(alpha: 0.07),
                      borderRadius: BorderRadius.circular(widget.borderRadius),
                      border: Border.all(
                        color: widget.highlighted
                            ? AppColors.gold.withValues(alpha: 0.35)
                            : AppColors.glassBorder,
                      ),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withValues(alpha: _hovering ? 0.5 : 0.3),
                          blurRadius: _hovering ? 40 : 25,
                          offset: Offset(-_rotateY * 100, _rotateX * 100),
                        ),
                      ],
                    ),
                    child: Stack(
                      children: [
                        // Light reflection
                        Positioned.fill(
                          child: AnimatedOpacity(
                            duration: const Duration(milliseconds: 200),
                            opacity: _hovering ? 0.15 : 0,
                            child: DecoratedBox(
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(widget.borderRadius),
                                gradient: RadialGradient(
                                  center: Alignment(_lightX * 2 - 1, _lightY * 2 - 1),
                                  radius: 0.8,
                                  colors: [
                                    Colors.white.withValues(alpha: 0.3),
                                    Colors.transparent,
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                        widget.child,
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
