from datetime import datetime
import pytz
import time


def get_parameters(request, supported_buildings):
    """ Storing and error checking request parameters.

    Parameters
    ----------
    request                 : gRPC request
        Contains parameters to fetch data.
    supported_buildings     : list(str)
        List of buildings available.

    Returns
    -------
    str
        Error message. If no error message, then return None.

    """

    start_time = datetime.utcfromtimestamp(float(request.start / 1e9)).replace(tzinfo=pytz.utc)
    end_time = datetime.utcfromtimestamp(float(request.end / 1e9)).replace(tzinfo=pytz.utc)

    if any(not elem for elem in [request.building, start_time, end_time,
                                 request.aggregate, request.window, request.point_type]):
        return "invalid request, empty param(s)"

    if request.start > int(time.time() * 1e9) or request.end > int(time.time() * 1e9):
        return "invalid request, start/end date is in the future"

    if request.start >= request.end:
        return "invalid request, start date is equal or after end date."

    if request.building not in supported_buildings:
        return "invalid request, building not found; supported buildings: " + str(supported_buildings)

    return None

    # # Other error checkings
    # duration = utils.get_window_in_sec(request.window)
    # if duration <= 0:
    #     return None, "invalid request, duration is negative or zero"
    # if request.start + (duration * 1e9) > request.end:
    #     return None, "invalid request, start date + window is greater than end date"