import 'package:ambito/src/extensions/extensions.dart';
import 'package:ambito/src/widgets/form/form_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

class FieldDropdown extends FormWidgetField {
  FieldDropdown({
    required this.name,
    required this.label,
    required this.filterValue,
    required this.onClear,
    required this.onSelected,
    required this.entries,
  });

  final String name;
  final String label;
  final String? filterValue;
  final VoidCallback onClear;
  final void Function(String?) onSelected;
  final List<String> entries;

  @override
  FormBuilderField get(theme) {
    return FormBuilderDropdown<String>(
      name: name,
      initialValue: filterValue,
      isDense: true,
      decoration: InputDecoration(
          labelText: label,
          isDense: true,
          hintText: label,
          border: OutlineInputBorder(borderRadius: BorderRadius.circular(8))),
      onReset: onClear,
      onChanged: onSelected,
      items: entries
          .map(
            (entry) => DropdownMenuItem(
              value: entry.toLowerCase().replaceUmlauts().toString(),
              alignment: Alignment.centerLeft,
              child: Row(
                children: [
                  Text(
                    entry,
                    softWrap: true,
                    maxLines: 3,
                    style: entry.toLowerCase().replaceUmlauts() == filterValue
                        ? theme.bodyMedium
                            .copyWith(color: theme.currentColorScheme.secondary)
                        : theme.bodyMedium,
                    overflow: TextOverflow.visible,
                  ),
                  if (entry.toLowerCase().replaceUmlauts() == filterValue)
                    IconButton(
                      icon: const Icon(Icons.cancel_outlined, size: 12),
                      onPressed: onClear,
                      color: theme.currentColorScheme.secondary,
                    ),
                ],
              ),
            ),
          )
          .toList(),
    );
  }
}
