import 'package:flutter/widgets.dart';

class AnimatedListController<T> {
  final List<T> items = [];
  final GlobalKey<SliverAnimatedListState> listKey =
      GlobalKey<SliverAnimatedListState>();

  set items(List<T> items) {
    this.items = items;
  }

  void add(T item,
      {Duration duration = const Duration(milliseconds: 300), int? index}) {
    if (index != null) {
      items.insert(index, item);
      listKey.currentState?.insertItem(index, duration: duration);
    } else {
      items.add(item);
      listKey.currentState?.insertItem(items.length - 1, duration: duration);
    }
  }

  void addAtIndex(int index, List<T> newItems,
      {Duration duration = const Duration(milliseconds: 300)}) {
    for (var i = 0; i < newItems.length; i++) {
      items.insert(index + i, newItems[i]);
      listKey.currentState?.insertItem(index + i, duration: duration);
    }
  }

  void addAll(List<T> newItems,
      {Duration duration = const Duration(milliseconds: 300)}) {
    for (var i = 0; i < newItems.length; i++) {
      items.add(newItems[i]);
      listKey.currentState?.insertItem(items.length - 1, duration: duration);
    }
  }

  void update(int index, T item) {
    clearAt(index);
    add(item, index: index);
  }

  void removeItem(
      T item, Widget Function(BuildContext, Animation<double>, T) buildItem,
      {Duration duration = const Duration(milliseconds: 300)}) {
    final index = items.indexOf(item);
    if (index != -1) {
      items.removeAt(index);
      listKey.currentState?.removeItem(
        index,
        (context, animation) => buildItem(context, animation, item),
        duration: duration,
      );
    }
  }

  void remove(
      int index, Widget Function(BuildContext, Animation<double>, T) buildItem,
      {Duration duration = const Duration(milliseconds: 300)}) {
    final removedItem = items.removeAt(index);
    listKey.currentState?.removeItem(
      index,
      (context, animation) => buildItem(context, animation, removedItem),
      duration: duration,
    );
  }

  void removeAtIndex(int index, int count,
      Widget Function(BuildContext, Animation<double>, T) buildItem,
      {Duration duration = const Duration(milliseconds: 300)}) {
    for (var i = 0; i < count; i++) {
      final removedItem = items.removeAt(index);
      listKey.currentState?.removeItem(
        index,
        (context, animation) => buildItem(context, animation, removedItem),
        duration: duration,
      );
    }
  }

  void removeAll(Widget Function(BuildContext, Animation<double>, T) buildItem,
      {Duration duration = const Duration(milliseconds: 300)}) {
    for (var i = items.length - 1; i >= 0; i--) {
      final removedItem = items.removeAt(i);
      listKey.currentState?.removeItem(
        i,
        (context, animation) => buildItem(context, animation, removedItem),
        duration: duration,
      );
    }
  }

  /// *不带动画****
  void clearAt(int index) {
    items.removeAt(index);
    listKey.currentState
        ?.removeItem(index, (context, animation) => Container());
  }


  void clearAll() {
    items.clear();
    listKey.currentState?.removeAllItems((context, animation) => Container());
  }

  /// *不带动画****

  int get length {
    return items.length;
  }

  T get(int index) {
    return items[index];
  }

  int indexOf(T item) {
    return items.indexOf(item);
  }
}
