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

class ScannerAnimation extends StatefulWidget {
  final double height;

  const ScannerAnimation({super.key, this.height = 200});

  @override
  State<ScannerAnimation> createState() => _ScannerAnimationState();
}

class _ScannerAnimationState extends State<ScannerAnimation>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

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

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

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.height,
      child: AnimatedBuilder(
        animation: _controller,
        builder: (context, child) {
          return CustomPaint(
            size: Size(double.infinity, widget.height),
            painter: _ScanPainter(progress: _controller.value),
          );
        },
      ),
    );
  }
}

class _ScanPainter extends CustomPainter {
  final double progress;
  _ScanPainter({required this.progress});

  @override
  void paint(Canvas canvas, Size size) {
    final y = size.height * progress;

    // Scan line
    final linePaint = Paint()
      ..shader = LinearGradient(
        colors: [
          Colors.transparent,
          AppColors.royalBlue.withValues(alpha: 0.8),
          AppColors.gold.withValues(alpha: 0.6),
          AppColors.royalBlue.withValues(alpha: 0.8),
          Colors.transparent,
        ],
      ).createShader(Rect.fromLTWH(0, y - 1, size.width, 2));
    canvas.drawRect(Rect.fromLTWH(0, y - 1, size.width, 2), linePaint);

    // Glow area above scan line
    final glowPaint = Paint()
      ..shader = LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          Colors.transparent,
          AppColors.royalBlue.withValues(alpha: 0.05),
          AppColors.royalBlue.withValues(alpha: 0.15),
        ],
      ).createShader(Rect.fromLTWH(0, math.max(0, y - 60), size.width, 60));
    canvas.drawRect(Rect.fromLTWH(0, math.max(0, y - 60), size.width, 60), glowPaint);

    // Corner brackets
    _drawCorner(canvas, size, 0, 0, 1, 1);
    _drawCorner(canvas, size, size.width, 0, -1, 1);
    _drawCorner(canvas, size, 0, size.height, 1, -1);
    _drawCorner(canvas, size, size.width, size.height, -1, -1);
  }

  void _drawCorner(Canvas canvas, Size size, double x, double y, double dx, double dy) {
    final len = 20.0;
    final paint = Paint()
      ..color = AppColors.gold.withValues(alpha: 0.7)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2
      ..strokeCap = StrokeCap.round;

    canvas.drawLine(Offset(x, y), Offset(x + len * dx, y), paint);
    canvas.drawLine(Offset(x, y), Offset(x, y + len * dy), paint);
  }

  @override
  bool shouldRepaint(covariant _ScanPainter old) => old.progress != progress;
}
