# Path: cardinal/decorators.py
# def command(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Command must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.commands = triggers
#         return f
# 
#     return wrap
# 
# def help(lines):
#     # For backwards compatibility
#     if isinstance(lines, str):
#         lines = [lines]
# 
#     if not isinstance(lines, list):
#         raise TypeError("Help must be a help string or list of help strings")
# 
#     def wrap(f):
#         # Create help list or prepend to it
#         if not hasattr(f, 'help'):
#             f.help = lines
#         else:
#             f.help = lines + f.help
# 
#         return f
# 
#     return wrap


import logging
import requests
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from cardinal.decorators import command, help

class WeatherAPIClient:
    API_ENDPOINT = "https://api.weatherapi.com/v1/current.json"

    def __init__(self, api_key):
        self.api_key = api_key

    @defer.inlineCallbacks
    def get_forecast(self, location) -> Forecast:
        params = {
            'q': location,
            'key': self.api_key,
        }

        r = yield deferToThread(
            requests.get,
            self.API_ENDPOINT,
            params=params
        )

        return self.parse_forecast(r.json())

    def parse_forecast(self, res):
        location = res['location']['name']
        if res['location']['region']:
            location += ", {}, {}".format(res['location']['region'],
                                          res['location']['country'])
        else:
            location += ", {}".format(res['location']['country'])


# Path: cardinal/decorators.py
# def command(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Command must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.commands = triggers
#         return f
# 
#     return wrap
# 
# def help(lines):
#     # For backwards compatibility
#     if isinstance(lines, str):
#         lines = [lines]
# 
#     if not isinstance(lines, list):
#         raise TypeError("Help must be a help string or list of help strings")
# 
#     def wrap(f):
#         # Create help list or prepend to it
#         if not hasattr(f, 'help'):
#             f.help = lines
#         else:
#             f.help = lines + f.help
# 
#         return f
# 
#     return wrap


# Path: plugins/weather/plugin.py
import logging
import requests
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from cardinal.decorators import command, help


class WeatherAPIClient:
    API_ENDPOINT = "https://api.weatherapi.com/v1/current.json"

    def __init__(self, api_key):
        self.api_key = api_key

    @defer.inlineCallbacks
    def get_forecast(self, location) -> Forecast:
        params = {
            'q': location,
            'key': self.api_key,
        }

        r = yield deferToThread(
            requests.get,
            self.API_ENDPOINT,
            params=params
        )

        return self.parse_forecast(r.json())

    def parse_forecast(self, res):
        location = res['location']['name']
        if res['location']['region']:
            location += ", {}, {}".format(res['location']['region'],
                                          res['location']['country'])
        else:
            location += ", {}".format(res['location']['country'])


        return Forecast(