import 'package:flutter/material.dart';

class GnssReceiverOutlineIcon extends StatelessWidget {
  final double size;
  final Color? color;
  final double strokeWidth;

  const GnssReceiverOutlineIcon({
    super.key,
    this.size = 26,
    this.color,
    this.strokeWidth = 2.1,
  });

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

    return SizedBox.square(
      dimension: size,
      child: CustomPaint(
        painter: _GnssReceiverOutlineIconPainter(
          color: iconColor,
          strokeWidth: strokeWidth,
        ),
      ),
    );
  }
}

class _GnssReceiverOutlineIconPainter extends CustomPainter {
  final Color color;
  final double strokeWidth;

  const _GnssReceiverOutlineIconPainter({
    required this.color,
    required this.strokeWidth,
  });

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

    // 100 x 100 koordinátarendszerre rajzolunk,
    // így jól skálázódik 24–32 px méret között is.
    canvas.scale(size.width / 100.0, size.height / 100.0);

    final stroke = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth * 100.0 / size.width
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round
      ..isAntiAlias = true;

    final thinStroke = Paint()
      ..color = color.withOpacity(0.82)
      ..style = PaintingStyle.stroke
      ..strokeWidth = stroke.strokeWidth * 0.72
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round
      ..isAntiAlias = true;

    // ── felső GNSS fej / antenna ────────────────────────
    //
    // Kicsit hasonlít a feltöltött vevő formájára,
    // de nem kitöltött, hanem outline.
    final head = Path()
      ..moveTo(17, 34)
      ..cubicTo(17, 18, 31, 10, 50, 10)
      ..cubicTo(69, 10, 83, 18, 83, 34)
      ..cubicTo(83, 43, 76, 48, 68, 49)
      ..lineTo(32, 49)
      ..cubicTo(24, 48, 17, 43, 17, 34)
      ..close();

    canvas.drawPath(head, stroke);

    // ── alsó vevőtest ───────────────────────────────────
    final body = RRect.fromRectAndRadius(
      const Rect.fromLTRB(26, 47, 74, 76),
      const Radius.circular(8),
    );

    canvas.drawRRect(body, stroke);

    // ── kijelző ──────────────────────────────────────────
    final display = RRect.fromRectAndRadius(
      const Rect.fromLTRB(40, 56, 60, 66),
      const Radius.circular(2),
    );

    canvas.drawRRect(display, thinStroke);

    // ── felső kis jelzőcsík ──────────────────────────────
    canvas.drawLine(
      const Offset(43, 34),
      const Offset(57, 34),
      thinStroke,
    );

    // ── oldalsó gombok / státusz LED-ek ─────────────────
    canvas.drawLine(
      const Offset(33, 57),
      const Offset(33, 57),
      stroke,
    );
    canvas.drawLine(
      const Offset(33, 64),
      const Offset(33, 64),
      stroke,
    );
    canvas.drawLine(
      const Offset(67, 57),
      const Offset(67, 57),
      stroke,
    );
    canvas.drawLine(
      const Offset(67, 64),
      const Offset(67, 64),
      stroke,
    );

    // ── nyak / csatlakozó ────────────────────────────────
    canvas.drawLine(
      const Offset(50, 76),
      const Offset(50, 89),
      stroke,
    );

    // ── alsó bot / csatlakozó rövid vonallal ─────────────
    final pole = RRect.fromRectAndRadius(
      const Rect.fromLTRB(44, 86, 56, 96),
      const Radius.circular(2),
    );

    canvas.drawRRect(pole, stroke);

    // kis gyűrű a nyakon
    canvas.drawLine(
      const Offset(44, 84),
      const Offset(56, 84),
      thinStroke,
    );

    canvas.restore();
  }

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