import 'dart:math';
import 'dart:ui';
import 'package:flyinsono/lab/pages/lab_image/urm_data_processor/index.dart';
import 'package:vid/us/vid_us_image.dart';
import 'package:vid/us/vid_us_2d_visual.dart';
import 'package:vid/us/vid_us_logical_coordinate.dart';
import 'package:vid/us/vid_us_physical_coordinate.dart';

// 图像Visual参数（从Vid信息中获取）
class URMVisualInfo {
  int width = 0;
  int height = 0;
  bool isLinear = true; // 是否是线阵
  double depthStart = 0;
  double depthEnd = 1;
  double scanWidth = 0;
  RoiViewType roiViewType = RoiViewType.nomal;
  bool get isLeftRight => roiViewType == RoiViewType.leftRightHalfHalf; // 是否是双幅
  double beamsLeftBoundary = 0; // 有效区域左边界百分比
  double beamsRightBoundary = 1; // 有效区域右边界百分比
  double get beamsRangePercent => beamsRightBoundary - beamsLeftBoundary;
  double zeroRadius = 0; // 弧阵零点半径
  Offset zeroCenterOffset = Offset(0, 0); // 弧阵零点中心归一化坐标
  double physicalLeft = 0; // 物理坐标左边界
  double physicalRight = 0; // 物理坐标右边界
  double physicalTop = 0; // 物理坐标上边界
  double physicalBottom = 0; // 物理坐标下边界
  bool isVisualLoaded = false; // Visual 参数是否已初始化完成
  double get regionWidth => physicalRight - physicalLeft; // 物理坐标区域宽度

  /// 判断是否为左右分屏模式的图像
  RoiViewType getViewType(VidUsImage vidUsImage) {
    if (vidUsImage.visuals.isEmpty) {
      return RoiViewType.nomal;
    }
    final visual = vidUsImage.visuals.first;
    if (!(visual is VidUs2DVisual)) return RoiViewType.nomal;
    if (visual.logicalCoordinates.length > 0 &&
        visual.physicalCoordinates.length > 0) {
      setPhysicalCoordinateType(visual.physicalCoordinates.entries.first.value);
      setBeamsBoundary(
        visual.logicalCoordinates.entries.first.value,
        visual.physicalCoordinates.entries.first.value,
      );
      setDepthParam(visual.physicalCoordinates.entries.first.value);
      calZeroCenterOffset();
    }
    final modesList = visual.modes.map((e) => e.name).toList();
    if ((modesList.contains("UrmCBI") &&
        visual.logicalCoordinates.length == 2)) {
      return RoiViewType.leftRightHalfHalf;
    }
    if (modesList.contains("UrmB")) {
      return RoiViewType.nomal;
    }

    return RoiViewType.nomal;
  }

  /// 设置 坐标系类型（线阵或弧阵）
  void setPhysicalCoordinateType(VidUsPhysicalCoordinate physicalCoordinates) {
    if (physicalCoordinates is VidUsConvexTissuePhysicalCoordinate) {
      isLinear = false;
    } else if (physicalCoordinates is VidUsTissuePhysicalCoordinate) {
      isLinear = true;
    }
  }

  /// 设置有效区域
  void setBeamsBoundary(VidUsLogicalCoordinate logicalCoordinate,
      VidUsPhysicalCoordinate physicalCoordinates) {
    if (physicalCoordinates is VidUsTissuePhysicalCoordinate) {
      beamsLeftBoundary = (1 -
              (physicalCoordinates.width /
                  (logicalCoordinate.region.right -
                      logicalCoordinate.region.left))) /
          2;
      beamsRightBoundary = 1 - beamsLeftBoundary;
      physicalLeft = logicalCoordinate.region.left;
      physicalRight = logicalCoordinate.region.right;
      physicalTop = logicalCoordinate.region.top;
      physicalBottom = logicalCoordinate.region.bottom;
    }
    if (physicalCoordinates is VidUsConvexTissuePhysicalCoordinate) {
      zeroRadius = physicalCoordinates.zeroRadius;
      physicalLeft = logicalCoordinate.region.left;
      physicalRight = logicalCoordinate.region.right;
      physicalTop = logicalCoordinate.region.top;
      physicalBottom = logicalCoordinate.region.bottom;
      beamsLeftBoundary = 0;
      beamsRightBoundary = 1;
    }
  }

  /// 设置 Depth 参数
  void setDepthParam(VidUsPhysicalCoordinate physicalCoordinates) {
    if (physicalCoordinates is VidUsTissuePhysicalCoordinate) {
      depthStart = physicalCoordinates.depthStart;
      depthEnd = physicalCoordinates.depthEnd;
      scanWidth = physicalCoordinates.width;
    }
    if (physicalCoordinates is VidUsConvexTissuePhysicalCoordinate) {
      depthStart = physicalCoordinates.depthStart;
      depthEnd = physicalCoordinates.depthEnd;
      scanWidth = physicalCoordinates.width;
    }
  }

  // 计算弧阵零点中心坐标
  void calZeroCenterOffset() {
    if (isLinear) {
      return;
    }
    final angle = scanWidth / (180.0 / pi); //转换为角度
    final verticalDistance = (cos(angle / 2) * zeroRadius).abs();
    zeroCenterOffset = Offset(
        0.5,
        -1 *
            (verticalDistance /
                (depthEnd - depthStart + zeroRadius - verticalDistance)));
  }

  /// 设置视图信息
  void loadVisualParams(VidUsImage? vidUsImage) {
    if (vidUsImage == null) {
      return;
    }
    width = vidUsImage.width;
    height = vidUsImage.height;
    roiViewType = getViewType(vidUsImage);
    isVisualLoaded = true;
  }

  /// 根据像素偏移值修复 beamsLeftBoundary 和 beamsRightBoundary
  void fixBeamsBoundaryByPixelOffset(int pixelOffset) {
    if (isLinear) {
      double offsetPct = pixelOffset / width;
      beamsLeftBoundary = max(0, beamsLeftBoundary + offsetPct);
      beamsRightBoundary = min(1, beamsRightBoundary + offsetPct);
    }
  }
}
