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

import '../../../../../application/analytic/category_analytic_loader/category_analytic_loader_bloc.dart';
import '../../../../../application/analytic/dashboard_analytic_loader/dashboard_analytic_loader_bloc.dart';
import '../../../../../application/analytic/inventory_analytic_loader/inventory_analytic_loader_bloc.dart';
import '../../../../../application/analytic/payment_method_analytic_loader/payment_method_analytic_loader_bloc.dart';
import '../../../../../application/analytic/product_analytic_loader/product_analytic_loader_bloc.dart';
import '../../../../../application/analytic/profit_loss_analytic_loader/profit_loss_analytic_loader_bloc.dart';
import '../../../../../application/analytic/sales_analytic_loader/sales_analytic_loader_bloc.dart';
import '../../../../../application/report/report_bloc.dart';
import '../../../../../common/data/report_menu.dart';
import '../../../../../common/theme/theme.dart';
import '../../../../../injection.dart';
import '../../../../components/button/button.dart';
import '../../../../components/page/page_title.dart';
import '../../../../components/picker/date_range_picker.dart';
import '../../../../components/spaces/space.dart';
import '../../../../router/app_router.gr.dart';
import 'sections/report_category_section.dart';
import 'sections/report_dashboard_section.dart';
import 'sections/report_inventory_section.dart';
import 'sections/report_payment_method_section.dart';
import 'sections/report_product_section.dart';
import 'sections/report_profit_loss_section.dart';
import 'sections/report_sales_section.dart';
import 'widgets/report_menu_card.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.white,
      body: SafeArea(
        child: BlocBuilder<ReportBloc, ReportState>(
          builder: (context, state) {
            return Column(
              children: [
                PageTitle(
                  isBack: false,
                  title: 'Report',
                  subtitle: state.rangeDateFormatted,
                  actionWidget: [
                    AppIconButton(
                      icons: Icons.calendar_month_outlined,
                      onTap: () {
                        DateRangePickerModal.show(
                          context: context,
                          initialEndDate: state.endDate,
                          initialStartDate: state.startDate,
                          primaryColor: AppColor.primary,
                          onChanged: (startDate, endDate) {
                            context.read<ReportBloc>().add(
                              ReportEvent.dateChanged(
                                startDate: startDate!,
                                endDate: endDate!,
                              ),
                            );
                          },
                        );
                      },
                    ),
                    const SpaceWidth(8),
                    AppIconButton(icons: Icons.download_outlined, onTap: () {}),
                    const SpaceWidth(8),
                    AppIconButton(
                      icons: Icons.refresh_outlined,
                      onTap: () => onFetched(context, state),
                    ),
                  ],
                ),
                Expanded(
                  child: Row(
                    children: [
                      Expanded(
                        flex: 2,
                        child: Material(
                          color: AppColor.white,
                          child: SingleChildScrollView(
                            child: Column(
                              children: List.generate(
                                reportMenus.length,
                                (index) => ReportMenuCard(
                                  menu: reportMenus[index],
                                  isActive:
                                      reportMenus[index].index ==
                                      state.selectedMenu,
                                  onTap: () {
                                    if (reportMenus[index].index == 1) {
                                      context.router.push(
                                        OrderRoute(status: 'completed'),
                                      );
                                    } else {
                                      context.read<ReportBloc>().add(
                                        ReportEvent.menuChanged(
                                          index: index,
                                          title: reportMenus[index].title,
                                        ),
                                      );

                                      onFetched(context, state);
                                    }
                                  },
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 4,
                        child: Material(
                          color: AppColor.background,
                          child: switch (state.selectedMenu) {
                            0 =>
                              BlocBuilder<
                                DashboardAnalyticLoaderBloc,
                                DashboardAnalyticLoaderState
                              >(
                                builder: (context, dashboard) {
                                  return ReportDashboardSection(
                                    menu: reportMenus[state.selectedMenu],
                                    state: dashboard,
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            1 => Text(state.title),
                            2 =>
                              BlocBuilder<
                                SalesAnalyticLoaderBloc,
                                SalesAnalyticLoaderState
                              >(
                                builder: (context, sales) {
                                  return ReportSalesSection(
                                    menu: reportMenus[state.selectedMenu],
                                    state: sales,
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            3 =>
                              BlocBuilder<
                                ProductAnalyticLoaderBloc,
                                ProductAnalyticLoaderState
                              >(
                                builder: (context, product) {
                                  return ReportProductSection(
                                    state: product,
                                    menu: reportMenus[state.selectedMenu],
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            4 =>
                              BlocBuilder<
                                PaymentMethodAnalyticLoaderBloc,
                                PaymentMethodAnalyticLoaderState
                              >(
                                builder: (context, paymentMethod) {
                                  return ReportPaymentMethodSection(
                                    menu: reportMenus[state.selectedMenu],
                                    state: paymentMethod,
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            5 =>
                              BlocBuilder<
                                ProfitLossAnalyticLoaderBloc,
                                ProfitLossAnalyticLoaderState
                              >(
                                builder: (context, profitLoss) {
                                  return ReportProfitLossSection(
                                    menu: reportMenus[state.selectedMenu],
                                    state: profitLoss,
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            6 =>
                              BlocBuilder<
                                InventoryAnalyticLoaderBloc,
                                InventoryAnalyticLoaderState
                              >(
                                builder: (context, inventory) {
                                  return ReportInventorySection(
                                    menu: reportMenus[state.selectedMenu],
                                    state: inventory,
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            7 =>
                              BlocBuilder<
                                CategoryAnalyticLoaderBloc,
                                CategoryAnalyticLoaderState
                              >(
                                builder: (context, category) {
                                  return ReportCategorySection(
                                    menu: reportMenus[state.selectedMenu],
                                    state: category,
                                    startDate: state.startDate,
                                    endDate: state.endDate,
                                  );
                                },
                              ),
                            _ => Container(),
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  void onFetched(BuildContext context, ReportState state) {
    switch (state.selectedMenu) {
      case 0:
        return context.read<DashboardAnalyticLoaderBloc>().add(
          DashboardAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );
      case 1:
      case 2:
        return context.read<SalesAnalyticLoaderBloc>().add(
          SalesAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );
      case 3:
        return context.read<ProductAnalyticLoaderBloc>().add(
          ProductAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );
      case 4:
        return context.read<PaymentMethodAnalyticLoaderBloc>().add(
          PaymentMethodAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );

      case 5:
        return context.read<ProfitLossAnalyticLoaderBloc>().add(
          ProfitLossAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );
      case 6:
        return context.read<InventoryAnalyticLoaderBloc>().add(
          InventoryAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );
      case 7:
        return context.read<CategoryAnalyticLoaderBloc>().add(
          CategoryAnalyticLoaderEvent.fetched(
            startDate: state.startDate,
            endDate: state.endDate,
          ),
        );
      default:
        return;
    }
  }

  @override
  Widget wrappedRoute(BuildContext context) => MultiBlocProvider(
    providers: [
      BlocProvider(create: (context) => getIt<ReportBloc>()),
      BlocProvider(
        create: (context) => getIt<DashboardAnalyticLoaderBloc>()
          ..add(
            DashboardAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
      BlocProvider(
        create: (context) => getIt<SalesAnalyticLoaderBloc>()
          ..add(
            SalesAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
      BlocProvider(
        create: (context) => getIt<ProductAnalyticLoaderBloc>()
          ..add(
            ProductAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
      BlocProvider(
        create: (context) => getIt<PaymentMethodAnalyticLoaderBloc>()
          ..add(
            PaymentMethodAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
      BlocProvider(
        create: (context) => getIt<ProfitLossAnalyticLoaderBloc>()
          ..add(
            ProfitLossAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
      BlocProvider(
        create: (context) => getIt<CategoryAnalyticLoaderBloc>()
          ..add(
            CategoryAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
      BlocProvider(
        create: (context) => getIt<InventoryAnalyticLoaderBloc>()
          ..add(
            InventoryAnalyticLoaderEvent.fetched(
              startDate: DateTime.now().subtract(const Duration(days: 30)),
              endDate: DateTime.now(),
            ),
          ),
      ),
    ],
    child: this,
  );
}
