import requests
from pprint import pprint

# Asystent podróży
# Api pogoodwe - https://openweathermap.org/api
# Api z informacjami o krajach - https://restcountries.com/
# Api z informacjami o kursach walut - https://api.nbp.pl/#info


API_KEY = "5559491e991a00f95a22b5ee57ff1fa0"
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']
   # city = response.json()[0]['name']#
    country = response.json()[0]['country']
    return lat, lon, country

def get_weather_info(lat,lon):
    response = requests.get(f'https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&limit=1&appid={API_KEY}&lang=PL&units=metric')
    #print(response.status_code)
    #pprint(response.json())
    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 get_currency_code(country_code):
    url = f"https://restcountries.com/v3.1/alpha/{country_code.upper()}"
    response = requests.get(url)
    currency_code = list(response.json()[0]['currencies'].keys())[0]
    return currency_code

def get_country_full_name(country_code):
    url = f"https://restcountries.com/v3.1/alpha/{country_code.upper()}"
    response = requests.get(url)
    country_name = response.json()[0]['name']['common']
    return country_name



def get_currency_ratio(ori_curr,dest_curr):
    if ori_curr != "PLN":
        url =f"https://api.nbp.pl/api/exchangerates/rates/A/{ori_curr.lower()}/"
        response = requests.get(url)
        ori_ratio = response.json()['rates'][0]['mid']
    else:
        ori_ratio = 1

    if ori_curr != "PLN":
        url =f"https://api.nbp.pl/api/exchangerates/rates/A/{dest_curr.lower()}/"
        response = requests.get(url)
        dest_ratio = response.json()['rates'][0]['mid']
    else:
        dest_ratio = 1
    ratio = float(ori_ratio)/float(dest_ratio)
    return ratio


origin_place = None 
destination_place = None
while True:
    print('''Jaką akcje chcesz wykonać
          1. Podaj miejsce startowe
          2.Podaj miejsce docelowe
          3.Sprawdz lokalizacje miejsca startowego 
          4.Sprawdz lokalizacje miejsca docelowego
          5.Sprawdz pogode miejsca startowego
          6.Sprawdz pogode miejca docelowego
          7.Dowiedz sie wiecej o walucie
          8.Koniec
          ''')
    choosen_option= int(input())
    if choosen_option == 1 :
        origin_place = input("Podaj miasto startowen.\n")
        
    elif choosen_option == 2:
        destination_place_place = input("Podaj miasto docelowe.\n")
        
    elif choosen_option == 3:
        if origin_place is not None:
            lat,lon,country  = check_coordinates(origin_place,API_KEY)
            country_name = get_country_full_name(country)
            print(f"Miasto:{origin_place}, kraj :{country_name},lattitiude:{lat},longtitude{lon}")
        else:
            print("Najpierw podaj miasto startowe") 
    elif choosen_option == 4:
        if destination_place is not None:
            lat,lon,country  = check_coordinates(origin_place,API_KEY)
            country_name = get_country_full_name(country)
            print(f"Miasto:{origin_place}, kraj :{country_name},lattitiude:{lat},longtitude{lon}")
        else:
            print("Najpierw podaj miasto docelowe") 
    elif choosen_option == 5:
        lat,lon =check_coordinates(origin_place,API_KEY)
        weather  = temp , pres , hum = get_weather_info(lat,lon)
        print(f"Pogoda dla miasta {origin_place}:{weather}")
        print(f"Temperatura {temp}")
        print(f"Ciśnienienie {pres}")
        print(f"Wilgotnosc {hum}")


    elif choosen_option == 6:
        lat,lon =check_coordinates(destination_place,API_KEY)
        weather  = temp , pres , hum = get_weather_info(lat,lon)
        print(f"Pogoda dla miasta {destination_place}:{weather}")
        print(f"Temperatura {temp}")
        print(f"Ciśnienienie {pres}")
        print(f"Wilgotnosc {hum}")

    elif choosen_option == 7:
        pass
    elif choosen_option == 8:
        quit()
    else:
        print("Podaj błedną opcję")
    print("Nacisnij enter aby kontynuować")
    input()


    
    
    