mirrored_repositories/Expenses_Tracker_App/lib/screens/home
import 'package:expense_repository/expense_repository.dart';
import 'package:expenses_tracker/bloc/get_expenses_bloc/get_expenses_bloc.dart';
import 'package:expenses_tracker/utils/constants/colors.dart';
import 'package:expenses_tracker/utils/constants/sizes.dart';
import 'package:expenses_tracker/utils/constants/tr_text.dart';

import 'package:expenses_tracker/utils/extensions/string_extensions.dart';
import 'package:expenses_tracker/utils/helpers/helpers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';

class TransactionList extends StatefulWidget {
  final List<Expense> expenses;
  const TransactionList(this.expenses, {super.key});

  @override
  State<TransactionList> createState() => _TransactionListState();
}

class _TransactionListState extends State<TransactionList> {
  @override
  Widget build(BuildContext context) {
    final dark = EHelperFunctions.isDarkMode(context);

    return BlocProvider(
      create: (context) => GetExpensesBloc(FirebaseExpenseRepo()),
      child: Expanded(
        child: ListView.builder(
            itemCount: widget.expenses.length,
            itemBuilder: (context, int i) {
              Color amountColor = widget.expenses[i].howExpense == 'Income'
                  ? EColors.success
                  : EColors.error;
              return Dismissible(
                key: Key(widget.expenses[i].expenseId),
                direction: DismissDirection.endToStart,
                background: Container(
                  alignment: Alignment.centerRight,
                  padding: const EdgeInsets.only(right: 20.0),
                  color: EColors.error,
                  child: const Icon(
                    Icons.delete,
                    color: EColors.white,
                  ),
                ),
                onDismissed: (direction) {
                  // Silinen harcama bir değişkende saklanır
                  Expense deletedExpense = widget.expenses[i];
                  // Silme işlemi tetiklenir
                  context
                      .read<GetExpensesBloc>()
                      .add(ExpensesDelete(deletedExpense));

                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      duration: const Duration(seconds: 2),
                      content: const Text(ETexts.snackBar),
                      action: SnackBarAction(
                        label: ETexts.snackLabel,
                        onPressed: () {
                          context
                              .read<GetExpensesBloc>()
                              .add(UndoDeleteExpense(deletedExpense));
                          ScaffoldMessenger.of(context).hideCurrentSnackBar();
                        },
                      ),
                    ),
                  );
                },
                child: Padding(
                  padding: const EdgeInsets.only(bottom: ESizes.sm),
                  child: Container(
                    decoration: BoxDecoration(
                        color: dark
                            ? EColors.darkContainer
                            : EColors.lightContainer,
                        borderRadius:
                            BorderRadius.circular(ESizes.borderRadiusLg)),
                    child: Padding(
                      padding: const EdgeInsets.all(ESizes.md),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Stack(
                                alignment: Alignment.center,
                                children: [
                                  Image.asset(
                                    widget.expenses[i].categoryImagePath,
                                    scale: 2,
                                  ),
                                ],
                              ),
                              const SizedBox(width: ESizes.md),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                      widget.expenses[i].categoryName
                                          .capitalizeFirstLetter(),
                                      style: Theme.of(context)
                                          .textTheme
                                          .titleLarge),
                                  Text(
                                      widget.expenses[i].explain
                                          .toString()
                                          .capitalizeFirstLetter(),
                                      style: Theme.of(context)
                                          .textTheme
                                          .labelLarge),
                                ],
                              ),
                            ],
                          ),
                          Column(
                            children: [
                              Text(
                                  "${widget.expenses[i].currency}${widget.expenses[i].amount}.00",
                                  style: Theme.of(context)
                                      .textTheme
                                      .bodyLarge!
                                      .apply(color: amountColor)),
                              Text(
                                  DateFormat(ETexts.dateFormat)
                                      .format(widget.expenses[i].date),
                                  style:
                                      Theme.of(context).textTheme.labelMedium),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              );
            }),
      ),
    );
  }
}
