from time import sleep
import requests
import os
import threading


class Weather:
    def __init__(self):
        self.weather = self.get_weather()

    def get_weather(self):
        api_key = os.environ["WEATHER_API_KEY"]
        location = os.environ["WEATHER_LOCATION"]
        units = "imperial"  # TODO: Make environment variable
        url = f"https://api.tomorrow.io/v4/weather/realtime?location={location}&units={units}&apikey={api_key}"
        headers = {"accept": "application/json"}
        response = requests.get(url, headers=headers)
        return response.json()

    def start_update_loop(self):
        thread = threading.Thread(target=self.update_loop)
        thread.daemon = True
        thread.start()

    def update_loop(self):
        while True:
            sleep(1200)
            self.weather = self.get_weather()
