import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/er/AmbulanceRequestOrdersModel.dart';
import 'package:diplomaticquarterapp/core/model/er/PatientAllPresOrders.dart';
import 'package:diplomaticquarterapp/core/model/er/PatientER.dart';
import 'package:diplomaticquarterapp/core/model/er/PatientER_RC.dart';
import 'package:diplomaticquarterapp/core/model/er/PickUpRequestPresOrder.dart';
import 'package:diplomaticquarterapp/core/model/er/get_all_transportation_method_list_model.dart';
import 'package:diplomaticquarterapp/core/model/hospitals/hospitals_model.dart';
import 'package:diplomaticquarterapp/core/service/er/am_service.dart';
import 'package:diplomaticquarterapp/core/service/hospital_service.dart';
import 'package:diplomaticquarterapp/core/service/medical/medical_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';
import 'package:diplomaticquarterapp/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:flutter/cupertino.dart';
import '../base_view_model.dart';
import '../../../locator.dart';

class AmRequestViewModel extends BaseViewModel {
  AmService _amService = locator<AmService>();
  HospitalService _hospitalService = locator<HospitalService>();
  MedicalService _medicalService = locator<MedicalService>();

  List<PatientERTransportationMethod> get amRequestModeList =>
      _amService.amModelList;

  List<PatientAllPresOrders> get patientAllPresOrdersList =>
      _amService.patientAllPresOrdersList;

  List<AppoitmentAllHistoryResultList> get appoitmentAllHistoryResultList =>
      _medicalService.appoitmentAllHistoryResultList;

  List<AmbulanceRequestOrdersModel> get patientAmbulanceRequestOrdersList =>
      _amService.patientAmbulanceRequestOrdersList;

  List<HospitalsModel> get hospitals => _hospitalService.hospitals;

  bool get hasPendingOrder =>_amService.hasPendingOrder;

  PickUpRequestPresOrder get pickUpRequestPresOrder =>_amService.pickUpRequestPresOrder;

  AmbulanceRequestOrdersModel get pendingAmbulanceRequestOrder =>_amService.pendingAmbulanceRequestOrder;

  Future getAppointmentHistory() async {
    setState(ViewState.BusyLocal);
    await _medicalService.getAppointmentHistory(isActiveAppointment: true);
    if (_medicalService.hasError) {
      error = _medicalService.error;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future getAmRequestOrders() async {
    setState(ViewState.Busy);
    await _amService.getAllTransportationOrders();
    if (_amService.hasError) {
      error = _amService.error;
      setState(ViewState.Error);
    } else
      getHospitals();
  }

  Future getHospitals() async {
    setState(ViewState.Busy);
    await _hospitalService.getHospitals(isResBasedOnLoc: false);
    if (_hospitalService.hasError) {
      error = _hospitalService.error;
      setState(ViewState.Error);
    } else
      // getPatientAllPresOrdersList();
      getPatientAllPresOrdersListRC();
  }

  Future getPatientAllPresOrdersListRC() async {
    setState(ViewState.Busy);
    await _amService.getPatientAllPresOrdersListRC(authenticatedUserObject.user.patientID);
    if (_hospitalService.hasError) {
      error = _hospitalService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getPatientAllPresOrdersList() async {
    setState(ViewState.Busy);
    await _amService.getPatientAllPresOrdersList();
    if (_hospitalService.hasError) {
      error = _hospitalService.error;
      setState(ViewState.Error);
    } else if (_amService.hasPendingOrder) {
      getOrderDetails();
    } else
      setState(ViewState.Idle);
  }

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

  Future updatePressOrder({@required int presOrderID}) async {
    setState(ViewState.Busy);
    // await _amService.updatePressOrder(presOrderID: presOrderID);
    await _amService.updatePressOrderRC(presOrderID: presOrderID, patientID: authenticatedUserObject.user.patientID.toString());
    if (_amService.hasError) {
      error = _amService.error;
      setState(ViewState.Error);
    } else
      getPatientAllPresOrdersListRC();
  }

  Future insertERPressOrder({@required PatientER_RC patientER})  async {
    setState(ViewState.Busy);
    await _amService.insertERPressOrder(patientER: patientER);
    if (_amService.hasError) {
      error = _amService.error;
      setState(ViewState.ErrorLocal);
    } else
      // getAmRequestOrders();
      getPatientAllPresOrdersListRC();
  }
}
