import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krezus/domains/category/category_bloc.dart';
import 'package:krezus/domains/transaction/transaction_bloc.dart';
import 'package:krezus/repositories/metadata/models/category.dart' as krezus_category;

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

  @override
  Widget build(BuildContext context) {
    final categoryState = context.watch<CategoryBloc>().state;
    return BlocBuilder<TransactionBloc, TransactionState>(
      buildWhen: (previous, current) => previous.categoryFilter != current.categoryFilter,
      builder: (context, state) => SizedBox(
        width: 500,
        child: DropdownButtonFormField<krezus_category.Category>(
          value: state.categoryFilter,
          onChanged: (value) => context.read<TransactionBloc>().add(TransactionFilterCategory(value!)),
          items: categoryState.categories.map((e) => DropdownMenuItem(value: e, child: Text(e.label))).toList(),
          decoration: InputDecoration(
            suffixIcon: IconButton(
              icon: const Icon(Icons.filter_alt_off),
              onPressed: () => context.read<TransactionBloc>().add(const TransactionFilterCategory(null)),
            ),
            hintText: 'Category',
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(5),
            ),
          ),
        ),
      ),
    );
  }
}