import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/core/service/PrescriptionDeliveryService.dart';
import 'package:diplomaticquarterapp/core/service/parmacyModule/parmacy_module_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/base_view_model.dart';

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

class PrescriptionDeliveryViewModel extends BaseViewModel {
  CustomerAddressesService _customerAddressesService = locator<CustomerAddressesService>();
  PharmacyModuleService _pharmacyModuleService = locator<PharmacyModuleService>();
  PrescriptionDeliveryService _prescriptionDeliveryService = locator<PrescriptionDeliveryService>();

  List<AddressInfo> get addressesList => _customerAddressesService.addressesList;

  Future getCustomerInfo() async {
    setState(ViewState.Busy);
    await _pharmacyModuleService.generatePharmacyToken().then((value) async {
      // await _customerAddressesService.getCustomerInfo();
      // await _customerAddressesService.getCustomerInfo();
      await _pharmacyModuleService.createUser();
    });
    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error;
      setState(ViewState.ErrorLocal);
    } else {}
    await getCustomerAddresses();
  }

  Future insertDeliveryOrder({int lineItemNo, double latitude, double longitude, int appointmentNo, int createdBy, int dischargeID, int projectID}) async {
    setState(ViewState.BusyLocal);
    // await _prescriptionDeliveryService.insertDeliveryOrder(
    //     lineItemNo: lineItemNo, latitude: latitude, longitude: longitude, appointmentNo: appointmentNo, createdBy: createdBy, dischargeID: dischargeID);
    await _prescriptionDeliveryService.insertDeliveryOrderRC(
        latitude: latitude, longitude: longitude, appointmentNo: appointmentNo, createdBy: createdBy, dischargeID: dischargeID, projectID: projectID);
    if (_prescriptionDeliveryService.hasError) {
      error = _prescriptionDeliveryService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }

  Future getCustomerAddresses() async {
    setState(ViewState.Busy);
    await _customerAddressesService.getCustomerAddresses();
    if (_customerAddressesService.hasError) {
      error = _customerAddressesService.error;
      setState(ViewState.ErrorLocal);
    } else {
      setState(ViewState.Idle);
    }
  }
}
