import 'dart:async';
import 'package:flutter/material.dart';
import 'package:guru_utils/controller/lifecycle_controller.dart';
import 'package:get/get.dart';
import 'package:draggable_float_widget/draggable_float_widget.dart';
import 'package:guru_widgets/animation/standard/transform_animation.dart';

import 'console.dart';


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: (BuildContext context) => GestureDetector(
        onTap: () {
          hideOverlayEntry();
        },
        child: DecoratedBox(
          decoration: const BoxDecoration(
            color: Colors.transparent,
          ),
          child: Stack(
            children: [
              Positioned(
                bottom: 0,
                left: 0,
                child: AnimatedTransformBuilder.single(
                  fromTranslation: const Offset(0, 300),
                  toTranslation: Offset.zero,
                  alignment: Alignment.topCenter,
                  curve: const Cubic(0.175, 0.885, 0.32, 1.1),
                  duration: const Duration(milliseconds: 300),
                  child: GestureDetector(
                    onTap: () {},
                    child: Container(
                      decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20))
                      ),
                      height: 300, 
                      width: MediaQuery.of(context).size.width,
                      child: ConsolePopup(onClosed: () {
                        hideOverlayEntry();
                      }),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

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

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

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