import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/urm_chart_panel/widgets/statistical_chart.dart';

class ChartPainterWidget extends StatelessWidget {
  final List<Point<double>> points;
  final List<PeakPoint> peakPoints;
  final List<double> xAxises;
  final List<double> yAxises;
  final Color lineColor;

  ChartPainterWidget({
    required this.points,
    required this.peakPoints,
    required this.xAxises,
    required this.yAxises,
    this.lineColor = Colors.yellow,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(double.infinity, 200),
      painter: StatisticalChart(
        points: points,
        peakPoints: peakPoints,
        xAxises: xAxises,
        yAxises: yAxises,
        lineColor: lineColor,
      ),
    );
  }
}

class StatisticalChart extends CustomPainter {
  final List<Point<double>> points;
  final List<PeakPoint> peakPoints;
  final List<double> xAxises;
  final List<double> yAxises;
  final Color lineColor;

  StatisticalChart({
    required this.points,
    required this.peakPoints,
    required this.xAxises,
    required this.yAxises,
    this.lineColor = Colors.yellow,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = lineColor
      ..strokeWidth = 1.0;

    final Paint gridPaint = Paint()
      ..color = Colors.yellow
      ..style = PaintingStyle.stroke;

    final double horizontalMargin = 30.0; // 左右边距
    final double verticalMargin = 20.0; // 上下边距
    final double chartWidth = size.width - horizontalMargin * 2;
    final double chartHeight = size.height - verticalMargin * 2;

    if (xAxises.isEmpty || yAxises.isEmpty) {
      return;
    }
    // 绘制背景网格
    int xGridCount = xAxises.length - 1;
    int yGridCount = yAxises.length - 1;
    double horizontalSpacing = chartWidth / xGridCount;
    double verticalSpacing = chartHeight / yGridCount;
    double xRange = xAxises.last - xAxises.first;
    // double yRange = yAxises.last - yAxises.first; // 暂时没用到，因为 y 轴的范围目前都是 1

    for (int i = 0; i <= xGridCount; i++) {
      double dx = horizontalMargin + i * horizontalSpacing;
      canvas.drawLine(Offset(dx, verticalMargin),
          Offset(dx, size.height - verticalMargin), gridPaint);
    }
    for (int i = 0; i <= yGridCount; i++) {
      double dy = verticalMargin + i * verticalSpacing;
      canvas.drawLine(Offset(horizontalMargin, dy),
          Offset(size.width - horizontalMargin, dy), gridPaint);
    }

    // 画数据线
    for (int i = 0; i < points.length - 1; i++) {
      final p1 = Offset(horizontalMargin + points[i].x / xRange * chartWidth,
          size.height - verticalMargin - points[i].y * chartHeight);
      final p2 = Offset(
          horizontalMargin + points[i + 1].x / xRange * chartWidth,
          size.height - verticalMargin - points[i + 1].y * chartHeight);
      canvas.drawLine(p1, p2, paint);
    }

    final Paint maxPaint = Paint()..color = Colors.red;
    final Paint minPaint = Paint()..color = Colors.purple;
    final Paint normalPaint = Paint()..color = Colors.blue;
    final TextStyle textStyle = TextStyle(color: Colors.yellow, fontSize: 12);
    for (PeakPoint peakPoint in peakPoints) {
      if (peakPoint.pointIndex < 0 || peakPoint.pointIndex >= points.length) {
        continue;
      }
      double xPos = points[peakPoint.pointIndex].x;
      double yPos = points[peakPoint.pointIndex].y;
      Offset offset = Offset(horizontalMargin + xPos / xRange * chartWidth,
          size.height - verticalMargin - yPos * chartHeight);
      switch (peakPoint.type) {
        case PeakType.normal:
          canvas.drawCircle(offset, 4.0, normalPaint);
          break;
        case PeakType.maximum:
          canvas.drawCircle(offset, 4.0, maxPaint);
          break;
        case PeakType.minimum:
          canvas.drawCircle(offset, 4.0, minPaint);
          break;
        default:
      }
    }

    // 绘制 y 轴标识
    for (int i = 0; i < yAxises.length; i++) {
      if (yAxises[i].isNaN) {
        continue;
      }
      final textSpan = TextSpan(text: yAxises[i].toString(), style: textStyle);
      final textPainter = TextPainter(
        text: textSpan,
        textAlign: TextAlign.right,
        textDirection: TextDirection.ltr,
      )..layout(minWidth: 0, maxWidth: horizontalMargin - 5);
      final dx = horizontalMargin - textPainter.width - 5;
      final dy = size.height -
          verticalMargin -
          i * verticalSpacing -
          textPainter.height / 2;
      textPainter.paint(canvas, Offset(dx, dy));
    }

    // 绘制 x 轴标识
    for (int i = 0; i < xAxises.length; i++) {
      final textSpan = TextSpan(
          text: xAxises[i].toStringAsFixed(2).replaceAll(".00", ""),
          style: textStyle);
      final textPainter = TextPainter(
        text: textSpan,
        textAlign: TextAlign.center,
        textDirection: TextDirection.ltr,
      )..layout(minWidth: 0, maxWidth: horizontalSpacing);
      final dx =
          horizontalMargin + i * horizontalSpacing - textPainter.width / 2;
      textPainter.paint(canvas, Offset(dx, size.height - textPainter.height));
    }

    // 绘制单位标识
    final unitTextSpan = TextSpan(text: 'mm', style: textStyle);
    final unitTextPainter = TextPainter(
      text: unitTextSpan,
      textAlign: TextAlign.right,
      textDirection: TextDirection.ltr,
    )..layout(minWidth: 0, maxWidth: horizontalSpacing);
    unitTextPainter.paint(
        canvas,
        Offset(
            size.width - horizontalMargin + 2, size.height - verticalSpacing));
  }

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