import 'package:flutter/material.dart';

class GnssReceiverIcon extends StatelessWidget {
  final double size;
  final Color? color;
  final Color? surfaceColor;

  const GnssReceiverIcon({
    super.key,
    this.size = 28,
    this.color,
    this.surfaceColor,
  });

  @override
  Widget build(BuildContext context) {
    final iconColor = color ??
        IconTheme.of(context).color ??
        Theme.of(context).colorScheme.onSurfaceVariant;

    final bgColor = surfaceColor ?? Theme.of(context).colorScheme.surface;

    return SizedBox(
      width: size,
      height: size,
      child: CustomPaint(
        painter: _GnssReceiverCustomIconPainter(
          color: iconColor,
          surfaceColor: bgColor,
        ),
      ),
    );
  }
}

class _GnssReceiverCustomIconPainter extends CustomPainter {
  final Color color;
  final Color surfaceColor;

  const _GnssReceiverCustomIconPainter({
    required this.color,
    required this.surfaceColor,
  });

  @override
  void paint(Canvas canvas, Size size) {
    canvas.save();

    // 100x100-as koordinátarendszerre rajzolunk,
    // így az ikon jól skálázható.
    canvas.scale(size.width / 100.0, size.height / 100.0);

    final mainPaint = Paint()
      ..color = color
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;

    final bandPaint = Paint()
      ..color = Color.lerp(color, Colors.black, 0.35)!
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;

    final detailPaint = Paint()
      ..color = Color.lerp(color, surfaceColor, 0.72)!
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;

    final shadowPaint = Paint()
      ..color = Color.lerp(color, Colors.black, 0.18)!
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;

    // ── fő GNSS vevőtest ────────────────────────────────

    final body = Path()
      ..moveTo(11, 34)
      ..cubicTo(11, 17, 28, 7, 50, 7)
      ..cubicTo(72, 7, 89, 17, 89, 34)
      ..cubicTo(89, 41, 85, 46, 79, 50)
      ..cubicTo(76, 52, 75, 56, 74, 62)
      ..lineTo(71, 75)
      ..cubicTo(70, 84, 63, 89, 55, 89)
      ..lineTo(45, 89)
      ..cubicTo(37, 89, 30, 84, 29, 75)
      ..lineTo(26, 62)
      ..cubicTo(25, 56, 24, 52, 21, 50)
      ..cubicTo(15, 46, 11, 41, 11, 34)
      ..close();

    canvas.drawPath(body, mainPaint);

    // ── felső sötét sáv ─────────────────────────────────

    final band = RRect.fromRectAndRadius(
      const Rect.fromLTRB(11, 29, 89, 43),
      const Radius.circular(8),
    );

    canvas.drawRRect(band, bandPaint);

    // ── kis felső kijelző / csík ─────────────────────────

    canvas.drawRRect(
      RRect.fromRectAndRadius(
        const Rect.fromLTRB(43, 34, 57, 36.5),
        const Radius.circular(1.2),
      ),
      detailPaint,
    );

    // ── középső kijelző ─────────────────────────────────

    canvas.drawRRect(
      RRect.fromRectAndRadius(
        const Rect.fromLTRB(38, 53, 62, 68),
        const Radius.circular(1.5),
      ),
      detailPaint,
    );

    // ── bal oldali kis jelzők ────────────────────────────

    _drawLed(canvas, const Rect.fromLTRB(31, 55, 34.5, 58.5), detailPaint);
    _drawLed(canvas, const Rect.fromLTRB(31, 61, 34.5, 64.5), detailPaint);
    _drawLed(canvas, const Rect.fromLTRB(31, 67, 34.5, 70.5), detailPaint);

    // ── jobb oldali kis jelzők ───────────────────────────

    _drawLed(canvas, const Rect.fromLTRB(65.5, 55, 69, 58.5), detailPaint);
    _drawLed(canvas, const Rect.fromLTRB(65.5, 61, 69, 64.5), detailPaint);
    _drawLed(canvas, const Rect.fromLTRB(65.5, 67, 69, 70.5), detailPaint);

    // ── alsó csatlakozó / bot ────────────────────────────

    canvas.drawRect(
      const Rect.fromLTRB(44, 89, 56, 98),
      shadowPaint,
    );

    canvas.drawRect(
      const Rect.fromLTRB(44, 98, 56, 118),
      mainPaint,
    );

    // csíkok a boton
    canvas.drawRect(
      const Rect.fromLTRB(44, 102, 56, 104.5),
      detailPaint..color = detailPaint.color.withOpacity(0.38),
    );

    canvas.drawRect(
      const Rect.fromLTRB(44, 109, 56, 111),
      detailPaint,
    );

    canvas.restore();
  }

  void _drawLed(Canvas canvas, Rect rect, Paint paint) {
    canvas.drawRRect(
      RRect.fromRectAndRadius(
        rect,
        const Radius.circular(0.8),
      ),
      paint,
    );
  }

  @override
  bool shouldRepaint(covariant _GnssReceiverCustomIconPainter oldDelegate) {
    return oldDelegate.color != color ||
        oldDelegate.surfaceColor != surfaceColor;
  }
}
