import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/request/steward_free_sale_bar/steward_free_sale_bar_request.dart';
import 'package:rasadyar_chicken/data/models/response/iran_province_city/iran_province_city_model.dart';
import 'package:rasadyar_chicken/data/models/response/out_province_carcasses_buyer/out_province_carcasses_buyer.dart';
import 'package:rasadyar_chicken/data/models/response/roles_products/roles_products.dart';
import 'package:rasadyar_chicken/data/models/response/steward_free_sale_bar/steward_free_sale_bar.dart';
import 'package:rasadyar_chicken/presentation/pages/steward/root/logic.dart';
import 'package:rasadyar_chicken/presentation/pages/steward/sale/logic.dart';
import 'package:rasadyar_chicken/presentation/pages/steward/sales_out_of_province_buyers/logic.dart';
import 'package:rasadyar_chicken/presentation/utils/utils.dart';
import 'package:rasadyar_core/core.dart';

class SalesOutOfProvinceSalesListLogic extends GetxController {
  StewardRootLogic rootLogic = Get.find<StewardRootLogic>();

  SaleLogic saleLogic = Get.find<SaleLogic>();

  SalesOutOfProvinceBuyersLogic buyerLogic = Get.find<SalesOutOfProvinceBuyersLogic>();

  RxInt selectedSegmentIndex = 0.obs;
  RxBool isExpanded = false.obs;
  RxInt currentPage = 1.obs;
  RxBool isSaleSubmitButtonEnabled = false.obs;
  RxInt expandedListIndex = (-1).obs;
  Rx<Jalali> fromDateFilter = Jalali.now().obs;
  Rx<Jalali> toDateFilter = Jalali.now().obs;
  RxnString searchedValue = RxnString();
  RxList<String> routesName = RxList();
  RxBool isLoadingMoreAllocationsMade = false.obs;
  Rxn<IranProvinceCityModel> selectedCity = Rxn();

  //TODO add this to Di
  ImagePicker imagePicker = ImagePicker();



  RxInt saleType = 1.obs;
  RxInt quotaType = 1.obs;


  GlobalKey<FormState> formKey = GlobalKey<FormState>();
  TextEditingController quarantineCodeController = TextEditingController();
  TextEditingController saleWeightController = TextEditingController();
  TextEditingController saleCountController = TextEditingController();
  Rx<Jalali> saleDate = Jalali.now().obs;
  String? key;

  Rx<Resource<PaginationModel<StewardFreeSaleBar>>> salesList =
      Resource<PaginationModel<StewardFreeSaleBar>>.loading().obs;

  Rxn<ProductModel> selectedProduct = Rxn();
  Rxn<OutProvinceCarcassesBuyer> selectedBuyer = Rxn();

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

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

    selectedProduct.value = rootLogic.rolesProductsModel.first;
    debounce(
      searchedValue,
      (callback) => getOutProvinceSales(),
      time: Duration(milliseconds: timeDebounce),
    );
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  Future<void> getOutProvinceSales([bool isLoadingMore = false]) async {
    if (isLoadingMore) {
      isLoadingMoreAllocationsMade.value = true;
    } else {
      salesList.value = Resource<PaginationModel<StewardFreeSaleBar>>.loading();
    }
    await safeCall(
      call: () => rootLogic.chickenRepository.getStewardFreeSaleBar(
        token: rootLogic.tokenService.accessToken.value!,
        queryParameters: buildQueryParams(
          pageSize: 20,
          page: currentPage.value,
          state: 'buyer-list',
          search: 'filter',
          role: 'Steward',
          value: searchedValue.value ?? '',
          fromDate: fromDateFilter.value.toDateTime(),
          toDate: toDateFilter.value.toDateTime(),
        ),
      ),
      onSuccess: (res) {
        if ((res?.count ?? 0) == 0) {
          salesList.value = Resource<PaginationModel<StewardFreeSaleBar>>.empty();
        } else {
          salesList.value = Resource<PaginationModel<StewardFreeSaleBar>>.success(
            PaginationModel<StewardFreeSaleBar>(
              count: res?.count ?? 0,
              next: res?.next,
              previous: res?.previous,
              results: [...(salesList.value.data?.results ?? []), ...(res?.results ?? [])],
            ),
          );

          isLoadingMoreAllocationsMade.value = false;
        }
      },
    );
  }

