import json
import requests

from datetime import datetime, timedelta

# These are the coordinate of central London. Please change at your convenience
from wafl.exceptions import CloseConversation

latitude = "51.5074"
longitude = "0.1272"

_db_filename = "db.json"

def close_conversation():
    raise CloseConversation()

def check_today_weather():
    today = datetime.now().strftime("%Y-%m-%d")
    return check_weather_lat_long(latitude, longitude, today)


def check_tomorrow_weather():
    today = datetime.now()
    tomorrow = (today + timedelta(days=1)).strftime("%Y-%m-%d")
    return check_weather_lat_long(latitude, longitude, tomorrow)


def check_weather_lat_long(latitude, longitude, day):
    secrets = json.load(open("secrets.json"))
    print("HERE!")
    result = requests.get(
        f"https://rgw.5878-e94b1c46.eu-gb.apiconnect.appdomain.cloud/metoffice/production/v0/forecasts/point/daily?excludeParameterMetadata=true&includeLocationName=true&latitude={latitude}&longitude={longitude}",
        headers={
            "X-IBM-Client-Id": secrets["key"],
            "X-IBM-Client-Secret": secrets["secret"],
        },
    )
    print("HERE2!")
    data = result.json()
    if "features" not in data:
        return "There is a connection error to the weather API. Please try later"

    to_say = ""
    for item in data["features"][0]["properties"]["timeSeries"]:
        if day in item["time"]:
            print("HEREEE!")
            to_say += f"The temperature should be between {int(item['dayLowerBoundMaxTemp'])} and {int(item['dayUpperBoundMaxTemp'])}.\n"
            if item["dayProbabilityOfPrecipitation"] != 0:
                to_say += f"The probability of rain is {item['dayProbabilityOfPrecipitation']} percent.\n"

            if item["dayProbabilityOfSnow"] != 0:
                to_say += f"The probability of snow is {item['dayProbabilityOfSnow']} percent.\n"

    print(to_say)
    return to_say


def get_time():
    return datetime.now().strftime("%H:%M")


def get_date():
    return datetime.now().strftime("%Y-%m-%d")


def get_day():
    return datetime.now().strftime("%A")



def add_to_shopping_list(list_of_items_to_add):
    db = json.load(open(_db_filename))
    for item in list_of_items_to_add:
        if item not in db["shopping_list"]:
            db["shopping_list"].append(item)

    json.dump(db, open(_db_filename, "w"))

    return "Item added"


def remove_shopping_list(list_of_items_to_remove):
    db = json.load(open(_db_filename))
    for item in list_of_items_to_remove:
        if item in db["shopping_list"]:
            db["shopping_list"].remove(item)

    json.dump(db, open(_db_filename, "w"))

    return "Item removed"


def get_shopping_list():
    db = json.load(open(_db_filename))
    if db["shopping_list"] == []:
        return "nothing"

    return ", ".join(db["shopping_list"])


def write_to_file(filename, text):
    with open(filename, "w") as file:
        file.write(text)

    return f"File {filename} saved"
