from meteoflow.client import WeatherClient
from meteoflow.location import LocationCoords, LocationSlug
from meteoflow.options import ForecastOptions


class FakeTransport(object):
    def __init__(self):
        self.last = None

    def get(self, path, params=None, headers=None):
        self.last = {
            "path": path,
            "params": params,
            "headers": headers,
        }
        return {"ok": True}


def test_weather_current_slug_auth_header():
    transport = FakeTransport()
    client = WeatherClient("TOKEN", transport=transport)
    client.current(LocationSlug("london"))
    assert transport.last["path"] == "/v2/current/"
    assert transport.last["params"] == {"slug": "london"}
    assert transport.last["headers"]["X-token"] == "TOKEN"


def test_weather_forecast_coords_options():
    transport = FakeTransport()
    client = WeatherClient("TOKEN", transport=transport, default_units="metric", default_lang="en")
    options = ForecastOptions(days=2, units="imperial", lang="ru")
    client.forecast_daily(LocationCoords(10, 20), options)
    assert transport.last["path"] == "/v2/forecast/by-days/"
    assert transport.last["params"]["lat"] == 10.0
    assert transport.last["params"]["lon"] == 20.0
    assert transport.last["params"]["days"] == 2
    assert transport.last["params"]["unit"] == "imperial"
    assert transport.last["params"]["lang"] == "ru"


def test_weather_auth_query():
    transport = FakeTransport()
    client = WeatherClient("TOKEN", transport=transport, auth_in_query=True)
    client.current(LocationSlug("london"))
    assert transport.last["params"]["key"] == "TOKEN"
