import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/urm_data_processor/urm_data_processor.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/accessibility_layer.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/active_canvas_layer.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/annotation_record_canvas_layer.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/measure_result_layer.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/mouse_gesture_layer.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/record_canvas_layer.dart';
import 'package:get/get.dart';
import 'index.dart';
import 'package:fis_measure/utils/canvas.dart';
import 'widgets/sharp_copy_layer.dart';
import 'package:fis_measure/view/gesture/annotation/annotation_gesture.dart';

class ImageMeasureOverlay extends GetView<ImageMeasureOverlayController> {
  const ImageMeasureOverlay({
    Key? key,
    required this.initController,
    required this.tabHashCode,
    required this.currentURMProcessor,
  }) : super(key: key);

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final ImageMeasureOverlayController initController;

  final URMDataProcessor currentURMProcessor;

  @override
  Widget build(BuildContext context) {
    MeasureCanvasExt.setFontFamily(
      Theme.of(context).textTheme.labelLarge?.fontFamily,
    );
    return GetBuilder<ImageMeasureOverlayController>(
      init: initController,
      tag: tabHashCode,
      id: ImageMeasureOverlayController.imageMeasureOverlayId,
      builder: (_) {
        bool isShowLeftRight = currentURMProcessor.showLeftRight;

        if (!controller.isActive) {
          return SizedBox.shrink();
        }
        return SizedBox.expand(
          child: Stack(
            children: [
              Center(
                child: AspectRatio(
                  aspectRatio: controller.aspectRatio,
                  child: Container(
                    key: controller.imageMeasureLayerKey,
                    child: Stack(
                      children: [
                        Row(
                          children: [
                            if (isShowLeftRight)
                              Expanded(
                                child: RecordCanvasLayer(
                                  tabHashCode: tabHashCode,
                                ),
                              ),
                            Expanded(
                              child: RecordCanvasLayer(
                                tabHashCode: tabHashCode,
                                isURMImage: true,
                              ),
                            )
                          ],
                        ),
                        AnnotationRecordCanvasLayer(tabHashCode: tabHashCode),
                        Row(
                          children: [
                            if (isShowLeftRight)
                              Expanded(
                                child: ActiveCanvasLayer(
                                  tabHashCode: tabHashCode,
                                ),
                              ),
                            Expanded(
                              child: ActiveCanvasLayer(
                                tabHashCode: tabHashCode,
                              ),
                            )
                          ],
                        ),

                        // 注意此处渲染上下层次不可颠倒
                        // 注释
                        controller.isAnnotation
                            ? AnnotationGestureLayer()
                            : MouseGestureLayer(
                                tabHashCode: tabHashCode,
                                onMouseDown: controller.onMouseDown,
                                onMouseMove: controller.onMouseMove,
                                isLeftRight: currentURMProcessor
                                        .visualInfo.isLeftRight &&
                                    currentURMProcessor.isSyncDisplay,
                              ),
                      ],
                    ),
                  ),
                ),
              ),
              MeasureResultLayer(
                tabHashCode: tabHashCode,
              ),
              AccessibilityLayer(
                tabHashCode: tabHashCode,
              ),
              SharpCopySectionLayer(
                tabHashCode: tabHashCode,
                isURMDualImages: isShowLeftRight,
              ),
              // Positioned(
              //   right: 6,
              //   bottom: 6,
              //   child: SharpCopyToolLayer(
              //     tabHashCode: tabHashCode,
              //   ),
              // ),
            ],
          ),
        );
      },
    );
  }
}
