import 'package:hmg_patient_app/config/config.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/model/vaccine/vaccination_on_hand.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';

class VaccineService extends BaseService {
  List<VaccineModel> _vaccineList =[];
  List<VaccinationItem> vaccinationItemList =[];
  List<VaccinationOnHand> vaccinationOnHandList =[];

  List<VaccineModel> get vaccineList => _vaccineList;

  Future getMyVaccine() async {
    Map<String, dynamic> body = Map();
    body['To'] = "0";
    body['From'] = "0";

    hasError = false;
    _vaccineList.clear();
    await baseAppClient.post(GET_VACCINES,
        onSuccess: (dynamic response, int statusCode) {
      response['List_DoneVaccines'].forEach((item) {
        _vaccineList.add(VaccineModel.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future sendEmail() async {
    Map<String, dynamic> body = Map();
    body['ListVaccines'] = vaccineList.map((v) => v.toJson()).toList();
    body['To'] = user.emailAddress;
    body['DateofBirth'] = user.dateofBirth;
    body['PatientIditificationNum'] = user.patientIdentificationNo;
    body['PatientMobileNumber'] = user.mobileNumber;
    body['PatientName'] = user.firstName! + " " + user.lastName!;

    hasError = false;

    await baseAppClient.post(GET_VACCINES_EMAIL,
        onSuccess: (dynamic response, int statusCode) {},
        onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getMyVaccinationItem() async {
    await baseAppClient.post(GET_VACCINATIONS_ITEMS,
        onSuccess: (dynamic response, int statusCode) {
      response['GetVaccinationsList'].forEach((item) {
        vaccinationItemList.add(VaccinationItem.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: Map());
  }

  Future getMyVaccinationOnHand({required String pItemCode}) async {
    Map<String, dynamic> body = Map();
    body['P_ITEM_CODE'] = pItemCode;
    await baseAppClient.post(GET_VACCINATION_ONHAND,
        onSuccess: (dynamic response, int statusCode) {
      response['GetVaccinationOnHandList'].forEach((item) {
        vaccinationOnHandList.add(VaccinationOnHand.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }
}
