import 'package:flutter/material.dart';



class GlobalWatermark extends StatelessWidget {
  final Widget child;
  final String text;
  final TextStyle? textStyle;
  final double rowSpacing;
  final double columnSpacing;
  final double rotate;
  final Color? color;

  const GlobalWatermark({
    Key? key,
    required this.child,
    required this.text,
    this.textStyle,
    this.rowSpacing = 80,
    this.columnSpacing = 100,
    this.rotate = -0.3,
    this.color,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        child,
        Positioned.fill(
          child: IgnorePointer(
            child: RepaintBoundary(
              child: CustomPaint(
                painter: WatermarkCustomPainter(
                  text: text,
                  textStyle: textStyle ??
                      TextStyle(
                        color: color ?? const Color(0x08000000),
                        fontSize: 14,
                        fontWeight: FontWeight.normal,
                      ),
                  rowSpacing: rowSpacing,
                  columnSpacing: columnSpacing,
                  rotate: rotate,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

class WatermarkCustomPainter extends CustomPainter {
  final String text;
  final TextStyle textStyle;
  final double rowSpacing;
  final double columnSpacing;
  final double rotate;

  WatermarkCustomPainter({
    required this.text,
    required this.textStyle,
    required this.rowSpacing,
    required this.columnSpacing,
    required this.rotate,
  });

  @override
  void paint(Canvas canvas, Size size) {
    if (text.isEmpty) return;

    final textPainter = TextPainter(
      text: TextSpan(text: text, style: textStyle),
      textDirection: TextDirection.ltr,
    );
    textPainter.layout();

    final textWidth = textPainter.width;
    final textHeight = textPainter.height;

    // 计算需要绘制的行列数
    final rowCount = (size.height / rowSpacing).ceil() + 1;
    final columnCount = (size.width / columnSpacing).ceil() + 1;

    for (int i = 0; i < rowCount; i++) {
      for (int j = 0; j < columnCount; j++) {
        final offsetX = j * columnSpacing + (i.isEven ? 0 : columnSpacing / 2);
        final offsetY = i * rowSpacing;

        canvas.save();
        canvas.translate(offsetX + textWidth / 2, offsetY + textHeight / 2);
        canvas.rotate(rotate);
        canvas.translate(-textWidth / 2, -textHeight / 2);
        textPainter.paint(canvas, Offset.zero);
        canvas.restore();
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return oldDelegate is! WatermarkCustomPainter ||
        oldDelegate.text != text ||
        oldDelegate.textStyle != textStyle ||
        oldDelegate.rowSpacing != rowSpacing ||
        oldDelegate.columnSpacing != columnSpacing ||
        oldDelegate.rotate != rotate;
  }
}

// 便捷扩展方法
extension WidgetWatermarkExtension on Widget {
  Widget addWatermark({
    required String text,
    TextStyle? textStyle,
    double rowSpacing = 100,
    double columnSpacing = 100,
    double rotate = -0.3,
    Color? color = Colors.black12,
  }) {
    return GlobalWatermark(
      text: text,
      textStyle: textStyle,
      rowSpacing: rowSpacing,
      columnSpacing: columnSpacing,
      rotate: rotate,
      color: color,
      child: this,
    );
  }
}