import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/model/er/AmbulanceRequestOrdersModel.dart';
import 'package:diplomaticquarterapp/core/model/er/PatientAllPresOrders.dart';
import 'package:diplomaticquarterapp/core/model/er/PatientER_RC.dart';
import 'package:diplomaticquarterapp/core/model/er/PickUpRequestPresOrder.dart';
import 'package:diplomaticquarterapp/core/model/er/get_all_transportation_method_list_model.dart';
import 'package:flutter/cupertino.dart';

import '../base_service.dart';

class AmService extends BaseService {
  List<PatientERTransportationMethod> amModelList = List();
  List<PatientAllPresOrders> patientAllPresOrdersList = List();

  List<AmbulanceRequestOrdersModel> patientAmbulanceRequestOrdersList = List();

  bool hasPendingOrder = false;
  int pendingOrderID = 0;
  String pendingOrderStatus = "";
  String pendingOrderStatusAR = "";
  PickUpRequestPresOrder pickUpRequestPresOrder;

  AmbulanceRequestOrdersModel pendingAmbulanceRequestOrder;

  Future getAllTransportationOrders() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['isDentalAllowedBackend'] = false;
    body['IdentificationNo'] = user.patientIdentificationNo;
    await baseAppClient.get(GET_ALL_TRANSPORTATIONS_RC + "?patientID=" + user.patientID.toString(), isExternal: false, onSuccess: (dynamic response, int statusCode) {
      amModelList.clear();
      response['data']['transportationservices'].forEach((item) {
        amModelList.add(PatientERTransportationMethod.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    });
  }

  Future getPatientAllPresOrdersList() async {
    hasError = false;
    hasPendingOrder = false;
    pickUpRequestPresOrder = null;
    await baseAppClient.post(GET_PATIENT_ALL_PRES_ORDERS, onSuccess: (dynamic response, int statusCode) {
      patientAllPresOrdersList.clear();
      response['PatientER_GetPatientAllPresOrdersList'].forEach((item) {
        if (item['ServiceID'] == OrderService.AMBULANCE.getIdOrderService()) {
          var order = PatientAllPresOrders.fromJson(item);
          patientAllPresOrdersList.add(order);
          if (order.status == 1) {
            hasPendingOrder = true;
            pendingOrderID = order.iD;
            pendingOrderStatus = order.description;
            pendingOrderStatusAR = order.descriptionN;
          }
        }
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  Future getPatientAllPresOrdersListRC(dynamic patientID) async {
    hasError = false;
    hasPendingOrder = false;
    pickUpRequestPresOrder = null;

    Map<String, dynamic> body = Map();
    body['patientId'] = patientID.toString();
    body['PatientID'] = patientID.toString();

    await baseAppClient.post(GET_ALL_TRANSPORTATIONS_ORDERS, onSuccess: (dynamic response, int statusCode) {
      patientAmbulanceRequestOrdersList.clear();
      hasPendingOrder = false;
      pendingOrderID = 0;
      pendingAmbulanceRequestOrder = null;

      response['data'].forEach((item) {
        patientAmbulanceRequestOrdersList.add(AmbulanceRequestOrdersModel.fromJson(item));
        if (item['statusId'] == 1) {
          hasPendingOrder = true;
          pendingOrderID = item['orderpayment']['id'];
          pendingOrderStatus = item['statusText'];
          pendingOrderStatusAR = item['statusText'];
          pendingAmbulanceRequestOrder = AmbulanceRequestOrdersModel.fromJson(item);
        }
      });
      print(patientAmbulanceRequestOrdersList.length);
      print(hasPendingOrder);
      print(pendingOrderID);
      print(pendingOrderStatus);
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getOrderDetails() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PresOrderID'] = pendingOrderID;
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(GET_PICK_UP_REQUEST_BY_PRES_ORDER_ID, onSuccess: (dynamic response, int statusCode) {
      response['PatientER_RRT_GetPickUpRequestByPresOrderIDList'].forEach((item) {
        pickUpRequestPresOrder = PickUpRequestPresOrder.fromJson(item);
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future updatePressOrder({@required int presOrderID}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PresOrderID'] = presOrderID;
    body['EditedBy'] = 3;
    body['RejectionReason'] = '';
    body['PresOrderStatus'] = OrderService.AMBULANCE.getIdOrderService();
    body['isDentalAllowedBackend'] = false;
    await baseAppClient.post(UPDATE_PRESS_ORDER, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future updatePressOrderRC({@required int presOrderID, @required String patientID}) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['Id'] = presOrderID;
    body['StatusId'] = 6;
    body['ClickButton'] = 14;
    body['PatientID'] = patientID;
    await baseAppClient.post(CANCEL_AMBULANCE_REQUEST, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future insertERPressOrder({@required PatientER_RC patientER}) async {
    hasError = false;

    var body = patientER.toJson();
    print(body);

    await baseAppClient.post(INSERT_TRANSPORTATION_ORDER_RC, onSuccess: (dynamic response, int statusCode) {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
