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_chicken/presentation/utils/nested_keys_utils.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.getNestedKey());
          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: Stack(
            children: [
              IndexedStack(
                children: [
                  Navigator(
                    key: Get.nestedKey(poultryFirstKey),
                    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(poultrySecondKey),
                    onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[1]),
                  ),
                  Navigator(
                    key: Get.nestedKey(poultryThirdKey),
                    onGenerateRoute: (settings) => GetPageRoute(page: () => controller.pages[2]),
                  ),
                ],
                index: data.value,
              ),
              Positioned(
                right: 0,
                left: 0,
                bottom: 0,
                child: RBottomNavigation(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  items: [
                    RBottomNavigationItem(
                      label: 'عملیات',
                      icon: Assets.vec.settingSvg.path,
                      isSelected: controller.currentPage.value == 0,
                      onTap: () {
                        Get.nestedKey(
                          poultrySecondKey,
                        )?.currentState?.popUntil((route) => route.isFirst);
                        Get.nestedKey(
                          poultryFirstKey,
                        )?.currentState?.popUntil((route) => route.isFirst);
                        controller.changePage(0);
                      },
                    ),
                    RBottomNavigationItem(
                      label: 'خانه',
                      icon: Assets.vec.homeSvg.path,
                      isSelected: controller.currentPage.value == 1,
                      onTap: () {
                        Get.nestedKey(
                          poultryFirstKey,
                        )?.currentState?.popUntil((route) => route.isFirst);
                        Get.nestedKey(
                          poultryThirdKey,
                        )?.currentState?.popUntil((route) => route.isFirst);
                        controller.changePage(1);
                      },
                    ),
                    RBottomNavigationItem(
                      label: 'پروفایل',
                      icon: Assets.vec.profileCircleSvg.path,
                      isSelected: controller.currentPage.value == 2,
                      onTap: () {
                        Get.nestedKey(
                          poultryFirstKey,
                        )?.currentState?.popUntil((route) => route.isFirst);
                        Get.nestedKey(
                          poultrySecondKey,
                        )?.currentState?.popUntil((route) => route.isFirst);
                        controller.changePage(2);
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    }, controller.currentPage);
  }
}
