import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/features/calculator/presentation/bloc/calculator_bloc.dart';
import 'package:cargocalculaterapp/features/calculator/presentation/pages/calculator_page.dart';
import 'package:cargocalculaterapp/features/home/presentation/bloc/home_bloc.dart';
import 'package:cargocalculaterapp/features/profile/presentation/bloc/profile_bloc.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import '../../../../injector_container.dart';
import '../../../home/presentation/pages/home_page.dart';
import '../../../profile/presentation/pages/profile_page.dart';
import '../bloc/main_bloc.dart';

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

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (_) => sl<MainBloc>()),
        BlocProvider(create: (_) => sl<ProfileBloc>()),
        BlocProvider(create: (_) => sl<HomeBloc>()),
        BlocProvider(create: (_) => sl<CalculatorBloc>()),
      ],
      child: BlocBuilder<MainBloc, MainState>(
        builder: (context, state) {
          return PopScope(
            canPop: false,
            onPopInvokedWithResult: (value, result) {
              // if (Navigator.canPop(shellRootNavigatorKey.currentContext!)) {
              //   Navigator.pop(shellRootNavigatorKey.currentContext!);
              // } else
              if (state.tab.index != 0) {
                context.read<MainBloc>().add(
                  ChangeTabEvent(tab: MainTab.values[0]),
                );
              } else {
                SystemNavigator.pop();
              }
            },
            child: Scaffold(
              body: IndexedStack(
                index: state.tab.index,
                children: const [HomePage(), CalculatorPage(), ProfilePage()],
              ),
              bottomNavigationBar: BottomNavigationBar(
                currentIndex: state.tab.index,
                onTap: (index) {
                  context.read<MainBloc>().add(
                    ChangeTabEvent(tab: MainTab.values.elementAt(index)),
                  );
                  if (index == 0) {
                    context.read<HomeBloc>().add(const RefreshEvent());
                  }
                },
                elevation: 1,
                iconSize: 24,
                selectedLabelStyle: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w500,
                  color: context.color.primaryColor,
                ),
                unselectedLabelStyle: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w500,
                  color: context.color.lightSecondary,
                ),
                showSelectedLabels: true,
                showUnselectedLabels: true,
                items: [
                  BottomNavigationBarItem(
                    icon: SvgPicture.asset(
                      "assets/svg/ic_box.svg",
                      colorFilter: ColorFilter.mode(
                        state.tab.index == 0
                            ? context.color.primaryColor
                            : context.color.lightSecondary,
                        BlendMode.srcIn,
                      ),
                    ),
                    label: AppLocalization.current.orders,
                  ),
                  BottomNavigationBarItem(
                    icon: SvgPicture.asset(
                      "assets/svg/ic_calculator.svg",
                      colorFilter: ColorFilter.mode(
                        state.tab.index == 1
                            ? context.color.primaryColor
                            : context.color.lightSecondary,
                        BlendMode.srcIn,
                      ),
                    ),
                    label: AppLocalization.current.calculator,
                  ),
                  BottomNavigationBarItem(
                    icon: SvgPicture.asset(
                      "assets/svg/ic_profile.svg",
                      colorFilter: ColorFilter.mode(
                        state.tab.index == 2
                            ? context.color.primaryColor
                            : context.color.lightSecondary,
                        BlendMode.srcIn,
                      ),
                    ),
                    label: AppLocalization.current.profile,
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}
