import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/pharmacies_list_model.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/pharmacies_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:geolocator/geolocator.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,
  );

  double _latitude=0;
  double _longitude=0;

  _getCurrentLocation() async {
    await Geolocator.getLastKnownPosition().then((value) {
      _latitude = value.latitude;
      _longitude = value.longitude;
    }).catchError((e) {
      _longitude = 0;
      _latitude = 0;
    });
  }

  Future getMedicineList({String drugName}) async {
    hasError = false;
    await _getCurrentLocation();
    Map<String,dynamic> body = Map();
    body['PHR_itemName'] = drugName.trim();
    body['isLoginForDoctorApp'] = true;
    body['isDentalAllowedBackend'] = true;
    body['PageSize'] = 25;
   body['Latitude'] = _latitude;
   body['Longitude'] = _longitude;

      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: body);

  }

  Future getPharmaciesList({int itemID}) async {

    await _getCurrentLocation();
    Map<String,dynamic> body = Map();
    body['ItemID'] = itemID;
    body['Latitude'] = _latitude;
    body['Longitude'] = _longitude;

      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:body);

  }
}
