import 'package:flutter/material.dart';
import 'package:rasadyar_app/data/model/response/slider/slider_model.dart';
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
import 'package:rasadyar_core/core.dart';

class ModulesLogic extends GetxController {
  TokenStorageService tokenService = Get.find<TokenStorageService>();
  SliderLogic upSlider = Get.find<SliderLogic>(tag: "up");
  SliderLogic downSlider = Get.find<SliderLogic>(tag: "down");
  RxnString latestNews = RxnString();
  RxBool isLoading = false.obs;

  List<ModuleModel> moduleList = [
    ModuleModel(
      title: 'رصدطیور',
      icon: Assets.icons.rasadToyor.path,
      module: Module.chicken,
      borderColor: Color(0xFF4665AF),
      backgroundColor: Color(0xFFECEEF2),
      titleColor: Color(0xFF4665AF),
    ),
    ModuleModel(
      title: 'رصدام',
      icon: Assets.icons.rasadDam.path,
      module: Module.liveStocks,
      borderColor: Color(0xFFD7A972),
      backgroundColor: Color(0xFFF4F1EF),
      titleColor: Color(0xFF7F7F7F),
    ),
    ModuleModel(
      title: 'رصدبان',
      icon: Assets.icons.rasadBan.path,
      module: Module.inspection,
      borderColor: Color(0xFF014856),
      backgroundColor: Color(0xFFE9EDED),
      titleColor: Color(0xFF014856),
    ),
    ModuleModel(
      title: 'رصدبار',
      icon: Assets.icons.rasadBar.path,
      borderColor: Color(0xFFF37021),
      backgroundColor: Color(0xFFFFECE1),
      titleColor: Color(0xFFF37021),
    ),
    ModuleModel(
      title: 'رصدبات',
      icon: Assets.icons.rasadBot.path,
      borderColor: Color(0xFF4A148C),
      backgroundColor: Color(0xFFEDEAF0),
      titleColor: Color(0xFF4A148C),
    ),
    ModuleModel(
      title: 'رصدنان',
      icon: Assets.icons.rasadNan.path,
      borderColor: Color(0xFFD7A972),
      backgroundColor: Color(0xFFF4F2EA),
      titleColor: Color(0xFF8E8E8E),
    ),
  ];

  RxnInt selectedIndex = RxnInt(null);

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

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

  @override
  void dispose() {
    eLog("ModulesLogic disposed");
    super.dispose();
  }

  void saveModule(Module module) {
    tokenService.saveModule(module);
    tokenService.appModule.value = module;
  }

  void onTapCard(Module? module, int index) async {
    if (module == null) {
      if (Get.isSnackbarOpen) return;
      Get.snackbar(
        "در حال توسعه",
        "این ماژول به زودی اضافه می‌شود",
        snackPosition: SnackPosition.BOTTOM,
        backgroundColor: AppColor.yellowDark,
      );
    } else {
      _goToModule(module, index);
    }
  }

  void _goToModule(Module module, int index) async {
    selectedIndex.value = index;
    await Future.delayed(Duration(milliseconds: 300));
    selectedIndex.value = null;
    //  saveModule(module);
    await navigateToModule(module);
  }

  Future<void> navigateToModule(Module module) async {
    var target = getAuthTargetPage(module).entries.first;

    if (target.value?[0] != null) {
      isLoading.value = !isLoading.value;
      await target.value?[0]?.call();
      isLoading.value = !isLoading.value;
    }

    var res = await Get.toNamed(target.key, arguments: module);

    if (target.value?[1] != null && res == -1) {
      await target.value?[1]?.call();
    }
  }

  Future<void> getSliders() async {
    var dio = Dio();
    dio.interceptors.add(
      PrettyDioLogger(
        request: true,
        enabled: true,
        requestHeader: true,
        responseHeader: true,
        requestBody: true,
        responseBody: true,
      ),
    );
    var res = await dio.get("https://miran.storage.c2.liara.space/app/urllapp.json");
    if (res.statusCode == 200) {
      SliderModel sliderModel = SliderModel.fromJson(res.data);
      upSlider.onSuccess(sliderModel.up ?? []);
      downSlider.onSuccess(sliderModel.down ?? []);
      latestNews.value = sliderModel.middle;
    }
  }
}
