import 'dart:math';
import 'package:vid/us/vid_us_unit.dart';

// URM 输出表格
class Series {
  final List<Point<double>> points;
  final List<PeakPoint> peakPoints;
  final int maxPointIndex;
  final int minPointIndex;
  final int featureId;

  Series(this.featureId, List<Point<double>> outerExterPoints, this.points,
      this.maxPointIndex, this.minPointIndex, VidUsUnit xUnit, VidUsUnit yUnit)
      : peakPoints = _calculatePeakPoints(outerExterPoints, points,
            maxPointIndex, minPointIndex, xUnit, yUnit);

  static List<PeakPoint> _calculatePeakPoints(
      List<Point<double>> outerExterPoints,
      List<Point<double>> points,
      int maxPointIndex,
      int minPointIndex,
      VidUsUnit xUnit,
      VidUsUnit yUnit) {
    List<PeakPoint> peaks = [];
    if (outerExterPoints.isNotEmpty) {
      for (var point in outerExterPoints) {
        int index = point.x.toInt();
        if (index == maxPointIndex) {
          peaks.add(PeakPoint(PeakType.maximum, index, point.y, xUnit, yUnit));
        } else if (index == minPointIndex) {
          peaks.add(PeakPoint(PeakType.minimum, index, point.y, xUnit, yUnit));
        } else {
          peaks.add(PeakPoint(PeakType.normal, index, point.y, xUnit, yUnit));
        }
      }
    } else if (points.isNotEmpty) {
      if (maxPointIndex >= 0 && maxPointIndex < points.length) {
        peaks.add(PeakPoint(PeakType.maximum, maxPointIndex,
            points[maxPointIndex].y, xUnit, yUnit));
      }
      if (minPointIndex >= 0 && minPointIndex < points.length) {
        peaks.add(PeakPoint(PeakType.minimum, minPointIndex,
            points[minPointIndex].y, xUnit, yUnit));
      }
    }
    return peaks;
  }
}

class PeakPoint {
  final PeakType type;
  final int pointIndex;
  final double lumenDiameter;
  final VidUsUnit xUnit;
  final VidUsUnit yUnit;

  PeakPoint(
      this.type, this.pointIndex, this.lumenDiameter, this.xUnit, this.yUnit);
}

enum PeakType { maximum, minimum, normal }
