import 'package:flutter/material.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/live_consultation/controller.dart';
import 'package:get/get.dart';

class ResizableWidget extends StatefulWidget {
  final Widget child;
  final Function(Size size)? onResize;
  final bool? showInLeft;
  final double? width;
  final double? height;
  final double? extraTitleHeight;

  /// 固定宽高比
  final double? aspectRatio;
  ResizableWidget({
    Key? key,
    required this.child,
    this.onResize,
    this.showInLeft,
    this.width,
    this.height,
    this.aspectRatio,
    this.extraTitleHeight,
  }) : super(key: key);

  @override
  _ResizableWidgetState createState() => _ResizableWidgetState();
}

class _ResizableWidgetState extends State<ResizableWidget> {
  final controller = Get.find<LiveConsultationController>();
  late double _width;
  late double _height;

  late Offset _dragStartPos;
  late double _startWidth;
  late double _startHeight;

  double _extraTitleHeight = 0;

  bool _resizingWidth = false;
  bool _resizingHeight = false;
  bool _draggingTop = false;
  bool _draggingRight = false;
  bool _showInLeft = false;

  double? _aspectRatio;

  @override
  void initState() {
    super.initState();
    _width = widget.width ?? 250;
    _height = widget.height ?? 202;
    _showInLeft = widget.showInLeft ?? false;
    _extraTitleHeight = widget.extraTitleHeight ?? 0;
    if (widget.aspectRatio != null) {
      _height = _width / widget.aspectRatio!;
      _aspectRatio = widget.aspectRatio;
    }
    controller.onSwitchPosition.addListener((sender, e) {
      _showInLeft = controller.state.isShowDevicePreviewInLeft;
    });
  }

  @override
  void didUpdateWidget(ResizableWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.aspectRatio != null && widget.aspectRatio != _aspectRatio) {
      _aspectRatio = widget.aspectRatio;
      _height = _width / _aspectRatio!;
      setState(() {});
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      cursor: _getCursor(),
      child: GestureDetector(
        onPanStart: _onPanStart,
        onPanUpdate: _onPanUpdate,
        onPanEnd: _onPanEnd,
        child: Stack(children: [
          Container(
            width: _width,
            height: _height + _extraTitleHeight,
            child: widget.child,
          ),
          Center(
            child: Container(
              margin: EdgeInsets.only(left: 15, bottom: 15),
              width: _width - 15,
              height: _height - 15 + _extraTitleHeight,
              child: MouseRegion(
                opaque: false,
                cursor: SystemMouseCursors.basic,
                child: Container(),
              ),
            ),
          )
        ]),
      ),
    );
  }

  MouseCursor _getCursor() {
    if (_showInLeft) {
      return SystemMouseCursors.resizeDownRight;
    }
    return SystemMouseCursors.resizeDownLeft;
  }

  void _onPanStart(DragStartDetails details) {
    final size = context.size;
    if (size == null) return;

    _dragStartPos = details.localPosition;
    _startWidth = _width;
    _startHeight = _height;

    final widthRatio = (_dragStartPos.dx / size.width);
    final heightRatio = (_dragStartPos.dy / size.height);

    _resizingWidth = widthRatio < 0.2 || widthRatio > 0.8;
    _resizingHeight = heightRatio < 0.2 || heightRatio > 0.8;
    _draggingTop = heightRatio < 0.5;
    _draggingRight = widthRatio > 0.5;
  }

  void _onPanUpdate(DragUpdateDetails details) {
    final size = context.size;
    if (size == null) return;

    final delta = details.localPosition - _dragStartPos;
    final deltaX = delta.dx;
    final deltaY = delta.dy;

    if (_resizingWidth && _resizingHeight) {
      if (_draggingRight) {
        _width = _startWidth + deltaX;
      } else {
        _width = _startWidth - deltaX;
      }
      if (_draggingTop) {
        _height = _startHeight - deltaY;
      } else {
        _height = _startHeight + deltaY;
      }
    } else if (_resizingWidth) {
      if (_draggingRight) {
        _width = _startWidth + deltaX;
      } else {
        _width = _startWidth - deltaX;
      }
    } else if (_resizingHeight) {
      if (_draggingTop) {
        _height = _startHeight - deltaY;
      } else {
        _height = _startHeight + deltaY;
      }
    }

    if (_aspectRatio != null) {
      _height = _width / _aspectRatio!;
    }
    if (_width < 100) {
      _width = 100;
    }
    if (_height < 100) {
      _height = 100;
    }
    setState(() {});

    widget.onResize?.call(Size(_width, _height));
  }

  void _onPanEnd(DragEndDetails details) {
    setState(() {});
  }
}
