import 'package:flutter/material.dart' show Colors;
import 'package:flutter/widgets.dart';
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
import 'package:rasadyar_auth/data/utils/safe_call.dart';
import 'package:rasadyar_chicken/data/models/response/inventory/inventory_model.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/repositories/chicken_repository.dart';
import 'package:rasadyar_chicken/data/repositories/chicken_repository_imp.dart';
import 'package:rasadyar_chicken/presentation/pages/home/view.dart';
import 'package:rasadyar_chicken/presentation/pages/out_of_province/view.dart';
import 'package:rasadyar_chicken/presentation/pages/sales_in_province/view.dart';
import 'package:rasadyar_chicken/presentation/utils/utils.dart';
import 'package:rasadyar_core/core.dart';

enum ErrorLocationType { serviceDisabled, permissionDenied, none }

class RootLogic extends GetxController {
  RxInt currentPage = 2.obs;
  List<Widget> pages = [
    SalesInProvincePage(),
    OutOfProvincePage(),
    HomePage(),
    Container(color: Colors.blue),
    Container(color: Colors.amber),
  ];

  late DioRemote dioRemote;
  var tokenService = Get.find<TokenStorageService>();
  late ChickenRepository chickenRepository;

  RxList<ErrorLocationType> errorLocationType = RxList();
  RxMap<int, dynamic> inventoryExpandedList = RxMap();
  Rxn<InventoryModel> inventoryModel = Rxn<InventoryModel>();
  RxList<IranProvinceCityModel> provinces = <IranProvinceCityModel>[].obs;

  @override
  void onInit() {
    super.onInit();
    dioRemote = DioRemote(baseUrl: tokenService.baseurl.value);
    dioRemote.init();
    chickenRepository = ChickenRepositoryImpl(dioRemote);

    getProvinces();

    getInventory();
    //getKillHouseDistributionInfo();
  }

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

  Future<void> getInventory() async {
    await safeCall<List<InventoryModel>?>(
      call: () async => await chickenRepository.getInventory(token: tokenService.accessToken.value!),
      onSuccess: (result) {
        if (result != null) {
          inventoryModel.value = result.first;
        }
      },
      onError: (error, stackTrace) {
        switch (error.response?.statusCode) {
          case 401:
            errorHandler(error);
            break;
          case 403:
            errorHandler(error);
            break;
          default:
            errorHandler(error);
        }
      },
    );
  }

  void rootErrorHandler(DioException error) {
    handleGeneric(error, () {
      tokenService.deleteTokens();
    });
  }

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

  Future<void> getProvinces() async {
    try {
      final res = await chickenRepository.getProvince();
      if (res != null) {
        provinces.clear();
        provinces.value = res;
      }
    } catch (e) {
      provinces.clear();
    }
  }

  void errorHandler(DioException error) {
    handleGeneric(error, () {
      tokenService.deleteTokens();
    });
  }
}
