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/extensions/string_extensions.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/requests_models/offers_model.dart';
import 'package:mc_common_app/models/requests_models/provider_offers_model.dart';
import 'package:mc_common_app/models/requests_models/request_model.dart';
import 'package:mc_common_app/utils/enums.dart';

abstract class RequestRepo {
  Future<GenericRespModel> createRequest(Map<String, dynamic> map);

  Future<List<OffersModel>> getOffersByRequest({required int requestId, int serviceProviderId = 0});

  Future<ProviderOffersModel> getOffersFromProvidersByRequest({required int requestId});

  Future<List<RequestModel>> getRequests(Map<String, dynamic> postParams);

  Future<GenericRespModel> updateOfferRequestStatus({required RequestOfferStatusEnum requestOfferStatusEnum, required int requestOfferId, required String comments});
}

class RequestRepoImp implements RequestRepo {
  ApiClient apiClient = injector.get<ApiClient>();
  AppState appState = injector.get<AppState>();

  @override
  Future<GenericRespModel> createRequest(Map<String, dynamic> postParams) async {
    GenericRespModel enumGenericModel = await apiClient.postJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.createRequest,
      postParams,
      token: appState.getUser.data!.accessToken,
    );
    return enumGenericModel;
  }

  @override
  Future<List<RequestModel>> getRequests(Map<String, dynamic> postParams) async {
    GenericRespModel enumGenericModel = await apiClient.postJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.getRequest,
      postParams,
      token: appState.getUser.data!.accessToken,
    );
    List<RequestModel> requests = List.generate(
      enumGenericModel.data.length,
      (index) => RequestModel.fromJson(
        enumGenericModel.data[index],
      ),
    );
    return requests;
  }

  @override
  Future<List<OffersModel>> getOffersByRequest({required int requestId, int serviceProviderId = 0}) async {
    var queryParameters = {
      "RequestID": requestId.toString(),
      "ServiceProviderID": serviceProviderId.toString(),
    };
    GenericRespModel genericRespModel = await apiClient.getJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.getRequestOffers,
      queryParameters: queryParameters,
      token: appState.getUser.data!.accessToken,
    );
    List<OffersModel> offersList = List.generate(
      genericRespModel.data.length,
      (index) => OffersModel.fromJson(genericRespModel.data[index]),
    );
    return offersList;
  }

  @override
  Future<ProviderOffersModel> getOffersFromProvidersByRequest({required int requestId}) async {
    final customerId = appState.getUser.data!.userInfo!.customerId;
    var queryParameters = {
      "customerID": customerId.toString(),
      "requestID": requestId.toString(),
    };
    GenericRespModel genericRespModel = await apiClient.postJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.requestOffersSpsGet,
      queryParameters,
      token: appState.getUser.data!.accessToken,
    );
    ProviderOffersModel providerOffersModel = ProviderOffersModel();
    if (genericRespModel.data != null && genericRespModel.data.length > 0) {
      providerOffersModel = ProviderOffersModel.fromJson(genericRespModel.data.first);
    }
    return providerOffersModel;
  }

  @override
  Future<GenericRespModel> updateOfferRequestStatus({required RequestOfferStatusEnum requestOfferStatusEnum, required int requestOfferId, required String comments}) async {
    var queryParameters = {
      "requestOfferId": requestOfferId.toString(),
      "offerStatus": requestOfferStatusEnum.getIdFromRequestOfferStatusEnum().toString(),
      "comments": comments,
    };
    GenericRespModel genericRespModel = await apiClient.postJsonForObject(
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.updateRequestOffer,
      queryParameters,
      token: appState.getUser.data!.accessToken,
    );

    return genericRespModel;
  }
}
