import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/main.dart';
import 'package:mc_common_app/models/general_models/enums_model.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/general_models/widgets_models.dart';
import 'package:mc_common_app/models/shipping_models/shipping_status_model.dart';
import 'package:mc_common_app/repositories/common_repo.dart';
import 'package:mc_common_app/repositories/shipping_repo.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/base_view_model.dart';

class ShippingManagementVM extends BaseVM {
  final ShippingRepo shippingRepo;
  final CommonRepo commonRepo;

  ShippingManagementVM({required this.shippingRepo, required this.commonRepo});

  List<ShippingRequestModel> shippingRequestsList = [];

  List<EnumsModel> shippingStatusEnums = [];

  List<FilterListModel> shippingRequestFilterOptions = [];
  List<FilterListModel> shippingRequestStatusesList = [];

  String requestStatusComments = "";

  void updateRequestStatusComments(var value) {
    requestStatusComments = value;
  }

  resetFilters() {
    if (shippingRequestFilterOptions.isEmpty) return;
    for (var value in shippingRequestFilterOptions) {
      value.isSelected = false;
    }
    shippingRequestFilterOptions[0].isSelected = true;
    requestStatusComments = "";
  }

  Future<void> populateShippingRequestFilterList() async {
    // if (shippingRequestFilterOptions.isNotEmpty && shippingRequestStatusesList.isNotEmpty) return;

    shippingStatusEnums = await commonRepo.getEnumTypeValues(enumTypeID: AppEnums.shippingStatusEnumId);
    shippingRequestFilterOptions.clear();
    shippingRequestStatusesList.clear();
    for (int i = 0; i < shippingStatusEnums.length; i++) {
      shippingRequestFilterOptions.add(FilterListModel(title: shippingStatusEnums[i].enumValueStr, isSelected: false, id: shippingStatusEnums[i].enumValue));
      shippingRequestStatusesList.add(FilterListModel(title: shippingStatusEnums[i].enumValueStr, isSelected: false, id: shippingStatusEnums[i].enumValue));
    }
    shippingRequestFilterOptions.insert(0, FilterListModel(title: Utils.getNameByShippingRequestStatusEnum(0.toShippingStatusEnum()), isSelected: false, id: 0));
    shippingRequestFilterOptions.insert(0, FilterListModel(title: Utils.getNameByShippingRequestStatusEnum(ShippingRequestStatusEnum.allRequests), isSelected: true, id: -1));
    notifyListeners();
  }

  Future<void> applyFiltersOnShippingRequests({required ShippingRequestStatusEnum shippingRequestStatusEnum}) async {
    for (var value in shippingRequestFilterOptions) {
      value.isSelected = false;
    }
    if (shippingRequestStatusEnum == ShippingRequestStatusEnum.allRequests) {
      shippingRequestFilterOptions[0].isSelected = true;
      await getShippingRequestsListByFilters();
      notifyListeners();
      return;
    }
    log("shippingRequestStatusEnum.getIdFromShippingStatusEnum(): ${shippingRequestStatusEnum.getIdFromShippingStatusEnum()}");
    log("shippingRequestFilterOptions: ${shippingRequestFilterOptions[shippingRequestStatusEnum.getIdFromShippingStatusEnum() + 1].title}");
    shippingRequestFilterOptions[shippingRequestStatusEnum.getIdFromShippingStatusEnum() + 1].isSelected = true; // +1 to match with the index 0 index has all requests
    await getShippingRequestsListByFilters(shippingStatusEnum: shippingRequestStatusEnum);
    notifyListeners();
  }

  void updateSelectionInShippingRequestStatuses(int index) {
    for (var value in shippingRequestStatusesList) {
      value.isSelected = false;
    }
    shippingRequestStatusesList[index].isSelected = true;
    notifyListeners();
  }

  Future<void> getShippingRequestsListByFilters({ShippingRequestStatusEnum? shippingStatusEnum}) async {
    setState(ViewState.busy);
    try {
      shippingRequestsList = await shippingRepo.getShippingRequestListByStatus(shippingStatusEnum: shippingStatusEnum);
      setState(ViewState.idle);
      notifyListeners();
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      setState(ViewState.idle);
    }
  }

  Future<bool> onUpdateShippingStatusTapped({required BuildContext context, required ShippingRequestStatusEnum shippingStatusEnum, required int shippingRequestId}) async {
    Utils.showLoading(context);
    try {
      GenericRespModel? genericRespModel = await shippingRepo.updateShippingRequestStatus(shippingStatusEnum: shippingStatusEnum, shippingRequestId: shippingRequestId, comment: requestStatusComments);
      Utils.showToast(genericRespModel.message.toString());
      Utils.hideLoading(context);
      requestStatusComments = "";
      return genericRespModel.messageStatus == 1;
    } catch (e) {
      logger.i(e.toString());
      Utils.showToast(e.toString());
      Utils.hideLoading(context);
      return false;
    }
  }
}
