import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../state/todo_state.dart';
import 'todo_item.dart';
import 'empty_state.dart';

class TodoList extends StatelessWidget {
  const TodoList({super.key});

  @override
  Widget build(BuildContext context) {
    final state = context.watch<TodoState>();
    final todos = state.filteredTodos;

    if (todos.isEmpty) {
      return const EmptyState();
    }

    return ListView.builder(
      padding: const EdgeInsets.only(bottom: 80),
      itemCount: todos.length,
      itemBuilder: (context, index) {
        final todo = todos[index];
        return Dismissible(
          key: ValueKey(todo.id),
          direction: DismissDirection.endToStart,
          background: Container(
            alignment: Alignment.centerRight,
            padding: const EdgeInsets.only(right: 20),
            color: Colors.red,
            child: const Icon(Icons.delete, color: Colors.white),
          ),
          onDismissed: (_) {
            state.deleteTodo(todo.id!);
          },
          child: TodoItem(todo: todo),
        );
      },
    );
  }
}
