import 'package:flutter/material.dart';
import '../tokens/unionflow_colors.dart';

/// Background avec motifs géométriques africains subtils
class AfricanPatternBackground extends StatelessWidget {
  final Widget child;
  final Color? patternColor;
  final double opacity;

  const AfricanPatternBackground({
    super.key,
    required this.child,
    this.patternColor,
    this.opacity = 0.03,
  });

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // Background avec motifs
        Positioned.fill(
          child: IgnorePointer(
            child: CustomPaint(
              painter: AfricanPatternPainter(
                color: (patternColor ?? UnionFlowColors.unionGreen).withOpacity(opacity),
              ),
            ),
          ),
        ),
        // Contenu
        child,
      ],
    );
  }
}

/// Painter pour dessiner les motifs africains
class AfricanPatternPainter extends CustomPainter {
  final Color color;

  AfricanPatternPainter({required this.color});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2;

    final fillPaint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    // Espacement entre les motifs
    const double spacing = 80.0;
    const double patternSize = 40.0;

    // Dessiner la grille de motifs
    for (double y = 0; y < size.height + spacing; y += spacing) {
      for (double x = 0; x < size.width + spacing; x += spacing) {
        final offset = Offset(x, y);

        // Alterner entre différents motifs
        final patternType = ((x ~/ spacing) + (y ~/ spacing)) % 3;

        switch (patternType) {
          case 0:
            _drawDiamondPattern(canvas, offset, patternSize, paint);
            break;
          case 1:
            _drawTrianglePattern(canvas, offset, patternSize, fillPaint);
            break;
          case 2:
            _drawCirclePattern(canvas, offset, patternSize, paint);
            break;
        }
      }
    }
  }

  void _drawDiamondPattern(Canvas canvas, Offset offset, double size, Paint paint) {
    final path = Path()
      ..moveTo(offset.dx, offset.dy - size / 2)
      ..lineTo(offset.dx + size / 2, offset.dy)
      ..lineTo(offset.dx, offset.dy + size / 2)
      ..lineTo(offset.dx - size / 2, offset.dy)
      ..close();
    canvas.drawPath(path, paint);
  }

  void _drawTrianglePattern(Canvas canvas, Offset offset, double size, Paint paint) {
    final path = Path()
      ..moveTo(offset.dx, offset.dy - size / 3)
      ..lineTo(offset.dx + size / 3, offset.dy + size / 3)
      ..lineTo(offset.dx - size / 3, offset.dy + size / 3)
      ..close();
    canvas.drawPath(path, paint);
  }

  void _drawCirclePattern(Canvas canvas, Offset offset, double size, Paint paint) {
    canvas.drawCircle(offset, size / 4, paint);
  }

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