  void setupListeners() {

    quarantineCodeController.addListener(checkSalesFormValid);
    ever(selectedBuyer, (_) => checkSalesFormValid);
    ever(selectedProduct, (_) => checkSalesFormValid);
    ever(saleDate, (_) => checkSalesFormValid());
  }

  void checkSalesFormValid() {
    isSaleSubmitButtonEnabled.value =
        saleDate.value.toString().isNotEmpty &&
        selectedProduct.value != null &&
        selectedBuyer.value != null &&
        saleWeightController.text.isNotEmpty &&
        quarantineCodeController.text.isNotEmpty;
  }

  void setEditDataSales(StewardFreeSaleBar item) {
    quarantineCodeController.text = item.clearanceCode ?? '';
    saleWeightController.text = item.weightOfCarcasses?.toInt().toString() ?? '';
    saleDate.value = Jalali.fromDateTime(DateTime.parse(item.date!));
    selectedCity.value = IranProvinceCityModel(name: item.city);
    selectedBuyer.value = buyerLogic.buyerList.value.data?.results?.firstWhere(
      (element) => element.key == item.buyer?.key,
    );
    selectedProduct.value = rootLogic.rolesProductsModel.first;
    key = item.key;
    isSaleSubmitButtonEnabled.value = true;
  }

  Future<void> deleteStewardPurchaseOutOfProvince(String key) async {
    //todo
    /* await safeCall(
      call: () => rootLogic.chickenRepository
          .editStewardPurchasesOutSideOfTheProvince(
            token: rootLogic.tokenService.accessToken.value!,
            stewardFreeBarKey: key,
          ),
    );*/
  }

  Future<bool> createSale() async {
    bool res = false;
    var tmpWight = int.tryParse(saleWeightController.text.clearComma);
    var tmpCount = (tmpWight! / selectedProduct.value!.weightAverage!).round();

    StewardFreeSaleBarRequest requestBody = StewardFreeSaleBarRequest(
      buyerKey: selectedBuyer.value?.key,
      numberOfCarcasses: tmpCount,
      weightOfCarcasses: tmpWight,
      date: saleDate.value.toDateTime().formattedDashedGregorian,
      clearanceCode: quarantineCodeController.text,
      productKey: selectedProduct.value?.key,
    );
    await safeCall(
      call: () => rootLogic.chickenRepository.createOutProvinceStewardFreeBar(
        token: rootLogic.tokenService.accessToken.value!,
        body: requestBody,
      ),
      onSuccess: (_) {
        res = true;
      },
    );
    return res;
  }

  void clearSaleForm() {
    quarantineCodeController.clear();
    saleWeightController.clear();
    saleDate.value = Jalali.now();
    selectedBuyer.value = null;
    selectedProduct.value = null;
  }

  Future<bool> editSale() async {
    bool res = false;
    StewardFreeSaleBarRequest requestBody = StewardFreeSaleBarRequest(
      numberOfCarcasses: 0,
      weightOfCarcasses: int.tryParse(saleWeightController.text.clearComma),
      date: saleDate.value.toDateTime().formattedDashedGregorian,
      clearanceCode: quarantineCodeController.text,
      key: key,
    );
    await safeCall(
      call: () => rootLogic.chickenRepository.updateOutProvinceStewardFreeBar(
        token: rootLogic.tokenService.accessToken.value!,
        body: requestBody,
      ),
      onSuccess: (_) {
        res = true;
      },
    );
    return res;
  }

  void resetSubmitForm() {
    selectedCity.value = null;
    selectedProduct.value = null;
    key = null;
  }
  void toggleExpansion({int? index}) {
    if (expandedListIndex.value == index || index == null) {
      expandedListIndex.value = -1;
    } else {
      expandedListIndex.value = index;
    }
  }
}
