mirrored_repositories/Expenses_Tracker_App/lib/screens/home
import 'package:expense_repository/expense_repository.dart';
import 'package:expenses_tracker/bloc/create_expense_bloc/create_expense_bloc.dart';
import 'package:expenses_tracker/bloc/get_expenses_bloc/get_expenses_bloc.dart';
import 'package:expenses_tracker/screens/add_expense/views/add_screen.dart';
import 'package:expenses_tracker/screens/graph/statistics.dart';
import 'package:expenses_tracker/screens/home/views/main_screen.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/helpers/helpers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:uuid/uuid.dart';

class HomeScreen extends StatefulWidget {
  final UserModel? userModel;
  const HomeScreen({super.key, this.userModel});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

final TextEditingController expenseController = TextEditingController();
final TextEditingController categoryController = TextEditingController();
final TextEditingController dateController = TextEditingController();

class _HomeScreenState extends State<HomeScreen> {
  int index = 0;
  late Color selectedColor;
  late Expense expense;
  Color unselectedColor = EColors.darkGrey;
  late GetExpensesBloc _getExpensesBloc;
  bool isLoading = false;

  @override
  void initState() {
    dateController.text = DateFormat(ETexts.dateFormat).format(DateTime.now());
    expense = Expense.empty;
    expense.expenseId = const Uuid().v1();
    expense.categoryId = const Uuid().v1();
    selectedColor = EColors.primary;
    _getExpensesBloc = GetExpensesBloc(FirebaseExpenseRepo());
    _getExpensesBloc.add(GetExpenses(Expense.empty));
    super.initState();
  }

  late List<Expense> fetchedExpenses;
  void refreshHomeScreen(List<Expense> updatedExpenses) {
    setState(() {
      fetchedExpenses.clear();
      fetchedExpenses.addAll(updatedExpenses);
    });
  }

  @override
  Widget build(BuildContext context) {
    final dark = EHelperFunctions.isDarkMode(context);

    return BlocBuilder<GetExpensesBloc, GetExpensesState>(
      builder: (context, state) {
        return BlocProvider(
          create: (context) => _getExpensesBloc,
          child: BlocBuilder<GetExpensesBloc, GetExpensesState>(
            builder: (context, state) {
              if (state is GetExpensesLoading) {
                return const Center(child: CircularProgressIndicator());
              }
              if (state is GetExpensesSuccess) {
                final List<Expense> fetchedExpenses = state.expenses;
                return Scaffold(
                  bottomNavigationBar: bottomNavigationBarMethod(dark),
                  floatingActionButtonLocation:
                      FloatingActionButtonLocation.centerDocked,
                  floatingActionButton: FloatingActionButton(
                    onPressed: () async {
                      Expense? newExpense = await Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (BuildContext context) => MultiBlocProvider(
                              providers: [
                                BlocProvider(
                                  create: (context) =>
                                      CreateExpenseBloc(FirebaseExpenseRepo()),
                                ),
                                BlocProvider(
                                  create: (context) =>
                                      GetExpensesBloc(FirebaseExpenseRepo()),
                                ),
                              ],
                              child: AddScreen(
                                refreshHomeScreen:
                                    (List<Expense> updatedExpenses) {
                                  refreshHomeScreen(updatedExpenses);
                                },
                              )),
                        ),
                      );
                      if (newExpense != null) {
                        setState(() {
                          fetchedExpenses.add(newExpense);
                        });
                      }
                    },
                    child: const Icon(CupertinoIcons.add),
                  ),
                  body: index == 0
                      ? MainScreen(fetchedExpenses, refreshHomeScreen)
                      : const Statistics(),
                );
              } else if (state is GetExpensesFailure) {
                return const Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              }
              return Container();
            },
          ),
        );
      },
    );
  }

  // Bottom Navigation Bar Method
  ClipRRect bottomNavigationBarMethod(bool dark) {
    return ClipRRect(
      borderRadius: const BorderRadius.vertical(
          top: Radius.circular(ESizes.borderRadiusXxl)),
      child: BottomNavigationBar(
        onTap: (value) {
          setState(() {
            index = value;
          });
          // ignore: avoid_print
          print(value);
        },
        backgroundColor: dark ? EColors.darkContainer : EColors.lightContainer,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        items: [
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.home,
              color: index == 0 ? selectedColor : unselectedColor,
            ),
            label: ETexts.bottomHome,
          ),
          BottomNavigationBarItem(
            icon: Icon(
              CupertinoIcons.graph_square,
              color: index == 1 ? selectedColor : unselectedColor,
            ),
            label: ETexts.bottomStats,
          ),
        ],
      ),
    );
  }
}
