import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_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 '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(
                  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!,
                              ),
                            );
                          },
                        );
                      },
                    ),
                    SpaceWidth(8),
                    AppIconButton(icons: Icons.download_outlined, onTap: () {}),
                  ],
                ),
                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,
                                        ),
                                      );
                                    }
                                  },
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 4,
                        child: Material(
                          color: AppColor.background,
                          child: switch (state.selectedMenu) {
                            0 => Text(state.title),
                            1 => Text(state.title),
                            2 => Text(state.title),
                            3 => Text(state.title),
                            4 => Text(state.title),
                            5 => Text(state.title),
                            6 => Text(state.title),
                            7 => Text(state.title),
                            _ => Container(),
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  @override
  Widget wrappedRoute(BuildContext context) =>
      BlocProvider(create: (context) => getIt<ReportBloc>(), child: this);
}
