import 'package:flutter/material.dart';

/// 绘制30s完整心电的背景方格
class GridBackgroundPainterForAll extends CustomPainter {
  final int xGridNums = 750; // 1秒25格，共30秒

  final int yGridNumsPerMv; // 多少格是一毫伏特 [暂未用到该参数]
  List<String> mLeadNames = [
    "I",
    "II",
    "III",
    "aVR",
    "aVL",
    "aVF",
    "V1",
    "V2",
    "V3",
    "V4",
    "V5",
    "V6"
  ];
  GridBackgroundPainterForAll({this.yGridNumsPerMv = 5});

  @override
  void paint(Canvas canvas, Size size) {
    final gridPaint = Paint()
      ..color = Colors.grey.withOpacity(0.5)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1.0;

    final double xGridSize = size.width / xGridNums;
    final double yGridSize = xGridSize;
    final double centerY = size.height / 2;

    /// 计算 y 轴方格数
    final int yGridNums = (size.height / yGridSize).ceil();
    for (int i = 0; i < xGridNums; i++) {
      final offsetX = i * xGridSize;
      canvas.drawLine(
        Offset(offsetX, 0),
        Offset(offsetX, size.height),
        gridPaint,
      );
    }

    for (int i = 0; i < yGridNums / 2; i++) {
      final offsetY = (i * yGridSize);
      canvas.drawLine(
        Offset(0, centerY + offsetY),
        Offset(size.width, centerY + offsetY),
        gridPaint,
      );
      canvas.drawLine(
        Offset(0, centerY - offsetY),
        Offset(size.width, centerY - offsetY),
        gridPaint,
      );
    }

    ///最后一根线往左移半个像素
    canvas.drawLine(
      Offset(size.width - 0.5, 0),
      Offset(size.width - 0.5, size.height),
      gridPaint,
    );

    for (int i = 0; i < 12; i++) {
      /// 绘制两根中心线
      final centerLinePaint = Paint()
        ..color = const Color.fromARGB(255, 1, 4, 148)
        ..style = PaintingStyle.stroke
        ..strokeWidth = 1.0;

      canvas.drawLine(
        Offset(0, size.height * i / 12),
        Offset(size.width, size.height * i / 12),
        centerLinePaint,
      );
      final textPainter = TextPainter(
        text: TextSpan(
          text: mLeadNames[i],
          style: TextStyle(
            height: 1,
            color: const Color.fromARGB(255, 99, 99, 99),
            fontSize: 25,
            background: Paint()..color = Colors.white.withOpacity(0.5),
          ),
        ),
        textDirection: TextDirection.ltr,
      );
      textPainter.layout();
      textPainter.paint(
        canvas,
        Offset(5, size.height * i / 12),
      );
    }

    /// 每隔5秒绘制一根竖粗线
    final boldLinePaint = Paint()
      ..color = Colors.grey.withOpacity(0.5)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;
    for (int i = 0; i < xGridNums; i++) {
      if (i % 25 == 0) {
        final offsetX = i * xGridSize;
        canvas.drawLine(
          Offset(offsetX, 0),
          Offset(offsetX, size.height),
          boldLinePaint,
        );
      }
      if (i % 125 == 0) {
        final offsetX = i * xGridSize;
        canvas.drawLine(
          Offset(offsetX, 0),
          Offset(offsetX, size.height),
          boldLinePaint,
        );

        /// 绘制文字标识
        final textPainter = TextPainter(
          text: TextSpan(
            text: '${i ~/ 25}秒',
            style: TextStyle(
              height: 1,
              color: const Color.fromARGB(255, 99, 99, 99),
              fontSize: 25,
              background: Paint()..color = Colors.white.withOpacity(0.5),
            ),
          ),
          textDirection: TextDirection.ltr,
        );
        textPainter.layout();
        textPainter.paint(
          canvas,
          Offset(offsetX + 5, size.height - 30),
        );
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
