__author__ = 'Will Hart'

from blitz.data.models import *
from blitz.utilities import to_blitz_date
import json
from tornado.web import RequestHandler


class CategoriesHandler(RequestHandler):
    def get(self):
        """
        handles a GET request to /categories by writing a
        JSON list of categories currently in the cache
        """

        data = self.settings['data']
        result = data.get_cache_variables()
        json_obj = {}
        data_objs = []

        for r in result:
            data_objs.append(r.to_dict())

        # remove the last comma
        json_obj['data'] = data_objs

        # write the response
        self.content_type = "application/json"
        self.write(json.dumps(json_obj))


class CacheHandler(RequestHandler):
    def get(self, since=None):
        """
        handles a GET request to /cache by writing a
        JSON list of the last 50 values for each variable.

        If an argument is provided for "since" then only
        return values since that date. Otherwise return last 50
        """

        data = self.settings['data']
        json_objs = {}
        data_objs = []

        if since is not None:
            result = data.get_cache(float(since))
        else:
            result = data.get_cache()

        for r in result:
            data_objs.append(r.to_dict())

        json_objs['data'] = data_objs

        self.content_type = "application/json"
        self.write(json.dumps(json_objs))


class DownloadHandler(RequestHandler):
    def get(self, session_id):
        """
        handles a GET request to /download/{id} by requesting a download
        from the data logger for the given session ID and then returning a
        complete list of variable and values that were recorded during this
        logging session.
        """

        data = self.settings['data']
        json_objs = {}
        data_objs = []
        readings = data.get_session_readings(session_id)
        session = data.get_by_id(Session, session_id)

        for r in readings:
            data_objs.append(r.to_dict())

        json_objs['sessionId'] = session_id
        json_objs['timeStarted'] = to_blitz_date(session.timeStarted)
        json_objs['timeStopped'] = to_blitz_date(session.timeStopped)
        json_objs['numberOfReadings'] = len(data_objs)
        json_objs['data'] = data_objs

        self.content_type = "application/json"
        self.write(json.dumps(json_objs))


class SessionsHandler(RequestHandler):
    def get(self):
        """
        handles a GET request to /sessions and returns a complete
        list of logging sessions that are available for view or download
        """

        # TODO work out why this is returning an empty dataset
        data = self.settings['data']
        json_objs = {}
        data_objs = []
        result = data.all(Session)

        for r in result:
            data_objs.append(r.to_dict())

        self.content_type = "application/json"
        self.write(json.dumps(json_objs))


class SessionHandler(RequestHandler):
    def get(self, session_id):
        """
        handles a GET request to /session/{id} and returns
        a complete list of data relating to this session
        """

        # TODO work out why this is returning an empty dataset
        data = self.settings['data']
        json_objs = {}
        data_objs = []
        result = data.get_session_readings(session_id)

        for r in result:
            data_objs.append(r.to_dict())

        self.content_type = "application/json"
        self.write(json.dumps(json_objs))


class ConfigHandler(RequestHandler):
    def get(self):
        """
        handles a GET request to /config and returns
        a complete list of data relating to this session
        """

        # TODO work out why this is returning an empty dataset
        data = self.settings['data']
        json_objs = {}
        data_objs = []
        result = data.all(Config)

        self.content_type = "application/json"
        self.write(json.dumps(json_objs))

    def post(self):
        """
        handles a POST request to /config and saves
        updated configuration information to the data logger
        """

        # TODO implement
        self.content_type = "application/json"
        self.write("{'response': 'ok'}")

@@ -2671,72 +2671,8 @@
  %22%22%22
-%0A%0A        # TODO work out why this is returning an empty dataset
 %0A   
@@ -2847,32 +2847,71 @@
 d(r.to_dict())%0A%0A
+        json_objs%5B'data'%5D = data_objs%0A%0A
         self.con
@@ -3199,71 +3199,8 @@
 %22%22%0A%0A
-        # TODO work out why this is returning an empty dataset%0A
     
@@ -3394,32 +3394,70 @@
 d(r.to_dict())%0A%0A
+        json_objs%5B'data'%5D = data_objs%0A
         self.con
@@ -3726,71 +3726,8 @@
 %22%22%0A%0A
-        # TODO work out why this is returning an empty dataset%0A
     
@@ -3840,16 +3840,123 @@
 onfig)%0A%0A
+        for r in result:%0A            data_objs.append(r.to_dict())%0A%0A        json_objs%5B'data'%5D = data_objs%0A%0A
         
@@ -4182,16 +4182,16 @@
  logger%0A
-
         
@@ -4187,32 +4187,173 @@
 er%0A        %22%22%22%0A%0A
+        # get the response body as a dict%0A        config_json = self.get_argument('config', %22%7B%7D%22)%0A        config = json.loads(config_json)%0A%0A%0A
         # TODO i

Write missing responses to the API document