import 'package:driverapp/config/config.dart';
import 'package:driverapp/core/model/orders/deliverd_order_res_model.dart';
import 'package:driverapp/core/model/orders/next_order_request_model.dart';
import 'package:driverapp/core/model/orders/pending_orders_req_model.dart';
import 'package:driverapp/core/model/orders/pending_orders_res_model.dart';
import 'package:driverapp/core/model/orders/update_order_status_request_model.dart';
import 'package:driverapp/core/model/scan_qr/scan_qr_request_model.dart';
import 'package:driverapp/core/service/base_service.dart';
import 'package:driverapp/uitl/utils.dart';
import 'package:location/location.dart';

class OrdersService extends BaseService {
  List<PendingOrdersRes> _orders = List();
  List<PendingOrdersRes> _nextOrdersList = List();
  List<DeliverdOrderResModel> _deliveredOrderList = List();

  List<PendingOrdersRes> get orders => _orders;

  List<DeliverdOrderResModel> get deliveredOrderList => _deliveredOrderList;

  List<PendingOrdersRes> get nextOrdersList => _nextOrdersList;
  bool isOrderInserted;
  bool isOrderStatusUpdated;
  int listCountDelivered;
  int listCountUnDelivered;

  Future getPendingOrders(int driverAppPageID) async {
    LocationData loc = await Utils.getLocation();
    if (loc != null) {
      PendingOrders _requestGetPendingOrders = PendingOrders(
          searchKey: "",
          pageSize: 0,
          pageIndex: 0,
          latitude: loc.latitude.toString(),
          longitude: loc.longitude.toString(),
          driverAppPageID: driverAppPageID);
      hasError = false;
      try {
        await baseAppClient.post(GET_ALL_ORDERS,
            onSuccess: (dynamic response, int statusCode) {
          _orders.clear();
          response['PatientER_Delivery_GetAllOrderList'].forEach((order) {
            _orders.add(PendingOrdersRes.fromJson(order));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: _requestGetPendingOrders.toJson());
      } catch (e) {
        hasError = true;
        super.error = error;

        throw e;
      }
    }
    // }
  }

  Future insertOrder(ScanQrRequestModel _scanQrRequestModel) async {
    hasError = false;
    try {
      await baseAppClient.post(SCAN_QR,
          onSuccess: (dynamic response, int statusCode) {
        isOrderInserted = response["PatientER_Delivery_IsOrderInserted"];
        listCountUnDelivered++;
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: _scanQrRequestModel.toJson());
    } catch (e) {
      hasError = true;
      super.error = error;
      throw e;
    }
  }

  Future getOrdersStatus() async {
    hasError = false;
    try {
      await baseAppClient.post(GET_ALL_ORDERS_STATUS,
          onSuccess: (dynamic response, int statusCode) {
        listCountDelivered =
            response["PatientER_CountOrderList"][0]["NumberOfDeliveredOrder"];
        listCountUnDelivered =
            response["PatientER_CountOrderList"][0]["NumberOfUnDeliveredOrder"];
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: {});
    } catch (e) {
      hasError = true;
      super.error = error;
      throw e;
    }
  }

  Future updateOrderStatus(
      UpdateOrderStatusRequestModel updateOrderStatusRequestModel) async {
    hasError = false;
    try {
      await baseAppClient.post(UPDATE_ORDER_STATUS,
          onSuccess: (dynamic response, int statusCode) {
        isOrderStatusUpdated = response["PatientER_Delivery_IsOrderUpdated"];
        listCountUnDelivered--;
        listCountDelivered++;
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: updateOrderStatusRequestModel.toJson());
    } catch (e) {
      hasError = true;
      super.error = error;
      throw e;
    }
  }

  Future nextOrder(NextOrderRequestModel nextOrderRequestModel) async {
    hasError = false;
    try {
      await baseAppClient.post(NEXT_ORDER,
          onSuccess: (dynamic response, int statusCode) {
        response["PatientER_Delivery_NextOrder"].forEach((order) {
          _nextOrdersList.add(PendingOrdersRes.fromJson(order));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: nextOrderRequestModel.toJson());
    } catch (e) {
      hasError = true;
      super.error = error;
      throw e;
    }
  }

  Future getDeliveredList() async {
    LocationData loc = await Utils.getLocation();
    PendingOrders _requestGetPendingOrders = PendingOrders(
        searchKey: "",
        pageSize: 0,
        pageIndex: 0,
        latitude: loc.latitude.toString(),
        longitude: loc.longitude.toString(),
        driverAppPageID: 0);
    if (loc != null) {
      hasError = false;
      try {
        await baseAppClient.post(GET_ALL_DELIVERD_ORDER,
            onSuccess: (dynamic response, int statusCode) {
          _deliveredOrderList.clear();
          response['PatientER_Delivery_GetAllDeliverdOrderList']
              .forEach((order) {
            _deliveredOrderList.add(DeliverdOrderResModel.fromJson(order));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: _requestGetPendingOrders.toJson());
      } catch (e) {
        hasError = true;
        super.error = error;

        throw e;
      }
    }
  }
}
