import 'package:mc_common_app/api/api_client.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/dependency_injection.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/provider_branches_models/branch_detail_model.dart';
import 'package:mc_common_app/models/subscriptions_models/branch_user_selection_model.dart';
import 'package:mc_common_app/models/subscriptions_models/subscription_model.dart';
import 'package:mc_common_app/models/user_models/branch_user.dart';

abstract class SubscriptionRepo {
  // Future<SubscriptionModel> getAllSubscriptions(String? serviceProviderID);

  // Future<List<ProviderSubscriptionModel>> getProviderSubscription({String? serviceProviderID});

  Future<SubscriptionModel> getMySubscriptions(String? serviceProviderID);

  Future<SubscriptionModel> getSubscriptionBySP(String? serviceProviderID, bool isRenew);

  Future<GenericRespModel> calculationUpgradePrice(String? serviceProviderID, String? newSubscription);

  Future<GenericRespModel> payForProviderSubscription(Map<String, dynamic> map);

  Future<List<BranchSelectionModel>> getSPBranchUserGet(Map<String, String> map);
}

class SubscriptionRepoImp extends SubscriptionRepo {
  ApiClient apiClient = injector.get<ApiClient>();
  AppState appState = injector.get<AppState>();

  @override
  Future<SubscriptionModel> getSubscriptionBySP(String? serviceProviderID, bool isRenew) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    Map<String, dynamic> queryParameters = {};
    if (serviceProviderID != null) {
      queryParameters = {
        "ProviderID": serviceProviderID,
        "IsRenew": isRenew.toString(),
      };
    }

    return await injector.get<ApiClient>().getJsonForObject(
          (json) => SubscriptionModel.fromJson(json),
          ApiConsts.getSubscriptionBySP,
          token: t,
          queryParameters: queryParameters,
        );
  }

  @override
  Future<GenericRespModel> calculationUpgradePrice(String? serviceProviderID, String? newSubscription) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    Map<String, String> queryParameters = {};
    if (serviceProviderID != null) {
      queryParameters = {
        "ServiceProviderID": serviceProviderID,
        "NewSubscription": newSubscription!,
      };
    }

    return await injector.get<ApiClient>().getJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.calculationUpgradePrice,
          token: t,
          queryParameters: queryParameters,
        );
  }

  @override
  Future<GenericRespModel> payForProviderSubscription(Map<String, dynamic> map) async {
    String t = AppState().getUser.data!.accessToken ?? "";

    return await injector.get<ApiClient>().postJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.payFortOrderProviderSubscriptionCreate,
          map,
          token: t,
        );
  }

  @override
  Future<List<BranchSelectionModel>> getSPBranchUserGet(Map<String, String> map) async {
    String t = AppState().getUser.data!.accessToken ?? "";

    GenericRespModel genericRespModel = await injector.get<ApiClient>().getJsonForObject(
          (json) => GenericRespModel.fromJson(json),
          ApiConsts.getSPBranchUserGet,
          token: t,
          queryParameters: map,
        );

    List<BranchSelectionModel> branchList = [];

    if (genericRespModel.data != null) {
      List<BranchDetailModel> branches = [];
      List<BranchUser> branchUsers = [];

      // List<BranchUsersDatum>.from(json["data"]!.map((x) => BranchUsersDatum.fromJson(x))
      branches = List<BranchDetailModel>.from(genericRespModel.data["branches"]["data"]!.map((x) => BranchDetailModel.fromJson(x)));
      branchUsers = List<BranchUser>.from(genericRespModel.data["branchUsers"]["data"]!.map((x) => BranchUser.fromJson(x)));
      for (int i = 0; i < branches.length; i++) {
        List<UserSelectionModel> availableUsers = [];
        for (int j = 0; j < branchUsers.length; j++) {
          if (branches[i].id == branchUsers[j].serviceProviderBranchID) {
            availableUsers.add(UserSelectionModel(userId: branchUsers[j].id, userName: ("${branchUsers[j].firstName ?? ""} ${branchUsers[j].lastName}"), isSelected: false));
          }
        }
        branchList.add(BranchSelectionModel(branchId: branches[i].id ?? 0, branchName: branches[i].branchName ?? "", isSelected: false, usersList: availableUsers));
      }
    }
    return branchList;
  }

  @override
  Future<SubscriptionModel> getMySubscriptions(String? serviceProviderID) async {
    String t = AppState().getUser.data!.accessToken ?? "";
    Map<String, String> queryParameters = {};
    if (serviceProviderID != null) {
      queryParameters = {
        "ID": serviceProviderID,
      };
    }

    return await injector.get<ApiClient>().getJsonForObject((json) => SubscriptionModel.fromJson(json), ApiConsts.getAllSubscriptions, token: t, queryParameters: queryParameters);
  }

// @override
// Future<SubscriptionModel> getAllSubscriptions(String? serviceProviderID) async {
//   String t = AppState().getUser.data!.accessToken ?? "";
//   Map<String, String> queryParameters = {};
//   if (serviceProviderID != null) {
//     queryParameters = {
//       "ID": serviceProviderID,
//     };
//   }
//
//   return await injector.get<ApiClient>().getJsonForObject((json) => SubscriptionModel.fromJson(json), ApiConsts.getAllSubscriptions, token: t, queryParameters: queryParameters);
// }

// @override
// Future<List<ProviderSubscriptionModel>> getProviderSubscription({String? serviceProviderID}) async {
//   Map<String, String> queryParameters = {};
//   if (serviceProviderID != null) {
//     queryParameters = {
//       "ServiceProviderID": serviceProviderID,
//       "IsActiveSubscriptionForProvider": "true",
//     };
//   }
//
//   GenericRespModel adsGenericModel = await apiClient.getJsonForObject(
//     token: appState.getUser.data!.accessToken,
//     (json) => GenericRespModel.fromJson(json),
//     ApiConsts.getMySubscriptions,
//     queryParameters: queryParameters,
//   );
//   List<ProviderSubscriptionModel> providerSubList = List.generate(adsGenericModel.data.length, (index) => ProviderSubscriptionModel.fromJson(adsGenericModel.data[index]));
//   return providerSubList;
// }

// @override
// Future<SubscriptionModel> getProviderSubscription(
//     String? serviceProviderID) async {
//   String t = AppState().getUser.data!.accessToken ?? "";
//   Map<String, String> queryParameters = {};
//   if (serviceProviderID != null) {
//     queryParameters = {
//       "ID": serviceProviderID,
//     };
//   }
//
//
//   return await injector.get<ApiClient>().getJsonForObject(
//       (json) => SubscriptionModel.fromJson(json),
//       ApiConsts.getMySubscriptions,
//       token: t,
//       queryParameters: queryParameters);
// }
}
