import 'dart:async';
import 'package:flutter/material.dart';
import 'package:guru_utils/controller/lifecycle_controller.dart';
import 'package:injectable/injectable.dart';
import 'package:get/get.dart';
import 'package:draggable_float_widget/draggable_float_widget.dart';


@injectable
class ConsoleButtonController extends LifecycleController {
  late StreamController<OperateEvent> eventStreamController;
  late OverlayEntry overlayEntry;

  void showOverlayEntry() {
    final navigatorState = Navigator.of(Get.overlayContext!, rootNavigator: false);
    final overlayState = navigatorState.overlay!;
    overlayState.insert(overlayEntry);
  }

  void hideOverlayEntry() {
    overlayEntry.remove();
  }

  ConsoleButtonController();

  void _createOverlayEntry() {
    overlayEntry = OverlayEntry(
      builder: (context) => GestureDetector(
        onTap: () {
          hideOverlayEntry();
        },
        child: DecoratedBox(
          decoration: const BoxDecoration(
            color: Colors.black54,
          ),
          child: Material(
            child: Stack(
              alignment: Alignment.bottomCenter,
              children: [
                Container(
                  color: Colors.white,
                  height: 400,
                )
              ],
            ),
          ),
        ),
    ));
  }

  @override
  void onInit() {
    eventStreamController = StreamController.broadcast();
    _createOverlayEntry();
    super.onInit();
  }

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

  @override
  void onClose() {
    eventStreamController.close();
    overlayEntry.remove();
    super.onClose();
  }
}