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

/// Animated 3D rings that orbit around the child widget — holographic feel.
class OrbitRings extends StatefulWidget {
  final Widget child;
  final double radius;
  final Color? ringColor;
  const OrbitRings({
    super.key,
    required this.child,
    this.radius = 160,
    this.ringColor,
  });

  @override
  State<OrbitRings> createState() => _OrbitRingsState();
}

class _OrbitRingsState extends State<OrbitRings>
    with SingleTickerProviderStateMixin {
  late AnimationController _ctrl;

  @override
  void initState() {
    super.initState();
    _ctrl = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 8),
    )..repeat();
  }

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

  @override
  Widget build(BuildContext context) {
    final color = widget.ringColor ?? AppColors.royalBlue;
    return AnimatedBuilder(
      animation: _ctrl,
      builder: (context, _) {
        return CustomPaint(
          painter: _OrbitPainter(
            t: _ctrl.value,
            radius: widget.radius,
            color: color,
          ),
          child: widget.child,
        );
      },
    );
  }
}

class _OrbitPainter extends CustomPainter {
  final double t;
  final double radius;
  final Color color;

  _OrbitPainter({
    required this.t,
    required this.radius,
    required this.color,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final cx = size.width / 2;
    final cy = size.height / 2;

    canvas.save();
    canvas.translate(cx, cy);

    // Ring 1 — tilted, rotating
    _drawRing(canvas, t * 2 * pi, 0.75, radius, color.withValues(alpha: 0.3), 1.8);

    // Ring 2 — opposite tilt
    _drawRing(canvas, t * 2 * pi + pi / 3, 0.6, radius * 0.85, color.withValues(alpha: 0.2), 1.2);

    // Ring 3 — gold accent, slower
    _drawRing(canvas, t * pi, 0.5, radius * 1.1, AppColors.gold.withValues(alpha: 0.15), 1.5);

    // Orbiting dots on ring 1
    _drawOrbitDots(canvas, t * 2 * pi, 0.75, radius, color);

    canvas.restore();
  }

  void _drawRing(Canvas canvas, double angle, double tiltFactor,
      double r, Color c, double strokeW) {
    canvas.save();

    // 3D tilt by scaling Y
    final tiltY = cos(angle) * tiltFactor;
    canvas.scale(1.0, tiltY.abs().clamp(0.2, 1.0));
    canvas.rotate(angle * 0.5);

    final paint = Paint()
      ..color = c
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeW
      ..maskFilter = MaskFilter.blur(BlurStyle.normal, strokeW);

    canvas.drawCircle(Offset.zero, r, paint);
    canvas.restore();
  }

  void _drawOrbitDots(Canvas canvas, double angle, double tiltFactor,
      double r, Color c) {
    for (int i = 0; i < 3; i++) {
      final theta = angle + (i * 2 * pi / 3);
      final x = cos(theta) * r;
      final y = sin(theta) * r * cos(angle) * tiltFactor;

      // Depth-based sizing — front dots bigger
      final depth = sin(theta);
      final dotSize = 3.0 + depth * 2.0;
      final alpha = (0.4 + depth * 0.4).clamp(0.1, 0.8);

      final paint = Paint()
        ..color = c.withValues(alpha: alpha)
        ..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3);
      canvas.drawCircle(Offset(x, y), dotSize, paint);

      // White core
      final core = Paint()
        ..color = Colors.white.withValues(alpha: alpha * 0.7);
      canvas.drawCircle(Offset(x, y), dotSize * 0.4, core);
    }
  }

  @override
  bool shouldRepaint(covariant _OrbitPainter old) => true;
}
