import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/vaccine/my_vaccine.dart';
import 'package:hmg_patient_app/core/model/vaccine/vaccination_item.dart';
import 'package:hmg_patient_app/core/service/vaccine_service.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';

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

class VaccineViewModel extends BaseViewModel {
  bool hasError = false;

  VaccineService _vaccineService = locator<VaccineService>();

  List<VaccineModel> get vaccineList => _vaccineService.vaccineList;
  List<VaccinationItem> get vaccinationItemList => _vaccineService.vaccinationItemList;


  Future getVaccine() async {
    hasError = false;
    setState(ViewState.Busy);
    await _vaccineService.getMyVaccine();
    if (_vaccineService.hasError) {
      error = _vaccineService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMyVaccinationItem() async {
    hasError = false;
    setState(ViewState.Busy);
    await _vaccineService.getMyVaccinationItem();
    if (_vaccineService.hasError) {
      error = _vaccineService.error!;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }

  Future getMyVaccinationOnHand({String? pItemCode}) async {
    hasError = false;
    setState(ViewState.Busy);
    await _vaccineService.getMyVaccinationOnHand(pItemCode: pItemCode!);
    if (_vaccineService.hasError) {
      error = _vaccineService.error!;
      setState(ViewState.ErrorLocal);
    } else
      setState(ViewState.Idle);
  }

  Future sendEmail({String? message}) async {
    hasError = false;
    setState(ViewState.BusyLocal);
    await _vaccineService.sendEmail();
    if (_vaccineService.hasError) {
      error = _vaccineService.error!;
      setState(ViewState.ErrorLocal);
      AppToast.showErrorToast(message: error);
    } else {
      AppToast.showSuccessToast(message: message!);
      setState(ViewState.Idle);
    }
  }
}
