import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vid/us/vid_us_unit.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/urm_chart_panel/widgets/statistical_chart.dart';

class URMChartPanelController extends GetxController {
  URMChartPanelController();

  static const urmChartPanelId = "progress_panel";

  bool _enable = false;
  bool get enable => _enable;
  static const List<Color> colorsConfig = [
    Color(0xFFFFFF00),
    Color(0xFF7CFC00),
    Color(0xFFFA8072),
    Color(0xFF00FFFF),
    Color(0xFF1E90FF),
  ];
  List<Point<double>> displayPoints = [];
  List<PeakPoint> displayPeakPoints = [];
  Color displayLineColor = colorsConfig[0]; // 当前展示的线的颜色
  List<double> xAxises = []; // x 轴坐标【UI】
  List<double> yAxises = []; // y 轴坐标【UI】

  // 新结构下的参数
  final List<Series> lines = [];
  final int _capacity = 5;
  final int xAxisLines = 5;
  final int yAxisLines = 5;
  VidUsUnit xUnit = VidUsUnit.mm;
  VidUsUnit yUnit = VidUsUnit.um;

  // 设置图表是否可用（是否展示）
  void setChartEnable(bool enable) {
    _enable = enable;
    update([urmChartPanelId]);
  }

  void addChart(int featureId, List<Point<double>> peakPoints,
      List<Point<double>> line, int maxIndex, int minIndex) {
    print("新增图表 $featureId");
    if (lines.length == _capacity) {
      lines.removeAt(0);
    }
    lines.add(
      Series(featureId, peakPoints, line, maxIndex, minIndex, xUnit, yUnit),
    );
    if (lines.isNotEmpty) {
      setDisplayChart(lines.length - 1);
    }
  }

  // 当有测量项被清除时，清除对应的图表
  void removeChart(int featureId) {
    for (int i = 0; i < lines.length; i++) {
      if (lines[i].featureId == featureId) {
        lines.removeAt(i);
        if (lines.isNotEmpty) {
          setDisplayChart(lines.length - 1);
        } else {
          clearAllCharts();
        }
      }
    }
  }

  // 清空所有图表
  void clearAllCharts() {
    lines.clear();
    displayPoints.clear();
    displayPeakPoints.clear();
    xAxises.clear();
    yAxises.clear();
    _enable = false;
    update([urmChartPanelId]);
  }

  void setDisplayChart(int index) {
    if (index < lines.length) {
      displayPoints = lines[index].points;
      displayPeakPoints = lines[index].peakPoints;
      setDisplayAxises(lines[index].points);
      displayLineColor = colorsConfig[index];
      update([urmChartPanelId]);
    }
  }

  // 设置坐标轴参数
  void setDisplayAxises(List<Point<double>> displayPoints) {
    double xMaxValue = -double.maxFinite;
    double xMinValue = double.maxFinite;
    double yMaxValue = -double.maxFinite;
    double yMinValue = double.maxFinite;
    for (Point<double> point in displayPoints) {
      yMaxValue = max(yMaxValue, point.y);
      yMinValue = min(yMinValue, point.y);
      xMaxValue = max(xMaxValue, point.x);
      xMinValue = min(xMinValue, point.x);
    }
    double xMax = xMaxValue;
    double xMin = xMinValue;
    int yMax = yMaxValue.ceil();
    int yMin = yMinValue.floor();
    List<double> newXAxis = [];
    List<double> newYAxis = [];
    for (int i = 0; i < xAxisLines; ++i) {
      newXAxis.add((xMin + i * (xMax - xMin) / (xAxisLines - 1)).toDouble());
    }
    for (int i = 0; i < yAxisLines; ++i) {
      newYAxis.add((yMin + i * (yMax - yMin) / (yAxisLines - 1)).toDouble());
    }
    if (newYAxis.isNotEmpty &&
        newXAxis.isNotEmpty &&
        newYAxis[0] == newXAxis[0]) {
      newYAxis[0] = double.nan;
    }
    xAxises = newXAxis;
    yAxises = newYAxis;
  }

  List<Point> changePoints(List<Point> originPoints, double cmlength) {
    List<Point> points = [];
    int count = originPoints.length;
    double step = cmlength / (count - 1);
    for (int i = 0; i < count; i++) {
      points.add(Point(i * step, originPoints[i].y));
    }
    return points;
  }

  @override
  void onReady() {
    super.onReady();
  }
}
