import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flyinsono/lab/components/mouse_gesture_area_for_roi.dart';
import 'package:get/get.dart';

import 'controller.dart';
import 'widgets/trace_canvas_layer.dart';

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

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final ImageTraceOverlayController? initController;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageTraceOverlayController>(
      tag: tabHashCode,
      init: initController ?? ImageTraceOverlayController(),
      id: ImageTraceOverlayController.imageTraceOverlayId,
      builder: (controller) {
        if (!controller.isActive) {
          return SizedBox.shrink();
        }
        return SizedBox.expand(
          child: Center(
            child: AspectRatio(
              aspectRatio: controller.aspectRatio,
              child: Container(
                key: controller.imageTraceLayerKey,
                child: Stack(
                  children: [
                    TraceCanvasLayer(
                      tabHashCode: tabHashCode,
                    ),
                    // 注意此处渲染上下层次不可颠倒，手势层需在最后
                    MouseGestureAreaForRoi(
                      onMouseDown: controller.onMouseDown,
                      onMouseMove: controller.onMouseMove,
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
