import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/di/chicken_di.dart';
import 'package:rasadyar_chicken/data/repositories/poultry_science/poultry_science_repository.dart';
import 'package:rasadyar_chicken/presentation/pages/common/profile/view.dart';
import 'package:rasadyar_chicken/presentation/pages/poultry_science/home/view.dart';
import 'package:rasadyar_chicken/presentation/pages/poultry_science/poultry_action/view.dart';
import 'package:rasadyar_chicken/presentation/utils/nested_keys_utils.dart';
import 'package:rasadyar_chicken/presentation/utils/utils.dart';
import 'package:rasadyar_core/core.dart';

enum ErrorLocationType { serviceDisabled, permissionDenied, none }

class PoultryScienceRootLogic extends GetxController {
  RxInt currentPage = 1.obs;
  List<Widget> pages = [PoultryActionPage(), PoultryScienceHomePage(), ProfilePage()];

  var tokenService = Get.find<TokenStorageService>();

  late PoultryScienceRepository poultryRepository;

  RxList<ErrorLocationType> errorLocationType = RxList();
  RxMap<int, dynamic> homeExpandedList = RxMap();

  @override
  void onInit() {
    super.onInit();
    poultryRepository = diChicken.get<PoultryScienceRepository>();
  }

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

  void toggleExpanded(int index) {
    if (homeExpandedList.keys.contains(index)) {
      homeExpandedList.remove(index);
    } else {
      homeExpandedList[index] = false;
    }
  }

  void rootErrorHandler(DioException error) {
    handleGeneric(error, () {
      tokenService.deleteModuleTokens(Module.chicken);
    });
  }

  void changePage(int index) {
    currentPage.value = index;
  }

  int getNestedKey() {
    switch (currentPage.value) {
      case 0:
        return poultryFirstKey;
      case 1:
        return poultrySecondKey;
      case 2:
        return poultryThirdKey;
      default:
        return poultryFirstKey;
    }
  }
}
