import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/GetCMCAllOrdersResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/get_cmc_service_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/get_cmc_service_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_hHC_all_pres_orders_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';

import '../base_service.dart';

class CMCService extends BaseService {
  List<GetCMCServicesResponseModel> cmcAllServicesList = List();
  List<GetCMCAllOrdersResponseModel> cmcAllPresOrdersList = List();

  List<GetOrderDetailByOrderIDResponseModel> cmcAllOrderDetail = List();
  List<CMCGetItemsResponseModel> checkupItemsList = List();

  bool isOrderUpdated;

  Future getCMCAllServices() async {
    GetCMCServicesRequestModel getCMCServicesRequestModel =
        new GetCMCServicesRequestModel(genderID: authenticatedUserObject.user.gender, identificationNo: authenticatedUserObject.user.patientIdentificationNo);
    hasError = false;

    // RC IMPLEMENTATION
    await baseAppClient.post(GET_ALL_CMC_SERVICES_RC + "?patientID=" + user.patientID.toString(), isRCService: true, isAllowAny: true, onSuccess: (dynamic response, int statusCode) {
      cmcAllServicesList.clear();
      response['response'].forEach((data) {
        cmcAllServicesList.add(GetCMCServicesResponseModel.fromJson(data));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: {});

    // await baseAppClient.post(HHC_GET_ALL_CMC_SERVICES, onSuccess: (dynamic response, int statusCode) {
    //   cmcAllServicesList.clear();
    //   response['PatientER_CMC_GetAllServicesList'].forEach((data) {
    //     cmcAllServicesList.add(GetCMCServicesResponseModel.fromJson(data));
    //   });
    // }, onFailure: (String error, int statusCode) {
    //   hasError = true;
    //   super.error = error;
    // }, body: getCMCServicesRequestModel.toJson());
  }

  Future getCmcAllPresOrders() async {
    GetHHCAllPresOrdersRequestModel getHHCAllPresOrdersRequestModel = GetHHCAllPresOrdersRequestModel();
    hasError = false;
    await baseAppClient.post(GET_PATIENT_ALL_PRES_ORDERS, onSuccess: (dynamic response, int statusCode) {
      cmcAllPresOrdersList.clear();
      cmcAllOrderDetail.clear();
      response['response'].forEach((data) {
        if (data['ServiceID'] == OrderService.Comprehensive_Medical_Checkup.getIdOrderService()) cmcAllPresOrdersList.add(GetCMCAllOrdersResponseModel.fromJson(data));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: getHHCAllPresOrdersRequestModel.toJson());
  }

  Future getCmcAllPresOrdersRC() async {
    GetHHCAllPresOrdersRequestModel getHHCAllPresOrdersRequestModel = GetHHCAllPresOrdersRequestModel();
    hasError = false;
    await baseAppClient.post(GET_ALL_CMC_ORDERS_RC, isRCService: true, onSuccess: (dynamic response, int statusCode) {
      cmcAllPresOrdersList.clear();
      cmcAllOrderDetail.clear();

      response['response'].forEach((data) {
        cmcAllPresOrdersList.add(GetCMCAllOrdersResponseModel.fromJson(data));
      });

      // response['PatientER_GetPatientAllPresOrdersList'].forEach((data) {
      //   if (data['ServiceID'] == OrderService.Comprehensive_Medical_Checkup.getIdOrderService()) cmcAllPresOrdersList.add(GetHHCAllPresOrdersResponseModel.fromJson(data));
      // });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: getHHCAllPresOrdersRequestModel.toJson());
  }

  Future getOrderDetailByOrderID(GetOrderDetailByOrderIDRequestModel getOrderDetailByOrderIDRequestModel) async {
    hasError = false;
    await baseAppClient.post(GET_CMC_ORDER_DETAIL_BY_ID, onSuccess: (dynamic response, int statusCode) {
      cmcAllOrderDetail.clear();
      response['PatientER_CMC_GetTransactionsForOrderList'].forEach((data) {
        cmcAllOrderDetail.add(GetOrderDetailByOrderIDResponseModel.fromJson(data));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: getOrderDetailByOrderIDRequestModel.toJson());
  }

  Future getCheckupItems(CMCGetItemsRequestModel cMCGetItemsRequestModel) async {
    hasError = false;
    await baseAppClient.post(GET_CHECK_UP_ITEMS, onSuccess: (dynamic response, int statusCode) {
      checkupItemsList.clear();
      response['GetCheckUpItemsList'].forEach((data) {
        checkupItemsList.add(CMCGetItemsResponseModel.fromJson(data));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: cMCGetItemsRequestModel.toJson());
  }

  Future updateCmcPresOrder(UpdatePresOrderRequestModel updatePresOrderRequestModel) async {
    hasError = false;
    await baseAppClient.post(PATIENT_ER_UPDATE_PRES_ORDER, onSuccess: (dynamic response, int statusCode) {
      isOrderUpdated = true;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: updatePresOrderRequestModel.toJson());
  }

  Future updateCmcPresOrderRC(UpdatePresOrderRequestModel updatePresOrderRequestModel) async {
    hasError = false;
    await baseAppClient.post(UPDATE_CMC_ORDER_RC, isRCService: true, onSuccess: (dynamic response, int statusCode) {
      isOrderUpdated = true;
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: updatePresOrderRequestModel.toJson());
  }

  Future<String> insertCMCOrderRC({CMCInsertPresOrderRequestModel order}) async {
    hasError = false;
    String reqId = "";
    order.latitude = 0.0;
    order.longitude = 0.0;
    await baseAppClient.post(ADD_CMC_ORDER_RC, isRCService: true, onSuccess: (dynamic response, int statusCode) {
      isOrderUpdated = true;
      reqId = response['response'].toString();
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: order.toJson());
    return reqId;
  }

  Future<String> insertPresPresOrder({CMCInsertPresOrderRequestModel order}) async {
    hasError = false;
    String reqId = "";
    await baseAppClient.post(PATIENT_ER_INSERT_PRES_ORDER, onSuccess: (dynamic response, int statusCode) {
      isOrderUpdated = true;
      reqId = response['RequestNo'].toString();
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: order.toJson());
    return reqId;
  }
}
