import 'package:driverapp/config/config.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/core/model/orders/deliverd_order_req_model.dart';

class OrdersService extends BaseService {
  List<PendingOrdersRes> _orders = List();
  List<PendingOrdersRes> _nextOrdersList = List();
  List<DeliverdOrderModel> _deliverdOrderList = List();

  List<PendingOrdersRes> get orders => _orders;
  List<DeliverdOrderModel> get deliverdOrderList => _deliverdOrderList;

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

  PendingOrders _requestGetPendingOrders = PendingOrders(
    searchKey: "",
    pageSize: 0,
    pageIndex: 0,
    latitude: "24.640000",
    longitude: "46.753000",
  );
  DeliverdOrderModel _requestGetDeliverdOrders = DeliverdOrderModel(
    searchKey: "",
    pageSize: 0,
    pageIndex: 0,
    latitude: "24.640000",
    longitude: "46.753000",
  );

  Future getPendingOrders() async {
    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"];
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: _scanQrRequestModel.toJson());
    } 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"];
      }, 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 getDeliverdList() async {
    hasError = false;
    try {
      await baseAppClient.post(GET_ALL_DELIVERD_ORDER,
          onSuccess: (dynamic response, int statusCode) {
        _deliverdOrderList.clear();
        response['PatientER_Delivery_GetAllDeliverdOrderList'].forEach((order) {
          _deliverdOrderList.add(DeliverdOrderModel.fromJson(order));
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: _requestGetPendingOrders.toJson());
    } catch (e) {
      hasError = true;
      super.error = error;

      throw e;
    }
  }
}
