import 'package:dio/dio.dart';

import 'base_repository.dart';

class GoogleApisRepository extends BaseRepository {
  GoogleApisRepository({required super.graphql, required super.dio});

  Future<String?> getAddressFromCoordinates(
      double latitude, double longitude) async {
    final dio = Dio();
    const apiKey = "AIzaSyAVUr4Ku4O1HlSkK8n9KGnUyqvsXBL-yfs";
    final latitudeString = latitude.toString();
    final longitudeString = longitude.toString();
    const url =
        'https://maps.googleapis.com/maps/api/geocode/json';

    try {
      final response = await dio.get(
        url,
        queryParameters: {
          'latlng': '$latitudeString,$longitudeString',
          'key': apiKey,
        },
      );

      if (response.statusCode == 200) {
        final data = response.data;
        // Mengambil compound_code dari hasil JSON
        final plusCode = data['plus_code'];
        if (plusCode != null && plusCode['compound_code'] != null) {
          return plusCode['compound_code'];
        } else {
          print('Alamat Tidak Ditemukan');
          return null;
        }
      } else {
        print('Failed to load data');
        return null;
      }
    } catch (e) {
      print('Error: $e');
      return null;
    }
  }
}
