import requests
from pprint import pprint

API_KEY = "59c907d62746a8922f21e6e9099b1422"

def get_weather_info(lat,lon):
    response = requests.get(f'http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}' +
                            f'&limit=1&appid={API_KEY}&lang=PL&units=metric')
    response_json = response.json()
    weather = response_json['weather'][0]['description']
    temperature = response_json['main']['temp']
    pressure = response_json['main']['pressure']
    humidity = response_json['main']['humidity']

    return weather, temperature, pressure, humidity
    

def check_coordinates(city, API_KEY):
    response = requests.get(f'http://api.openweathermap.org/geo/1.0/direct?q={city}&appid={API_KEY}')
    # print(response.status_code)
    # pprint(response.json())
    lat = response.json()[0]['lat']
    lon = response.json()[0]['lon']
    name = response.json()[0]['name']
    country = response.json()[0]['country']

    return lat, lon, name, country

check_coordinates('Warsaw', API_KEY)

print('Witaj, jestem Travelinator, twój inteligentny asystent podróży')

origin_city = input('Podaj nazwę miasta z którego podróżujesz: ')
destination_city = input('Podaj nazwę miasta do którego podróżujesz: ')

origin_lat, origin_lon, origin_name, origin_country = check_coordinates(origin_city, API_KEY)

destination_lat, destination_lon, destination_name, destination_country = check_coordinates(destination_city,
                                                                                             API_KEY)

weather, temperature, pressure, humidity = get_weather_info(destination_lat, destination_lon)

print(f'Miasto z którego podróżujesz: {origin_name}')
print(f'Miasto do którego podróżujesz: {destination_name}')
print(f'Wpółrzędne miasta do którego podróżujesz to: \n{destination_lat} szerokości \n{destination_lon} długości')

print(f'Pogoda: {weather}')
print(f'Temperatura: {temperature} stopni Celcujsza')
print(f'wilgotność: {humidity}%')
print(f'ciśnienie atmosferyczne: {pressure}hPa')