
import 'package:geolocator/geolocator.dart';
import 'package:http/http.dart';

import '../../Model/Response/Appconfig/Appconfigresponse.dart';
import '../Constants/Apiconstants.dart';
import 'app_config_service.dart';
import 'dart:convert' as convert;

class LocationService {
  AppConfigurationService appConfigurationService = AppConfigurationService();


  Future<AppConfigResponse?> getGoogleApi() async{
    var result = await appConfigurationService.getAppConfig("${ApiConstants.configUrl}?configid=1");
    return result;

  }

  Future<List> getPlaceId(String input) async{
    final String key = "AIzaSyCF4KatYCI3vqz1_H3kiHeyS3yCMfYToh8";
    // https://maps.googleapis.com/maps/api/place/autocomplete/json?input=podanur&components=country:IN&key=AIzaSyCF4KatYCI3vqz1_H3kiHeyS3yCMfYToh8
    String base = "https://maps.googleapis.com/maps/api/place/autocomplete/json";
    String path = "$base?input=$input&components=country:IN&key=$key";
    print(path);
    var response  =  await get(Uri.parse(path));
    var json =  convert.jsonDecode(response.body);
    print(convert.jsonDecode(response.body));
    print(response.body);
    var placeId = json['predictions'];
    return placeId;
  }

  Future<Map<String, dynamic>> getPlaces(String input) async{
    final String key = "AIzaSyCF4KatYCI3vqz1_H3kiHeyS3yCMfYToh8";

    String base = "https://maps.googleapis.com/maps/api/place/details/json";
    String path = "$base?key=$key&placeid=$input";
    print('path $path');
    var response  =  await get(Uri.parse(path));
    var json =  convert.jsonDecode(response.body);
    print(convert.jsonDecode(response.body));
    var results = json['result'];

    return results;

  }


  Future<Position> getLocation() async {
    getGoogleApi();
    bool serviceEnabled;
    LocationPermission permission;

    // Test if location services are enabled.
    // serviceEnabled = await Geolocator.isLocationServiceEnabled();
    // if (!serviceEnabled) {
    //   // permission = await Geolocator.requestPermission();
    //   await Geolocator.openLocationSettings();
    //
    //   // Location services are not enabled don't continue
    //   // accessing the position and request users of the
    //   // App to enable the location services.
    //   return Future.error('Location services are disabled.');
    // }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        return Future.error('Location permissions are denied');
      }
    }

    if (permission == LocationPermission.deniedForever) {
      await Geolocator.openLocationSettings();
      // Permissions are denied forever, handle appropriately.
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }

    Position position = await Geolocator.getCurrentPosition();

    print(position);
    return position;
  }


}