import 'dart:math';

import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_livestock/presentation/page/map/view.dart';
import 'package:rasadyar_livestock/presentation/page/profile/view.dart';
import 'package:rasadyar_livestock/presentation/page/request_tagging/view.dart';
import 'package:rasadyar_livestock/presentation/page/requests/view.dart';
import 'package:rasadyar_livestock/presentation/routes/app_pages.dart';

class RootLogic extends GetxController {
  // Unique nested keys for each Navigator
  final List<GlobalKey<NavigatorState>> navigatorKeys = [
    ?Get.nestedKey(0), // Map
    ?Get.nestedKey(1), // Requests / RequestTagging
    ?Get.nestedKey(2), // Profile
  ];

  List<Widget> get pages => [
    Navigator(
      key: navigatorKeys[0],
      onGenerateRoute: (settings) => GetPageRoute(page: () => MapPage()),
    ),

    Navigator(
      key: navigatorKeys[1],
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case LiveStockRoutes.requests:
            return GetPageRoute(page: () => RequestsPage());
          case LiveStockRoutes.requestTagging:
            return GetPageRoute(page: () => RequestTaggingPage());
          default:
            return GetPageRoute(page: () => RequestsPage());
        }
      },
    ),

    Navigator(
      key: navigatorKeys[2],
      onGenerateRoute: (settings) => GetPageRoute(page: () => ProfilePage()),
    ),
  ];

  RxInt currentIndex = 0.obs;
  TokenStorageService tokenService = Get.find<TokenStorageService>();

  @override
  void onInit() {
    super.onInit();
  }


  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void changePage(int index) {
    if (index == currentIndex.value) {
      // Optional: pop to first route if the same tab is tapped
      navigatorKeys[index].currentState?.popUntil((route) => route.isFirst);
    } else {
      currentIndex.value = index;
    }
  }
}
