import 'package:diplomaticquarterapp/core/enum/OrderService.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/HHC_get_all_services_request_modle.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/HHC_get_all_services_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/PatientERHHCInsertServicesList.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_hhc_all_pres_orders_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/get_order_detail_by_order_iD_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/patient_er_insert_pres_order_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/HomeHealthCare/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/home_health_care_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';

import '../../../locator.dart';

class HomeHealthCareViewModel extends BaseViewModel {
  HomeHealthCareService _homeHealthCareService =
      locator<HomeHealthCareService>();

  List<HHCGetAllServicesResponseModel> get hhcAllServicesList =>
      _homeHealthCareService.hhcAllServicesList;

  List<GetHHCAllPresOrdersResponseModel> get hhcAllPresOrders =>
      _homeHealthCareService.hhcAllPresOrdersList;

  List<GetOrderDetailByOrderIDResponseModel> get hhcAllOrderDetail =>
      _homeHealthCareService.hhcAllOrderDetail;

  bool get isOrderUpdated => _homeHealthCareService.isOrderUpdated;

  GetHHCAllPresOrdersResponseModel pendingOrder;

  List<PatientERHHCInsertServicesList> patientERHHCInsertServicesList = List();

  Future getHHCAllServices() async {
    HHCGetAllServicesRequestModel hHCGetAllServicesRequestModel =
        new HHCGetAllServicesRequestModel();
    //setState(ViewState.Busy);
    await _homeHealthCareService
        .getHHCAllServices(hHCGetAllServicesRequestModel);
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getHHCAllPresOrders() async {
    pendingOrder = null;
    setState(ViewState.Busy);
    await _homeHealthCareService.getHHCAllPresOrders();
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.Error);
    } else {
       pendingOrder = _homeHealthCareService.hhcAllPresOrdersList
          .firstWhere((element) => element.status == 1 || element.status == 2,
          orElse: () => null);
      if (pendingOrder != null) {
        await _homeHealthCareService.getOrderDetailByOrderID(pendingOrder.iD);
        setState(ViewState.Idle);
      } else {
        getHHCAllServices();
      }

    }
  }

  Future updateHHCPresOrder(UpdatePresOrderRequestModel updatePresOrderRequestModel) async {
    setState(ViewState.Busy);
    await _homeHealthCareService.updateHHCPresOrder(
        updatePresOrderRequestModel);
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future insertPresPresOrder({PatientERInsertPresOrderRequestModel order}) async {
    setState(ViewState.Busy);
    order.createdBy = user.patientID;
    order.orderServiceID = OrderService.HOME_HEALTH_CARE.getIdOrderService();
    await _homeHealthCareService.insertPresPresOrder(order: order);
    if (_homeHealthCareService.hasError) {
      error = _homeHealthCareService.error;
      setState(ViewState.ErrorLocal);
    } else {
      getHHCAllPresOrders();
    }
  }



}