import 'package:driverapp/core/enum/viewstate.dart';
import 'package:driverapp/core/model/orders/deliverd_order_req_model.dart';
import 'package:driverapp/core/model/orders/next_order_request_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/orders_service.dart';
import 'package:driverapp/core/model/orders/deliverd_order_res_model.dart';
import '../../locator.dart';
import 'base_view_model.dart';

class OrdersViewModel extends BaseViewModel {
  OrdersService _ordersService = locator<OrdersService>();

  List<PendingOrdersRes> get orders => _ordersService.orders;

  List<DeliverdOrderResModel> get deliverdOrders =>
      _ordersService.deliverdOrderList;

  List<PendingOrdersRes> get nextOrdersList => _ordersService.nextOrdersList;
  bool isBottomSheetAppear = false;

  Future getPendingOrders() async {
    setState(ViewState.Busy);
    await _ordersService.getPendingOrders();
    if (_ordersService.hasError) {
      error = _ordersService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getDeliverdOrders() async {
    setState(ViewState.Busy);
    await _ordersService.getDeliverdList();
    if (_ordersService.hasError) {
      error = _ordersService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future insertOrder(ScanQrRequestModel _scanQrRequestModel) async {
    setState(ViewState.ErrorLocal);
    await _ordersService.insertOrder(_scanQrRequestModel);
    if (_ordersService.hasError) {
      error = _ordersService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future updateOrderStatus(
      UpdateOrderStatusRequestModel updateOrderStatusRequestModel) async {
    setState(ViewState.BusyLocal);
    await _ordersService.updateOrderStatus(updateOrderStatusRequestModel);
    if (_ordersService.hasError) {
      error = _ordersService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future nextOrder(NextOrderRequestModel nextOrderRequestModel) async {
    setState(ViewState.BusyLocal);
    await _ordersService.nextOrder(nextOrderRequestModel);
    if (_ordersService.hasError) {
      error = _ordersService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  showBottomSheet() {
    isBottomSheetAppear = true;
    setState(ViewState.Idle);
  }

  hideBottomSheet() {
    isBottomSheetAppear = false;
    setState(ViewState.Idle);
  }
}
