package com.rdkr.tide_display.backend.images.services.mapProvider;

import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

@Component("GoogleMaps")
@RequiredArgsConstructor
public class GoogleMaps implements MapProvider {

  private final RestClient.Builder restClient;

  @Value("${GCP_MAP_ID:dummy}")
  private String mapId;

  @Value("${GCP_API_KEY:dummy}")
  private String apiKey;

  @Override
  public byte[] getStaticMap(double latitude, double longitude, int zoom) {
    val client = restClient.baseUrl("https://maps.googleapis.com/maps/api/staticmap").build();
    return client.get()
        .uri(builder -> builder
            .queryParam("center", latitude + "," + longitude)
            .queryParam("zoom", "" + zoom)
            .queryParam("size", "540x540")
            .queryParam("map_id", mapId)
            .queryParam("key", apiKey)
            .build())
        .retrieve()
        .body(byte[].class);
  }

}
