import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rasadyar_core/presentation/common/app_color.dart';

class OverlayDropdownWidget<T> extends StatefulWidget {
  final List<T> items;
  final T? selectedItem;
  final T? initialValue;
  final Widget Function(T item) itemBuilder;
  final Widget Function(T? selected) labelBuilder;
  final void Function(T selected)? onChanged;
  final EdgeInsets? contentPadding;

  const OverlayDropdownWidget({
    super.key,
    required this.items,
    required this.itemBuilder,
    required this.labelBuilder,
    this.initialValue,
    this.onChanged,
    this.selectedItem,
    this.contentPadding,
  });

  @override
  State<OverlayDropdownWidget<T>> createState() => _OverlayDropdownState<T>();
}

class _OverlayDropdownState<T> extends State<OverlayDropdownWidget<T>> {
  final GlobalKey _key = GlobalKey();
  OverlayEntry? _overlayEntry;
  final RxBool _isOpen = false.obs;
  T? selectedItem;

  @override
  void initState() {
    super.initState();
    selectedItem = widget.selectedItem ?? widget.initialValue;
  }

  void _showOverlay() {
    final renderBox = _key.currentContext!.findRenderObject() as RenderBox;
    final size = renderBox.size;
    final offset = renderBox.localToGlobal(Offset.zero);

    _overlayEntry = OverlayEntry(
      builder: (_) => GestureDetector(
        onTap: _removeOverlay,
        behavior: HitTestBehavior.translucent,
        child: Stack(
          children: [
            Positioned(
              left: offset.dx,
              top: offset.dy + size.height + 4,
              width: size.width,
              child: Material(
                elevation: 4,
                borderRadius: BorderRadius.circular(8),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(color: AppColor.darkGreyLight),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Container(
                    constraints: BoxConstraints(maxHeight: 300),
                    child: ListView(
                      padding: EdgeInsets.only(bottom: 50),
                      shrinkWrap: true,
                      physics: BouncingScrollPhysics(),
                      scrollDirection: Axis.vertical,
                      children: widget.items.map((item) {
                        return InkWell(
                          onTap: () {
                            widget.onChanged?.call(item);
                            setState(() {
                              selectedItem = item;
                            });
                            _removeOverlay();
                          },
                          child: Padding(
                            padding: widget.contentPadding ?? const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                            child: widget.itemBuilder(item),
                          ),
                        );
                      }).toList(),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );

    Overlay.of(context).insert(_overlayEntry!);
    _isOpen.value = true;
  }

  void _removeOverlay() {
    _overlayEntry?.remove();
    _overlayEntry = null;
    _isOpen.value = false;
  }

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

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      key: _key,
      onTap: () {
        _isOpen.value ? _removeOverlay() : _showOverlay();
      },
      child: Container(
        height: 40,
        width: Get.width,
        padding: const EdgeInsets.symmetric(horizontal: 12),
        decoration: BoxDecoration(
          border: Border.all(color: AppColor.darkGreyLight),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            widget.labelBuilder(selectedItem),
            Icon(_isOpen.value ? CupertinoIcons.chevron_up : CupertinoIcons.chevron_down, size: 14),
          ],
        ),
      ),
    );
  }
}
