package nominatim

import (
	"strconv"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestNewClient(t *testing.T) {
	assert := assert.New(t)
	c, err := New("my siteName")
	assert.NoError(err)

	assert.Equal("my siteName", c.siteName)

	_, err = New("")
	assert.Error(err)
}

func TestSearch(t *testing.T) {
	assert := assert.New(t)
	c, err := New("my siteName")
	assert.NoError(err)

	r, err := c.Search("bergen")
	assert.NoError(err)

	assert.NotEmpty(r)
	firstResult := (*r)[0]
	lon, err := strconv.ParseFloat(*firstResult.Longitude, 64)
	assert.NoError(err)
	lat, err := strconv.ParseFloat(*firstResult.Latitude, 64)
	assert.NoError(err)

	assert.Equal(5.3259192, lon)
	assert.Equal(60.3943055, lat)

	assert.Nil(firstResult.Address)
}

func TestSearchWithSpaceInQuery(t *testing.T) {
	assert := assert.New(t)
	c, err := New("my siteName")
	assert.NoError(err)

	_, err = c.Search("drotningsvik senter")
	assert.NoError(err)
}

func TestLookup(t *testing.T) {
	assert := assert.New(t)
	c, err := New("my siteName")
	assert.NoError(err)

	r, err := c.Lookup("bergen")
	assert.NoError(err)

	assert.NotEmpty(r)

	assert.Equal(5.3259192, r.Longitude)
	assert.Equal(60.3943055, r.Latitude)

	_, err = c.Lookup("")
	assert.Error(err)
}

func TestReverse(t *testing.T) {
	assert := assert.New(t)
	c, err := New("my siteName")
	assert.NoError(err)

	r, err := c.Reverse(60.3943055, 5.3259192)
	assert.NoError(err)

	assert.NotEmpty(r)

	assert.Equal("5.325973", *r.Longitude)
	assert.Equal("60.394487", *r.Latitude)
	assert.Equal("", *r.Name)

	_, err = c.Reverse(-10000, -10000)
	assert.Error(err)
}