import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krezus/domains/transaction/transaction_bloc.dart';
import 'package:krezus/pages/transactions/widgets/transactions_actions.dart';
import 'package:krezus/pages/transactions/widgets/transactions_header.dart';
import 'package:krezus/pages/transactions/widgets/transactions_list.dart';

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

  @override
  Widget build(BuildContext context) {
    MediaQueryData mediaQuery = MediaQuery.of(context);
    return BlocListener<TransactionBloc, TransactionState>(
      listenWhen: (previous, current) => previous.showSnackBar != current.showSnackBar,
      listener: (context, state) {
        if (state.showSnackBar) {
          ScaffoldMessenger
          .of(context)
          .showSnackBar(
            SnackBar(
              backgroundColor: state.snackBarIsError ? Colors.red : Colors.green,
              content: Text(state.snackBarMessage),
            ),
          );
          context.read<TransactionBloc>().add(const TransactionResetSnackBar());
        }
      },
      child: Center(
        child: Container(
          constraints: const BoxConstraints(maxWidth: 1000),
          padding: mediaQuery.padding,
          child: const Column(
            children: [
              TransactionsActions(),
              TransactionsHeader(),
              TransactionsList(),
            ],
          ),
        ),
      ),
    );
  }
}
