import 'package:ambito/src/entity/entities.dart';
import 'package:ambito/src/packages/ambito_db/base_db.dart';
import 'package:ambito/src/packages/ambito_notifier/notifier/filter_notifier.dart';
import 'package:isar/isar.dart';

import '../../../consts/consts.dart';
import '../../../widgets/form/fields/field_dropdown.dart';

class ItemFilterRepository extends BaseDB {
  @override
  IsarCollection collection = isar.itemFilters;

  Set<ItemFilter>? getByType(String type) {
    return isar.itemFilters.where().typeEqualTo(type).findAll().toSet();
  }

  List<dynamic>? getIdsByType(String type) {
    return isar.itemFilters
        .where()
        .typeEqualTo(type)
        .idsProperty()
        .findAll()
        .expand((list) => list ?? [])
        .toSet()
        .toList();
  }

  List<dynamic>? getIdsByTypeAndName(String type, String name) {
    return isar.itemFilters
        .where()
        .typeEqualTo(type)
        .and()
        .nameEqualTo(name)
        .idsProperty()
        .findAll()
        .expand((list) => list ?? [])
        .toSet()
        .toList();
  }

  FieldDropdown getDropDown(String type, String label) {
    if (type == 'month') {}

    Set<ItemFilter> filterItems = getByType(type)!;

    return FieldDropdown(
      name: type,
      label: label,
      filterValue: ambitoFilterNotifier.activeFilters[type],
      onClear: () => ambitoFilterNotifier.removeFilter(type),
      onSelected: (value) => ambitoFilterNotifier.setFilter(type, value ?? ''),
      entries: filterItems.map((item) => item.name!).toList(),
    );
  }
}
