import 'package:flutter/material.dart';

/// Renders the DLS brand logo — navy tooth + cyan chevrons.
///
/// [color] null → brand colors (#00397C tooth + #57B8CE chevrons).
/// Pass a color (e.g. Colors.white) for monochrome override on dark backgrounds.
class ToothLogo extends StatelessWidget {
  const ToothLogo({
    super.key,
    required this.size,
    this.color,
  });

  final double size;
  final Color? color;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: size * 1.9,
      height: size,
      child: CustomPaint(
        painter: _DlsLogoPainter(color: color),
      ),
    );
  }
}

class _DlsLogoPainter extends CustomPainter {
  const _DlsLogoPainter({this.color});
  final Color? color;

  static const _navy = Color(0xFF00397C);
  static const _cyan = Color(0xFF57B8CE);

  @override
  void paint(Canvas canvas, Size size) {
    final toothColor = color ?? _navy;
    final chevronColor = color ?? _cyan;

    // Content bounding box in SVG 200×200 space: x=[42.5..157.5], y=[72..133]
    // Width=115, Height=61 → aspect ~1.885 ≈ widget aspect 1.9
    const svgLeft = 42.5, svgTop = 72.0, svgWidth = 115.0, svgHeight = 61.0;
    final s = size.height / svgHeight;
    final dx = (size.width - svgWidth * s) / 2.0 - svgLeft * s;
    final dy = (size.height - svgHeight * s) / 2.0 - svgTop * s;

    canvas.translate(dx, dy);
    canvas.scale(s);

    _drawTooth(canvas, toothColor);
    _drawChevrons(canvas, chevronColor);
  }

  static void _drawTooth(Canvas canvas, Color color) {
    // SVG path with scale(0.58) + translate(100,100) applied inline.
    const cx = 100.0, cy = 100.0, sc = 0.58;
    double px(double v) => cx + v * sc;
    double py(double v) => cy + v * sc;

    final path = Path()
      ..moveTo(px(0), py(-46))
      ..cubicTo(px(-22), py(-50), px(-44), py(-38), px(-44), py(-12))
      ..cubicTo(px(-44), py(8), px(-34), py(32), px(-26), py(46))
      ..cubicTo(px(-20), py(57), px(-11), py(53), px(-8), py(33))
      ..cubicTo(px(-6), py(19), px(-2), py(17), px(0), py(17))
      ..cubicTo(px(2), py(17), px(6), py(19), px(8), py(33))
      ..cubicTo(px(11), py(53), px(20), py(57), px(26), py(46))
      ..cubicTo(px(34), py(32), px(44), py(8), px(44), py(-12))
      ..cubicTo(px(44), py(-38), px(22), py(-50), px(0), py(-46))
      ..close();

    canvas.drawPath(path, Paint()..color = color..style = PaintingStyle.fill);
  }

  static void _drawChevrons(Canvas canvas, Color color) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 11.0
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round;

    // Polyline points + translate(100,100). Left: (-52,-22)→(-34,0)→(-52,22)
    canvas.drawPath(
      Path()
        ..moveTo(48, 78)
        ..lineTo(66, 100)
        ..lineTo(48, 122),
      paint,
    );
    // Right: (52,-22)→(34,0)→(52,22)
    canvas.drawPath(
      Path()
        ..moveTo(152, 78)
        ..lineTo(134, 100)
        ..lineTo(152, 122),
      paint,
    );
  }

  @override
  bool shouldRepaint(_DlsLogoPainter old) => old.color != color;
}
