import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:rasadyar_core/presentation/common/app_color.dart';
import 'package:rasadyar_core/presentation/widget/draggable_bottom_sheet/draggable_bottom_sheet_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DraggableBottomSheet extends StatelessWidget {
  final DraggableBottomSheetController controller;
  final Widget? child;
  final Color? backgroundColor;


  const DraggableBottomSheet({
    super.key,
    required this.controller,
    this.child,
    this.backgroundColor = Colors.white,
  });

  @override
  Widget build(BuildContext context) {

    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (controller.isVisible.value && !controller.isVisible.value) {
        controller.show();
      }
    });

    return Obx(() {
      if (!controller.isVisible.value) return SizedBox.shrink();
      return  Stack(

        children: [
          // پس‌زمینه تیره
          Positioned.fill(
            child: GestureDetector(
              onTap: controller.toggle,
              child: Container(color: Colors.black54),
            ),
          ),
          // محتوای BottomSheet
          AnimatedPositioned(
            duration: Duration(milliseconds: 300),
            curve: Curves.easeOut,
            left: 0,
            right: 0,
            bottom: 0,
            child: GestureDetector(
              onVerticalDragUpdate: (details) =>
                  controller.updateHeight(details.primaryDelta),
              child: Container(
                height: controller.currentHeight.value,
                decoration: BoxDecoration(
                  color: backgroundColor,
                  borderRadius: BorderRadius.vertical(
                    top: Radius.circular(20),
                  ),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withValues(alpha: 0.1),
                      blurRadius: 10,
                    ),
                  ],
                ),
                child: Column(
                  children: [
                    GestureDetector(
                      onTap: controller.toggle,
                      child: Container(
                        padding: EdgeInsets.all(10),
                        child: Icon(Icons.drag_handle),
                      ),
                    ),
                    Expanded(child: child ?? SizedBox.shrink()),
                  ],
                ),
              ),
            ),
          ),
        ],
      );

    }

    );
  }
}