import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rasadyar_chicken/presentation/routes/pages.dart';
import 'package:rasadyar_chicken/presentation/routes/routes.dart';
import 'package:rasadyar_core/core.dart';

import 'logic.dart';

class PoultryScienceRootPage extends GetView<PoultryScienceRootLogic> {
  PoultryScienceRootPage({super.key});

  DateTime? _lastBackPressed;

  @override
  Widget build(BuildContext context) {
    return ObxValue((data) {
      return PopScope(
        canPop: false,
        onPopInvokedWithResult: (didPop, result) async {
          final nestedKey = Get.nestedKey(controller.currentPage.value);
          final currentNavigator = nestedKey?.currentState;

          if (currentNavigator?.canPop() ?? false) {
            currentNavigator?.pop();
          } else {
            final now = DateTime.now();
            if (_lastBackPressed == null ||
                now.difference(_lastBackPressed!) > Duration(seconds: 2)) {
              _lastBackPressed = now;
              Get.snackbar(
                'خروج از برنامه',
                'برای خروج دوباره بازگشت را بزنید',
                snackPosition: SnackPosition.TOP,
                duration: Duration(seconds: 2),
                backgroundColor: AppColor.warning,
              );
            } else {
              await SystemNavigator.pop();
            }
          }
        },
        child: Scaffold(
          backgroundColor: AppColor.bgLight,
          body: IndexedStack(
            children: [
              /* Navigator(
                key: Get.nestedKey(0),
                onGenerateRoute: (settings) {
                  final page = ChickenPages.pages.firstWhere(
                    (e) => e.name == settings.name,
                    orElse: () =>
                        ChickenPages.pages.firstWhere((e) => e.name == ChickenRoutes.buySteward),
                  );

                  return buildRouteFromGetPage(page);
                },
              ),
              Navigator(
                key: Get.nestedKey(1),
                onGenerateRoute: (settings) {
                  final page = ChickenPages.pages.firstWhere(
                    (e) => e.name == settings.name,
                    orElse: () =>
                        ChickenPages.pages.firstWhere((e) => e.name == ChickenRoutes.saleSteward),
                  );

                  return buildRouteFromGetPage(page);
                },
              ),*/
              Navigator(
                key: Get.nestedKey(0),
                onGenerateRoute: (settings) {
                  final page = ChickenPages.pages.firstWhere(
                    (e) => e.name == settings.name,
                    orElse: () => ChickenPages.pages.firstWhere(
                      (e) => e.name == ChickenRoutes.actionPoultryScience,
                    ),
                  );

                  return buildRouteFromGetPage(page);
                },
              ),
              Navigator(
                key: Get.nestedKey(1),
                onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[1]),
              ),
              Navigator(
                key: Get.nestedKey(2),
                onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[2]),
              ),
            ],
            index: data.value,
          ),

          bottomNavigationBar: RBottomNavigation(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            items: [
              RBottomNavigationItem(
                label: 'اقدام',
                icon: Assets.vec.settingSvg.path,
                isSelected: controller.currentPage.value == 0,
                onTap: () {
                  Get.nestedKey(1)?.currentState?.popUntil((route) => route.isFirst);

                  controller.changePage(0);
                },
              ),
              RBottomNavigationItem(
                label: 'خانه',
                icon: Assets.vec.homeSvg.path,
                isSelected: controller.currentPage.value == 1,
                onTap: () {
                    Get.nestedKey(0)?.currentState?.popUntil((route) => route.isFirst);
                  Get.nestedKey(2)?.currentState?.popUntil((route) => route.isFirst);
                  controller.changePage(1);
                },
              ),
              RBottomNavigationItem(
                label: 'پروفایل',
                icon: Assets.vec.profileCircleSvg.path,
                isSelected: controller.currentPage.value == 2,
                onTap: () {
                  /*        Get.nestedKey(1)?.currentState?.popUntil((route) => route.isFirst);
                  Get.nestedKey(0)?.currentState?.popUntil((route) => route.isFirst);
*/
                  controller.changePage(2);
                },
              ),
            ],
          ),
        ),
      );
    }, controller.currentPage);
  }
}
