
import 'package:fis_measure/interfaces/date_types/rect_region.dart';
import 'package:fis_measure/view/gesture/cross_position_indicator.dart';
import 'package:fis_measure/view/gesture/mouse_gesture.dart';
import 'package:fis_measure/values/colors.dart';
import 'package:fis_measure/view/cursor.dart';
import 'package:flutter/material.dart';

// 鼠标指示器（无需测量，无需控制器）
class MouseGestureAreaForRoi extends StatefulWidget {
  const MouseGestureAreaForRoi({super.key, this.onMouseDown, this.onMouseMove});
  final ValueChanged<Offset>? onMouseDown;
  final ValueChanged<Offset>? onMouseMove;

  @override
  State<MouseGestureAreaForRoi> createState() => _MouseGestureAreaForRoiState();
}

class _MouseGestureAreaForRoiState extends State<MouseGestureAreaForRoi> {
  Offset? cursorPosition;
  CursorDisplayType displayType = CursorDisplayType.none;
  RectRegion tissueTMPixelRegion = RectRegion();
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (details) {
        widget.onMouseDown?.call(details.localPosition);
      },
      onPanUpdate: (details) {
        setState(() {
          cursorPosition = details.localPosition;
        });
        widget.onMouseMove?.call(details.localPosition);
      },
      child: MouseRegion(
        cursor: SystemMouseCursors.none,
        onHover: (event) {
          setState(() {
            cursorPosition = event.localPosition;
          });
          widget.onMouseMove?.call(event.localPosition);
          // _handleAreaChange(event.localPosition);
        },
        onEnter: (e) {
          setState(() {
            displayType = CursorDisplayType.normal;
          });
        },
        onExit: (e) {
          setState(() {
            displayType = CursorDisplayType.none;
          });
        },
        child: Stack(
          children: [
            _buildCursor(),
            // if (isShowRightClickTip && displayType != CursorDisplayType.none)
            //   _buildTip(),
          ],
        ),
      ),
    );
  }

  Widget _buildCursor() {
    switch (displayType) {
      case CursorDisplayType.none:
        return Container();
      case CursorDisplayType.normal:
        return _buildPositionedCursor(cursorPosition);
      case CursorDisplayType.cross:
        return CrossIndicator(
          areaRegion: Rect.fromLTRB(
              tissueTMPixelRegion.left,
              tissueTMPixelRegion.top,
              tissueTMPixelRegion.right,
              tissueTMPixelRegion.bottom),
        );
    }
  }

  Widget _buildPositionedCursor(Offset? cursorPosition) {
    if (cursorPosition == null) return Container();

    /// FIXME 更多参数需要通过刷新来传入
    final cursorSize = 12;
    final cursorScaleRatio = 2.0;
    final offset = cursorScaleRatio * cursorSize / 2;
    final dx = cursorPosition.dx - offset;
    final dy = cursorPosition.dy - offset;

    return Positioned(
      left: dx,
      top: dy,
      child: MeasureCursor(
        type: MeasureCursorType.cursor01,
        size: cursorSize * cursorScaleRatio,
        color: MeasureColors.Primary,
      ),
    );
  }
}
