import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../application/checkout/checkout_form/checkout_form_bloc.dart';
import '../../../application/order/order_form/order_form_bloc.dart';
import '../../../application/payment_method/payment_method_loader/payment_method_loader_bloc.dart';
import '../../../application/table/table_loader/table_loader_bloc.dart';
import '../../../common/theme/theme.dart';
import '../../../injection.dart';
import '../../components/spaces/space.dart';
import '../../components/toast/flushbar.dart';
import '../../router/app_router.gr.dart';
import 'widgets/checkout_left_panel.dart';
import 'widgets/checkout_right_panel.dart';

@RoutePage()
class CheckoutPage extends StatelessWidget implements AutoRouteWrapper {
  const CheckoutPage({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiBlocListener(
      listeners: [
        BlocListener<OrderFormBloc, OrderFormState>(
          listenWhen: (previous, current) =>
              previous.failureOrCreateOrderWithPayment !=
              current.failureOrCreateOrderWithPayment,
          listener: (context, state) {
            state.failureOrCreateOrderWithPayment.fold(() {}, (either) {
              either.fold(
                (f) => AppFlushbar.showOrderFailureToast(context, f),
                (order) {
                  if (context.mounted) {
                    context.read<CheckoutFormBloc>().add(
                      const CheckoutFormEvent.started([]),
                    );
                    context.router.replace(SuccessOrderRoute(order: order));
                  }
                },
              );
            });
          },
        ),
        BlocListener<OrderFormBloc, OrderFormState>(
          listenWhen: (previous, current) =>
              previous.failureOrCreateOrder != current.failureOrCreateOrder,
          listener: (context, state) {
            state.failureOrCreateOrder.fold(() {}, (either) {
              either.fold(
                (f) => AppFlushbar.showOrderFailureToast(context, f),
                (order) {
                  if (context.mounted) {
                    context.read<CheckoutFormBloc>().add(
                      const CheckoutFormEvent.started([]),
                    );
                    context.router.replace(SuccessOrderRoute(order: order));
                  }
                },
              );
            });
          },
        ),
        BlocListener<OrderFormBloc, OrderFormState>(
          listenWhen: (previous, current) =>
              previous.failureOrAddItemOrder != current.failureOrAddItemOrder,
          listener: (context, state) {
            state.failureOrAddItemOrder.fold(() {}, (either) {
              either.fold(
                (f) => AppFlushbar.showOrderFailureToast(context, f),
                (order) {
                  if (context.mounted) {
                    context.read<CheckoutFormBloc>().add(
                      CheckoutFormEvent.orderAddedItems(order),
                    );
                    context.router.replace(const SuccessAddItemOrderRoute());
                  }
                },
              );
            });
          },
        ),
      ],
      child: SafeArea(
        child: Hero(
          tag: 'checkout_screen',
          child: BlocBuilder<CheckoutFormBloc, CheckoutFormState>(
            builder: (context, state) {
              final price = state.items.fold(
                0,
                (previousValue, element) =>
                    previousValue +
                    (element.product.price.toInt() +
                            (element.variant?.priceModifier.toInt() ?? 0)) *
                        element.quantity,
              );

              return Scaffold(
                backgroundColor: AppColor.white,
                body: Row(
                  children: [
                    Expanded(
                      flex: 3,
                      child: CheckoutLeftPanel(
                        checkoutState: state,
                        price: price,
                      ),
                    ),
                    const SpaceWidth(2),
                    Expanded(
                      flex: 3,
                      child: CheckoutRightPanel(
                        checkoutState: state,
                        price: price,
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ),
      ),
    );
  }

  @override
  Widget wrappedRoute(BuildContext context) => MultiBlocProvider(
    providers: [
      BlocProvider(
        create: (context) =>
            getIt<PaymentMethodLoaderBloc>()
              ..add(const PaymentMethodLoaderEvent.fetched(isRefresh: true)),
      ),
      BlocProvider(
        create: (context) => getIt<TableLoaderBloc>()
          ..add(
            const TableLoaderEvent.fetched(
              isRefresh: true,
              status: 'available',
            ),
          ),
      ),
    ],
    child: this,
  );
}
