import 'dart:math' as math;
import 'package:fis_measure/interfaces/date_types/point.dart';
import 'package:fis_measure/interfaces/date_types/skew_transform.dart';
import 'package:fis_measure/interfaces/process/physical_coordinates/physical_coordinate.dart';
import 'package:flutter/foundation.dart';
import 'package:vid/us/vid_us_visual_area_type.dart';

class ConvexTissuePhysicalCoordinate implements ITissuePhysicalCoordinate {
  late final double _depthStart;
  late final double _depthEnd;
  late final double _width;
  late final double _beamPosition;
  late final double _zeroRadius;

  ConvexTissuePhysicalCoordinate(
    double depthStart,
    double depthEnd,
    double width,
    double beamPosition,
    double zeroRadius,
  ) {
    _depthStart = depthStart;
    _depthEnd = depthEnd;
    _width = width;
    _beamPosition = beamPosition;
    _zeroRadius = zeroRadius;
  }

  @override
  VidUsVisualAreaType get areaType => VidUsVisualAreaType.Tissue;

  @override
  double get depthEnd => _depthEnd;

  @override
  double get depthStart => _depthStart;

  @override
  double get width => _width;

  @override
  double get beamPosition => _beamPosition;

  /// 可见扇形边到圆点的距离
  double get zeroRadius => _zeroRadius;

  @override
  DPoint convert(DPoint pointInLogical) {
    double depth = math.sqrt(math.pow(pointInLogical.x, 2) +
            math.pow(pointInLogical.y + zeroRadius, 2)) -
        zeroRadius;
    double tilt =
        toDegrees(math.atan2(pointInLogical.x, pointInLogical.y + zeroRadius));
    return DPoint(tilt, depth);
  }

  @override
  DPoint convertBack(DPoint pointInPhysical) {
    int ratio = pointInPhysical.x >= 0 ? 1 : -1;
    var angle = pointInPhysical.x.abs();
    double radian = (math.pi / 180) * angle.abs();
    double x = ratio * math.sin(radian) * (pointInPhysical.y + zeroRadius);
    double y = math.cos(radian) * (pointInPhysical.y + zeroRadius) - zeroRadius;
    return DPoint(x, y);
  }

  @protected
  double toDegrees(double angle) {
    return angle / math.pi * 180;
  }
}
