import 'package:fis_measure/interfaces/date_types/point.dart';
import 'package:fis_measure/interfaces/date_types/rect_region.dart';
import 'package:fis_measure/interfaces/process/modes/mode.dart';
import 'package:fis_measure/interfaces/process/physical_coordinates/physical_coordinate.dart';
import 'package:fis_measure/interfaces/process/visuals/tissue_area.dart';
import 'package:fis_measure/interfaces/process/visuals/visual.dart';
import 'package:fis_measure/process/viewports/tissue_viewport.dart';
import 'package:vid/us/vid_us_2d_visual.dart';
import 'package:vid/us/vid_us_physical_coordinate.dart';
import 'package:vid/us/vid_us_visual_area_type.dart';

import '../physical_coordinates/convex_tissue.dart';
import '../physical_coordinates/linear_tissue.dart';
import 'visual_area.dart';

class TissueArea extends VisualArea implements ITissueArea {
  TissueArea(IVisual visual, IMode mode) : super(visual, mode);

  void init() {
    final visualData = visual.visualData as VidUs2DVisual;
    if (mode.modeType == ModeTypeEnum.tissue) {
      final logicalInfo =
          visualData.logicalCoordinates[VidUsVisualAreaType.Tissue]!;
      final region = logicalInfo.region;
      final rectRegion = RectRegion.pointTLBR(
        DPoint(region.topLeft.x, region.topLeft.y),
        DPoint(region.bottomRight.x, region.bottomRight.y),
      );
      ITissuePhysicalCoordinate physical;
      // TODO: 动态创建
      final physicalInfo = visualData.physicalCoordinates.entries.first.value;
      if (physicalInfo is VidUsLinearTissuePhysicalCoordinate) {
        physical = LinearTissuePhysicalCoordinate(
          physicalInfo.depthStart,
          physicalInfo.depthEnd,
          physicalInfo.width,
          physicalInfo.beamPosition,
          physicalInfo.steer,
        );
      } else {
        final info = physicalInfo as VidUsConvexTissuePhysicalCoordinate;
        physical = ConvexTissuePhysicalCoordinate(
          physicalInfo.depthStart,
          physicalInfo.depthEnd,
          physicalInfo.width,
          physicalInfo.beamPosition,
          info.zeroRadius,
        );
      }

      viewport = TissueViewPort(
        this,
        physical,
        logicalInfo.xUnit,
        logicalInfo.yUnit,
        logicalInfo.isFlipHorizontal,
        logicalInfo.isFlipVertical,
        rectRegion,
      );
    }
  }
}
