import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/pharmacies_list_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/pharmacies_model.dart';

class PharmacyService extends BaseService {
  List<PharmaciesModel> _medicineItem = List();
  List<PharmaciesListModel> _pharmaciesList = List();

  List<PharmaciesModel> get medicineItem => _medicineItem;
  List<PharmaciesListModel> get pharmaciesList => _pharmaciesList;

  bool isFinished = true;
  bool hasError = false;
  String errorMsg = '';

  void clearMedicineList() {
    _medicineItem.clear();
  }

  void clearPharmaciesList() {
    _pharmaciesList.clear();
  }

  PharmaciesModel _requestGetPharmaciesModel = PharmaciesModel(
      channel: 3,
      clinicID: 1,
      deviceTypeID: 2,
      doctorID: 1485,
      editedBy: 1485,
      generalid: "Cs2020@2016\$2958",
      iPAdress: "11.11.11.11",
      isDentalAllowedBackend: false,
      isLoginForDoctorApp: true,
      languageID: 2,
      pageIndex: 0,
      pageSize: 20,
      patientOutSA: 0,
      pHRItemName: "panadol",
      projectID: 15,
      sessionID: "BlUSkYymTt",
      stamp: "2020-04-27T12:17:17.721Z",
      tokenID: "6EfeKCLRu0GPdFutIt3m7Q==",
      versionID: 8.3);

  PharmaciesListModel _pharmaciesListModel = PharmaciesListModel(
    patientTypeID: 1,
    languageID: 2,
    stamp: '2020-04-23T21:01:21.492Z',
    iPAdress: '11.11.11.11',
    versionID: 8.3,
    sessionID: 'e29zoooEJ4',
    isLoginForDoctorApp: true,
    channel: 9,
    editedBy: 1485,
    doctorID: 1485,
    patientOutSA: 0,
    clinicID: 1,
    projectID: 15,
  );

  Future getMedicineList({String drugName}) async {
    hasError = false;
    _requestGetPharmaciesModel.pHRItemName = drugName;
    try {
      await baseAppClient.post(GET_PHARMCY_ITEMS,
          onSuccess: (dynamic response, int statusCode) {
        _medicineItem.clear();
        response['ListPharmcy_Region'].forEach((item) {
          _medicineItem.add(PharmaciesModel.fromJson(item));
        });
      }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
      }, body: _requestGetPharmaciesModel.toJson());
    } catch (error) {
      throw error;
    }
  }

  Future getPharmaciesList({int itemID}) async {
    _pharmaciesListModel.itemID = itemID;
    try {
      await baseAppClient.post(GET_PHARMACY_LIST,
          onSuccess: (dynamic response, int statusCode) {
        _pharmaciesList.clear();
        response['PharmList'].forEach((item) {
          _pharmaciesList.add(PharmaciesListModel.fromJson(item));
          hasError = false;
        });
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: _pharmaciesListModel.toJson());
    } catch (error) {
      throw error;
    }
  }
}
