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 'multi_select_dropdown_logic.dart';

class MultiSelectDropdown<T> extends StatelessWidget {
  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;
  final String Function(T item)? itemToString;

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MultiSelectDropdownLogic<T>>(
      init: MultiSelectDropdownLogic<T>(
        items: items,
        selectedItem: selectedItem,
        initialValue: initialValue,
        itemToString: itemToString,
        onChanged: onChanged,
        contentPadding: contentPadding,
        itemBuilder: itemBuilder,
      ),
      builder: (controller) {
        return GestureDetector(
          onTap: () {
            controller.isOpen.value ? controller.removeOverlay() : controller.showOverlay(context);
          },
          child: Container(
            height: 40,
            width: Get.width,
            padding: const EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: items.isEmpty ? Colors.grey.shade200 : AppColor.bgLight,
              border: Border.all(color: AppColor.darkGreyLight),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(child: labelBuilder(controller.selectedItem.value)),
                Icon(
                  controller.isOpen.value ? CupertinoIcons.chevron_up : CupertinoIcons.chevron_down,
                  size: 14,
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
