import 'package:driverapp/core/enum/viewstate.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 '../../locator.dart';
import 'base_view_model.dart';

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

  List<PendingOrdersRes> get orders => _OrdersService.orders;
  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 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);
  }

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

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