//nolint:gocritic // explicit branching retained for clarity
package places

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/photoprism/photoprism/pkg/clean"
	"github.com/photoprism/photoprism/pkg/geo/s2"
)

// LatLng returns location details based on the specified latitude and longitude.
func LatLng(lat, lng float64, locale string) (result Location, err error) {
	if lat == 0.0 || lng == 0.0 {
		return result, ErrMissingCoordinates
	}

	// Remember start time.
	start := time.Now()

	// Generate query parameter string.
	values := url.Values{"lat": {fmt.Sprintf("%f", lat)}, "lng": {fmt.Sprintf("%f", lng)}}
	params := values.Encode()

	// Get request locale.
	locale = Locale(locale)

	// Create cache key based on query parameters.
	id := s2.Token(lat, lng)
	cacheKey := fmt.Sprintf("id:%s:%s", id, locale)

	// Are location results cached?
	if hit, ok := clientCache.Get(cacheKey); ok {
		log.Tracef("places: cache hit for %s [%s]", cacheKey, time.Since(start))
		cached := hit.(Location)
		cached.Cached = true
		return cached, nil
	}

	var r *http.Response

	// Query the specified places service URLs.
	for _, serviceUrl := range ReverseServiceUrls {
		reqUrl := fmt.Sprintf("%s?%s", serviceUrl, params)
		if r, err = GetRequest(reqUrl, locale); err == nil {
			break
		}
	}

	// Failed?
	if err != nil {
		log.Errorf("places: %s (location request failed)", err.Error())
		return result, err
	} else if r == nil {
		err = fmt.Errorf("location request could not be performed")
		return result, err
	} else if r.StatusCode >= 400 {
		err = fmt.Errorf("location request failed with code %d", r.StatusCode)
		return result, err
	}

	// Decode JSON response body.
	err = json.NewDecoder(r.Body).Decode(&result)

	if err != nil {
		log.Errorf("places: %s (decode location result)", err.Error())
		return result, err
	}

	if result.ID == "" {
		return result, fmt.Errorf("no location result for %s", id)
	}

	clientCache.SetDefault(cacheKey, result)
	log.Tracef("places: cached %s [%s]", clean.Log(cacheKey), time.Since(start))
	result.Cached = false

	return result, nil
}
