import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/model/hospitals/hospitals_model.dart';
import 'package:diplomaticquarterapp/core/model/hospitals/request_get_hospitals_model.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:geolocator/geolocator.dart';

class HospitalService extends BaseService {
  List<HospitalsModel> _hospitals = List();

  List<HospitalsModel> get hospitals => _hospitals;

  double _latitude;
  double _longitude;

  _getCurrentLocation() async {
    await getLastKnownPosition().then((value) {
      _latitude = value.latitude;
      _longitude = value.longitude;
    }).catchError((e) {
      _longitude = 0;
      _latitude = 0;
    });
    //   currentLocation = LatLng(position.latitude, position.longitude);
  }

  Future getHospitals() async {
    await _getCurrentLocation();
    Map<String, dynamic> body = Map();
    body['Latitude'] = _latitude;
    body['Longitude'] = _longitude;

    await baseAppClient.post(GET_PROJECT,
        onSuccess: (dynamic response, int statusCode) {
      _hospitals.clear();
      response['ListProject'].forEach((hospital) {
        _hospitals.add(HospitalsModel.fromJson(hospital));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

}
