import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/models/pharmacies/pharmacies_List_request_model.dart';
import 'package:doctor_app_flutter/models/pharmacies/pharmacies_items_request_model.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:flutter/cupertino.dart';

class MedicineProvider with ChangeNotifier {
  DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

  var pharmacyItemsList = [];
  var pharmaciesList = [];
  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';

  PharmaciesItemsRequestModel _itemsRequestModel =
  PharmaciesItemsRequestModel();
  PharmaciesListRequestModel _listRequestModel = PharmaciesListRequestModel();


  clearPharmacyItemsList(){
    pharmacyItemsList.clear();
    notifyListeners();
  }

 getMedicineItem(String itemName) async {
    _itemsRequestModel.pHRItemName = itemName;
    resetDefaultValues();
    pharmacyItemsList.clear();
    notifyListeners();
    try {
      await BaseAppClient.post(PHARMACY_ITEMS_URL,
          onSuccess: (dynamic response, int statusCode) {
            pharmacyItemsList = response['ListPharmcy_Region'];
            hasError = false;
            isFinished = true;
            errorMsg = "Done";
          }, onFailure: (String error, int statusCode) {
            isFinished = true;
            hasError = true;
            errorMsg = error;
          }, body: _itemsRequestModel.toJson());
      notifyListeners();
    } catch (error) {
      throw error;
    }
  }

   getPharmaciesList(int itemId) async {
    resetDefaultValues();
    try {
      _listRequestModel.itemID = itemId;
      isFinished = true;
      await BaseAppClient.post(PHARMACY_LIST_URL,
          onSuccess: (dynamic response, int statusCode) {
            pharmaciesList = response['PharmList'];
            hasError = false;
            isFinished = true;
            errorMsg = "Done";
          }, onFailure: (String error, int statusCode) {
            isFinished = true;
            hasError = true;
            errorMsg = error;
          }, body: _listRequestModel.toJson());
      notifyListeners();
    } catch (error) {
      throw error;
    }
  }

  resetDefaultValues() {
    isFinished = false;
    hasError = false;
    errorMsg = '';
    notifyListeners();
  }
}
