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

class LeafPatternPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final random = math.Random(42); // Fixed seed for consistent pattern
    final leafCount = (size.width * size.height / 15000).round().clamp(10, 50);

    for (int i = 0; i < leafCount; i++) {
      final x = random.nextDouble() * size.width;
      final y = random.nextDouble() * size.height;
      final rotation = random.nextDouble() * 2 * math.pi;
      final scale = random.nextDouble() * 0.5 + 0.5; // 0.5 to 1.0
      final opacity = (random.nextDouble() * 0.15 + 0.05).clamp(
        0.05,
        0.2,
      ); // 0.05 to 0.2

      canvas.save();
      canvas.translate(x, y);
      canvas.rotate(rotation);
      canvas.scale(scale);

      _drawLeaf(canvas, opacity);

      canvas.restore();
    }
  }

  void _drawLeaf(Canvas canvas, double opacity) {
    final paint =
        Paint()
          ..color = Colors.green.shade800.withOpacity(opacity)
          ..style = PaintingStyle.fill
          ..strokeWidth = 1.0;

    final path = Path();

    // Leaf shape
    path.moveTo(0, 0);
    path.cubicTo(5, -10, 10, -15, 20, -20);
    path.cubicTo(10, -10, 5, -5, 0, 0);
    path.cubicTo(-5, -5, -10, -10, -20, -20);
    path.cubicTo(-10, -15, -5, -10, 0, 0);
    path.close();

    // Draw leaf
    canvas.drawPath(path, paint);

    // Draw vein
    paint.color = Colors.green.shade800.withOpacity(opacity * 1.5);
    paint.style = PaintingStyle.stroke;
    paint.strokeWidth = 0.5;

    final veinPath = Path();
    veinPath.moveTo(0, 0);
    veinPath.lineTo(0, -15);

    canvas.drawPath(veinPath, paint);
  }

  @override
  bool shouldRepaint(LeafPatternPainter oldDelegate) => false;
}
