import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_request_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/cmc_get_items_response_model.dart';
import 'package:diplomaticquarterapp/core/model/AlHabibMedicalService/ComprehensiveMedicalCheckup/get_cmc_service_response_model.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/update_pres_oreder_request_model.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/cmc_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';

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

class CMCViewModel extends BaseViewModel {
  CMCService _cMCService =
      locator<CMCService>();

  List<GetCMCServicesResponseModel> get cmcAllServicesList =>

      _cMCService.cmcAllServicesList;

  List<GetHHCAllPresOrdersResponseModel> get cmcAllPresOrders =>
      _cMCService.cmcAllPresOrdersList;

  List<GetOrderDetailByOrderIDResponseModel> get cmcAllOrderDetail =>
      _cMCService.cmcAllOrderDetail;
 List<CMCGetItemsResponseModel> get checkupItems =>
      _cMCService.checkupItemsList;

  bool get isOrderUpdated => _cMCService.isOrderUpdated;

  Future getCMCAllServices() async {
    await _cMCService
        .getCMCAllServices();
    CMCGetItemsRequestModel cMCGetItemsRequestModel = new CMCGetItemsRequestModel(checkupType: cmcAllServicesList[0].iD);

    await getCheckupItems(cMCGetItemsRequestModel);
    if (_cMCService.hasError) {
      error = _cMCService.error;
      setState(ViewState.Error);
    } else {
      setState(ViewState.Idle);
    }
  }
  Future getCheckupItems(CMCGetItemsRequestModel cMCGetItemsRequestModel) async {
    await _cMCService
        .getCheckupItems(cMCGetItemsRequestModel);
    if (_cMCService.hasError) {
      error = _cMCService.error;
      setState(ViewState.Error);
    }
  }

  Future getCmcAllPresOrders() async {
    setState(ViewState.Busy);
    await _cMCService.getCmcAllPresOrders();
    if (_cMCService.hasError) {
      error = _cMCService.error;
      setState(ViewState.Error);
    } else {
      var pendingOrder = _cMCService.cmcAllPresOrdersList.firstWhere(
          (element) => element.status == 1 || element.status == 2,
          orElse: () => null);
      if (pendingOrder != null) {
        await getOrderDetailByOrderID(pendingOrder);
        setState(ViewState.Idle);
      } else {
        getCMCAllServices();
      }
    }
  }

  Future updateCmcPresOrder(
      UpdatePresOrderRequestModel updatePresOrderRequestModel) async {
    setState(ViewState.BusyLocal);
    await _cMCService.updateCmcPresOrder(
        updatePresOrderRequestModel);
    if (_cMCService.hasError) {
      error = _cMCService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getOrderDetailByOrderID(GetHHCAllPresOrdersResponseModel order) async {
    GetOrderDetailByOrderIDRequestModel getOrderDetailByOrderIDRequestModel =
        GetOrderDetailByOrderIDRequestModel(presOrderID: order.iD);
    await _cMCService
        .getOrderDetailByOrderID(getOrderDetailByOrderIDRequestModel);
  }
}