import 'package:flutter/material.dart';

class EcgPainterForAll extends CustomPainter {
  /// 分为新数据和历史数据
  final List<int> allPoints;

  /// 横坐标数据量（横轴一共多少个点）
  late final int xDataCount = allPoints.length ~/ 12;

  final int yGridNumsPerMv; // 多少格是一毫伏特

  /// 纵坐标最大值
  List<double> yMaxList;

  EcgPainterForAll({
    required this.allPoints,
    required this.yMaxList,
    this.yGridNumsPerMv = 5,
  });
  List<List<int>> separateLines(List<int> array) {
    // 将数据拆分成12条线的数据
    List<List<int>> lines = List.generate(12, (_) => []);
    for (int i = 0; i < array.length; i++) {
      int lineIndex = i % 12; // 计算当前数据所属的线的索引
      lines[lineIndex].add(array[i]); // 将数据添加到对应的线中
    }
    return lines;
  }

  @override
  void paint(Canvas canvas, Size size) {
    yMaxList = [
      1500,
      7500,
      3000,
      8000,
      4000,
      12000,
      18000,
      18000,
      18000,
      18000,
      18000,
      18000,
    ];
    if (allPoints.isNotEmpty) {
      List<List<int>> ecgList = separateLines(allPoints);
      for (int i = 0; i < 12; i++) {
        double y = (size.height / 6 * (i + 0.5));

        Size currentSize = Size(size.width, y);
        print("当前:$i : ${yMaxList[i]}");
        drawPoints(
          canvas,
          currentSize,
          ecgList[i],
          yMaxList[i],
        );
      }
    }
    if (allPoints.length < xDataCount) {
      /// 绘制一根x轴直线，补全剩余数据
      final paint = Paint()
        ..color = Colors.green
        ..style = PaintingStyle.stroke
        ..strokeWidth = 2.0;
      final double xGridSize = size.width / xDataCount;
      final startX = allPoints.length * xGridSize;
      final startY = size.height / 2;
      final endX = size.width;
      final endY = size.height / 2;
      canvas.drawLine(Offset(startX, startY), Offset(endX, endY), paint);
    }
  }

  void drawPoints(
    Canvas canvas,
    Size size,
    List<int> points,
    double yMax, {
    int offset = 0,
  }) {
    final paint = Paint()
      ..color = Colors.green
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;
    final double xGridSize = size.width / xDataCount;
    final double yScale = size.height / 2 / yMax;

    final double yCenter = size.height / 2;

    final path = Path();
    for (int i = offset; i < points.length; i++) {
      if (i % 5 == 0) {
        final offsetX = i * xGridSize;
        final offsetY = -points[i] * yScale + yCenter;
        if (i == offset) {
          path.moveTo(offsetX, offsetY);
        } else {
          // 利用 cubicTo 画出平滑的曲线
          // final double preOffsetX = (i - 1) * xGridSize;
          // final double preOffsetY = -points[i - 1] * yScale + yCenter;
          // final double c1x = preOffsetX + (offsetX - preOffsetX) / 2;
          // final double c1y = preOffsetY;
          // final double c2x = preOffsetX + (offsetX - preOffsetX) / 2;
          // final double c2y = offsetY;
          // path.cubicTo(c1x, c1y, c2x, c2y, offsetX, offsetY);
          path.lineTo(offsetX, offsetY);
        }
      }
    }
    canvas.drawPath(path, paint);
  }

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