from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from algo.visualization import create_html
from algo.training_data import TrainingData
import sys
import logging


class NLUVisualizationServer(object):
    def __init__(self, config):
        self.server = None
        self.config = config

    def start(self):
        self.server = HTTPServer(('', 8080), lambda *args: VisualizationRequestHandler(*args))
        logging.info('Started http server at http://0.0.0.0:8080')
        self.server.serve_forever()

    def stop(self):
        logging.info('^C received. Aborting.')
        if self.server is not None:
            logging.info('shutting down server')
            self.server.socket.close()


class VisualizationRequestHandler(BaseHTTPRequestHandler):
    def __init__(self, *args):
        BaseHTTPRequestHandler.__init__(self, *args)

    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        data_file = sys.argv[1]
        training_data = TrainingData(data_file, 'mitie', 'en')
        data = create_html(training_data)
        self.wfile.write(data.encode('utf-8'))
        return


if __name__ == "__main__":

    try:
        server = NLUVisualizationServer(None)
        server.start()
    except KeyboardInterrupt:
        server.stop()
