import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/pharmacies/pharmacies_list_model.dart';
import 'package:hmg_patient_app/core/model/pharmacies/pharmacies_model.dart';
import 'package:hmg_patient_app/core/service/pharmacies_service.dart';

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

class PharmacyViewModel extends BaseViewModel {
  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';
  PharmacyService _pharmacyService = locator<PharmacyService>();

  List<PharmaciesModel> get pharmacy => _pharmacyService.medicineItem;
  List<PharmaciesListModel> get pharmacyList => _pharmacyService.pharmaciesList;

  Future getPharmacies({int? id}) async {
    setState(ViewState.Busy);

    await _pharmacyService.getPharmaciesList(itemID: id!);
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  clearMedicineSearch(){
    _pharmacyService.clearMedicineList();
  }
  Future getMedicine({String? name}) async {
    hasError = false;
    _pharmacyService.clearMedicineList();
    setState(ViewState.BusyLocal);
    await _pharmacyService.getMedicineList(drugName: name!);
    if (_pharmacyService.hasError) {
      error = _pharmacyService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }
}
