import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/controller.dart';

class CanvasMagnifier extends StatefulWidget {
  final double magnifierScale;
  final Size magnifierSize;
  final ImageMeasureOverlayController initController;
  const CanvasMagnifier(
      {Key? key,
      this.magnifierScale = 2.0,
      this.magnifierSize = const Size(300, 180),
      required this.initController})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CanvasMagnifierState();
  }
}

class _CanvasMagnifierState extends State<CanvasMagnifier> {
  final GlobalKey _magnifierKey = GlobalKey();

  Offset deltaOffsetFromTouchPoint = const Offset(0, 0);
  Offset magnifierCenter = const Offset(0, 0);
  Offset containerPos = const Offset(0, 0);
  Size containerSize = const Size(0, 0);

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) => {
          _initContainerParam(),
        });
    super.initState();
  }

  void _initContainerParam() {
    final containerLayer = context.findRenderObject() as RenderBox;
    containerPos = containerLayer.localToGlobal(Offset.zero);
    containerSize = containerLayer.size;
  }

  void _updateMagnifierPosition(_, Offset offset) {
    _getPositions();
    final finalOffset =
        (magnifierCenter - (offset - containerPos)) * widget.magnifierScale;
    setState(() {
      deltaOffsetFromTouchPoint = finalOffset;
    });
  }

  void _getPositions() {
    if (_magnifierKey.currentContext == null) {
      return;
    }
    final RenderBox renderBoxRed =
        _magnifierKey.currentContext!.findRenderObject() as RenderBox;
    magnifierCenter = renderBoxRed.localToGlobal(Offset(
            widget.magnifierSize.width / 2 - containerPos.dx,
            widget.magnifierSize.height / 2 -
                containerPos.dy -
                (widget.magnifierScale - 1) * 10)) /
        widget.magnifierScale;
  }

  @override
  Widget build(BuildContext context) {
    final offsetMatrix = Matrix4.identity()
      ..translate(deltaOffsetFromTouchPoint.dx, deltaOffsetFromTouchPoint.dy)
      ..scale(widget.magnifierScale);
    final aliment = _getAlignmentFromLocation(widget.initController
        .measureDataController!.measureSystemSetting.showResultLocation);
    return Container(
      child: SizedBox.expand(
        child: MouseRegion(
          opaque: false,
          cursor: SystemMouseCursors.basic,
          onHover: (event) => _updateMagnifierPosition(null, event.position),
          child: Column(
            children: [
              SizedBox(
                height: 30,
              ),
              Align(
                alignment: aliment,
                child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: BackdropFilter(
                      filter: ImageFilter.matrix(offsetMatrix.storage),
                      child: Container(
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.white, width: 2),
                          borderRadius: BorderRadius.circular(10),
                        ),
                        child: SizedBox(
                          key: _magnifierKey,
                          width: widget.magnifierSize.width,
                          height: widget.magnifierSize.height,
                        ),
                      ),
                    )),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 面板对齐方式
  Alignment _getAlignmentFromLocation(int locationNum) {
    switch (locationNum) {
      case 0:
        return Alignment.topRight;
      case 1:
        return Alignment.topRight;
      case 2:
        return Alignment.topLeft;
      case 3:
        return Alignment.topRight;
      default:
        return Alignment.topRight;
    }
  }
}
