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

class PulsingOrb extends StatefulWidget {
  final double size;
  final double amplitude;
  final bool active;

  const PulsingOrb({
    super.key,
    this.size = 200,
    this.amplitude = 0.0,
    this.active = true,
  });

  @override
  State<PulsingOrb> createState() => _PulsingOrbState();
}

class _PulsingOrbState extends State<PulsingOrb> with TickerProviderStateMixin {
  late AnimationController _pulseController;
  late AnimationController _rotateController;

  @override
  void initState() {
    super.initState();
    _pulseController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    )..repeat(reverse: true);

    _rotateController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 12),
    )..repeat();
  }

  @override
  void dispose() {
    _pulseController.dispose();
    _rotateController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: Listenable.merge([_pulseController, _rotateController]),
      builder: (context, child) {
        final pulse = _pulseController.value;
        final amp = widget.amplitude.clamp(0.0, 1.0);
        final scale = 1.0 + (pulse * 0.08) + (amp * 0.15);

        return SizedBox(
          width: widget.size,
          height: widget.size,
          child: CustomPaint(
            painter: _OrbPainter(
              pulse: pulse,
              rotation: _rotateController.value * 2 * math.pi,
              amplitude: amp,
              active: widget.active,
            ),
            child: Center(
              child: Container(
                width: widget.size * 0.5 * scale,
                height: widget.size * 0.5 * scale,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  gradient: RadialGradient(
                    colors: widget.active
                        ? [
                            AppColors.royalBlue.withValues(alpha: 0.9),
                            AppColors.royalBlue.withValues(alpha: 0.4),
                            Colors.transparent,
                          ]
                        : [
                            Colors.grey.withValues(alpha: 0.5),
                            Colors.grey.withValues(alpha: 0.2),
                            Colors.transparent,
                          ],
                  ),
                  boxShadow: widget.active
                      ? [
                          BoxShadow(
                            color: AppColors.royalBlue.withValues(alpha: 0.4 + amp * 0.3),
                            blurRadius: 40 + amp * 30,
                            spreadRadius: 5 + amp * 10,
                          ),
                        ]
                      : null,
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}

class _OrbPainter extends CustomPainter {
  final double pulse;
  final double rotation;
  final double amplitude;
  final bool active;

  _OrbPainter({
    required this.pulse,
    required this.rotation,
    required this.amplitude,
    required this.active,
  });

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

    // Concentric rings
    for (int i = 0; i < 4; i++) {
      final ringProgress = (i + 1) / 4;
      final radius = maxR * ringProgress * (1.0 + pulse * 0.03 * (i + 1));
      final paint = Paint()
        ..color = (active ? AppColors.royalBlue : Colors.grey)
            .withValues(alpha: (0.15 - i * 0.03) + amplitude * 0.1)
        ..style = PaintingStyle.stroke
        ..strokeWidth = 1.5;

      canvas.save();
      canvas.translate(center.dx, center.dy);
      canvas.rotate(rotation + i * 0.5);
      canvas.translate(-center.dx, -center.dy);
      canvas.drawCircle(center, radius, paint);
      canvas.restore();
    }

    // Orbiting dots
    if (active) {
      for (int i = 0; i < 6; i++) {
        final angle = rotation * (1 + i * 0.1) + i * (math.pi / 3);
        final orbitR = maxR * (0.4 + i * 0.08);
        final dx = center.dx + math.cos(angle) * orbitR;
        final dy = center.dy + math.sin(angle) * orbitR;
        final dotPaint = Paint()
          ..color = AppColors.gold.withValues(alpha: 0.6 + amplitude * 0.3);
        canvas.drawCircle(Offset(dx, dy), 2.5 + amplitude * 2, dotPaint);
      }
    }
  }

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