import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_pos_ecommerce/core/components/search_input_pos.dart';
import 'package:flutter_pos_ecommerce/core/components/spaces.dart';
import 'package:flutter_pos_ecommerce/module/ecommerce/presentation/home/bloc/alat_pertanian/alat_pertanian_bloc.dart';
import 'package:flutter_pos_ecommerce/module/ecommerce/presentation/home/widgets/organism/product_list.dart';
import 'package:go_router/go_router.dart';

import '../../../../../core/assets/assets.gen.dart';
import '../../../../../core/router/app_router.dart';
import '../bloc/checkout/checkout_bloc.dart';
import 'package:badges/badges.dart' as badges;

class AlatPertanianPage extends StatefulWidget {
  const AlatPertanianPage({super.key});

  @override
  State<AlatPertanianPage> createState() => _AlatPertanianPageState();
}

class _AlatPertanianPageState extends State<AlatPertanianPage> {
  late TextEditingController searchController;
  @override
  void initState() {
    context
        .read<AlatPertanianBloc>()
        .add(const AlatPertanianEvent.getProductAlatPertanian());
    searchController = TextEditingController();
    super.initState();
  }

  @override
  void dispose() {
    searchController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Alat Pertanian'),
        actions: [
          BlocBuilder<CheckoutBloc, CheckoutState>(
            builder: (context, state) {
              return state.maybeWhen(
                loaded: (checkout, _, __, ___, ____, ______, _____) {
                  final totalQuantity = checkout.fold<int>(
                    0,
                    (previousValue, element) =>
                        previousValue + element.quantity,
                  );
                  return totalQuantity > 0
                      ? badges.Badge(
                          badgeContent: Text(
                            totalQuantity.toString(),
                            style: const TextStyle(color: Colors.white),
                          ),
                          child: IconButton(
                            onPressed: () {
                              context.goNamed(
                                RouteConstants.cart,
                                pathParameters: PathParameters().toMap(),
                              );
                            },
                            icon: Assets.icons.cart.svg(height: 24.0),
                          ),
                        )
                      : IconButton(
                          onPressed: () {
                            context.goNamed(
                              RouteConstants.cart,
                              pathParameters: PathParameters().toMap(),
                            );
                          },
                          icon: Assets.icons.cart.svg(height: 24.0),
                        );
                },
                orElse: () => const SizedBox.shrink(),
              );
            },
          ),
          const SizedBox(
            width: 16,
          )
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          SearchInput(
            controller: searchController,
            onChanged: (value) {
              if (value.length > 3) {
                context
                    .read<AlatPertanianBloc>()
                    .add(AlatPertanianEvent.searchProductAlatPertanian(value));
              }
              if (value.isEmpty) {
                context
                    .read<AlatPertanianBloc>()
                    .add(const AlatPertanianEvent.fetchAllAlatPertanian());
              }
            },
          ),
          const SpaceHeight(20.0),
          BlocBuilder<AlatPertanianBloc, AlatPertanianState>(
            builder: (context, state) {
              return state.maybeWhen(
                orElse: () => const SizedBox.shrink(),
                error: (message) => Center(
                  child: Text(message),
                ),
                loading: () => const Center(
                  child: CircularProgressIndicator(),
                ),
                loaded: (products) {
                  return ProductList(
                    title: 'Pestisida',
                    onSeeAllTap: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const AlatPertanianPage()));
                    },
                    items: products,
                  );
                },
              );
            },
          ),
        ],
      ),
    );
  }
}
