import io
from flask import request, Response
from db.repository import Repository
from processing.clustering.clusterer import Clusterer
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

repo = Repository()
clusterer = Clusterer()

def get_locations():
    clusters = repo.get_location_clusters()
    return [c.to_serializable_dict() for c in clusters]

def get_times():
    clusters = repo.get_time_clusters()
    return [c.to_serializable_dict() for c in clusters]

def get_image_1():
    return Response(status=501)
    # todo

    locations = repo.getLocations()

    fig = clusterer.draw_locations(locations)

    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype="image/png")


def get_image_2():
    return Response(status=501)
    # todo

    locations = repo.getLocations()

    fig = clusterer.draw_locations(locations)

    output = io.BytesIO()
    FigureCanvas(fig).print_png(output)
    return Response(output.getvalue(), mimetype="image/png")