import 'package:cool_dropdown/cool_dropdown.dart';
import 'package:cool_dropdown/models/cool_dropdown_item.dart';
import 'package:flutter/material.dart';

class VSelectKV<T> {
  final String label;
  final T value;

  VSelectKV(this.label, this.value);
}

class VSelect<T, TValue> extends StatelessWidget {
  final List<T> source;
  final TValue? value;
  final String Function(T data) labelGetter;
  final TValue Function(T data) valueGetter;
  final ValueChanged<TValue>? onChanged;

  const VSelect({
    super.key,
    required this.source,
    this.value,
    required this.labelGetter,
    required this.valueGetter,
    this.onChanged,
  });

  @override
  Widget build(BuildContext context) {
    CoolDropdownItem<TValue>? defaultItem = null;
    List<CoolDropdownItem<TValue>> items = source.map((e) {
      final itemValue = valueGetter(e);
      bool isSelected = value == itemValue;

      final item = CoolDropdownItem<TValue>(
        label: labelGetter(e),
        value: itemValue,
        isSelected: isSelected,
      );
      if (isSelected) {
        defaultItem = item;
      }
      return item;
    }).toList();

    final controller = DropdownController(
      duration: const Duration(milliseconds: 200),
    );

    final primaryColor = Theme.of(context).primaryColor;
    final secondaryColor = Theme.of(context).secondaryHeaderColor;

    const borderSide = BorderSide(width: 1, color: Colors.grey);
    final openBorderSide = BorderSide(width: 1, color: primaryColor);
    return CoolDropdown<TValue>(
      defaultItem: defaultItem,
      resultOptions: ResultOptions(
        boxDecoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.zero,
          border: Border(
            top: borderSide,
            bottom: borderSide,
            left: borderSide,
            right: borderSide,
          ),
        ),
        openBoxDecoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.zero,
          border: Border(
            top: openBorderSide,
            bottom: openBorderSide,
            left: openBorderSide,
            right: openBorderSide,
          ),
          boxShadow: const [
            BoxShadow(
              color: Color(0x1a9E9E9E),
              spreadRadius: 1,
              blurRadius: 10,
              offset: Offset(0, 1),
            ),
          ],
        ),
      ),
      dropdownOptions: const DropdownOptions(
        animationType: DropdownAnimationType.size,
        borderRadius: BorderRadius.zero,
        duration: Duration(milliseconds: 200),
      ),
      dropdownItemOptions: DropdownItemOptions(
        selectedTextStyle: TextStyle(
          color: primaryColor,
          fontSize: 16,
          fontWeight: FontWeight.w400,
        ),
        selectedBoxDecoration: BoxDecoration(
          color: secondaryColor,
          borderRadius: BorderRadius.zero,
        ),
      ),
      dropdownList: items,
      controller: controller,
      onChange: (TValue value) {
        try {
          onChanged?.call(value);
        } catch (e) {}
        controller.close();
      },
    );
  }
}
