from flask import Flask, render_template, json, request
from controllers import SearchController, Config

app = Flask(__name__)


########################################################################################################################
#                                                                                                                      #
#                                                                                                                      #
#                                       App Prototype v1                                                               #
#                                                                                                                      #
#                                                                                                                      #
########################################################################################################################

@app.route('/')
def home():
    return render_template('index.html', keyword=request.args.get('query', ""),
                           delta_cloud=request.args.get('delta_cloud', "false"),
                           category_filter=",".join(request.args.getlist("cat[]", None)),
                           cluster=",".join(request.args.getlist("cluster", None)),
                           depth=request.args.get("depth", 0),
                           depth_config=list(Config.DEPTH.values()),
                           main_filter=",".join(request.args.getlist("main_cat[]", None))
                           )


@app.route('/data', methods=['GET', 'POST'])
def charts_data():
    res = SearchController.search(
        request.args.get('query', None),
        request.args.get('cluster', None),
        request.args.get('cluster_size', 0),
        request.args.get('depth', None),
        request.args.get('delta_cloud', "false")
    )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )

    return response


@app.route('/top_items', methods=['GET'])
def top_items():
    if request.args.get("query", None):
        res = SearchController.fetch_top_items(request.args.get('query'),
                                               request.args.get('filter'),
                                               request.args.get('cluster_size')
                                               )
    else:
        res = SearchController.top_products_by_frq()

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )
    return response


@app.route('/docs', methods=['GET', 'POST'])
def docs():
    res = SearchController.get_docs(
        request.args.get('query', None),
        request.args.get('cluster', None),
        request.args.get('cluster_size', 0),
        request.args.get('name', None))

    return render_template('docs.html', keyword=request.args.get('query', ""),
                           delta_cloud=request.args.get('delta_cloud', "false"),
                           category_filter=",".join(request.args.getlist("cat[]", None)),
                           cluster=",".join(request.args.getlist("cluster", None)),
                           depth=request.args.get("depth", 0),
                           main_filter=",".join(request.args.getlist("main_cat[]", None)),
                           data=res["result"]
                           )


@app.route('/doc', methods=['GET', 'POST'])
def preview():
    res = SearchController.get_doc(request.args.get('id', None))
    return app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )


########################################################################################################################
#                                                                                                                      #
#                                                                                                                      #
#                                       App Prototype v2                                                               #
#                                                                                                                      #
#                                                                                                                      #
########################################################################################################################

@app.route('/v2/index')
def home2():
    return render_template('index2.html', keyword=request.args.get('query', ""),
                           delta_cloud=request.args.get('delta_cloud', "false"),
                           category_filter=",".join(request.args.getlist("cat[]", None)),
                           cluster=",".join(request.args.getlist("cluster", None)),
                           depth=request.args.get("depth", 0),
                           depth_config=list(Config.DEPTH.values()),
                           main_filter=",".join(request.args.getlist("main_cat[]", None))
                           )


@app.route('/v2/data', methods=['GET', 'POST'])
def charts_data_v2():
    res = SearchController.search_v2(
        request.args.get('query', None),
        request.args.get('cluster_size', 0),
        request.args.get('grp_name', None),
        request.args.get('disp_name', None),
        request.args.get('author_name', None),
        request.args.get('pub_period', None)
    )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )

    return response


@app.route('/v2/facets', methods=['GET', 'POST'])
def facets_v2():
    keywords = []
    if request.args.get('query', None):
        keywords = request.args.get('query').split(",")

    res = SearchController.facets(
        keywords,
        request.args.get('cluster_size', 0),
        request.args.get('grp_name', None),
        request.args.get('disp_name', None),
        request.args.get('author_name', None),
        request.args.get('pub_period', None)
    )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )

    return response


@app.route('/v2/top_items', methods=['GET'])
def top_items_v2():
    if request.args.get("query", None):
        res = SearchController.fetch_top_items_by_cluster(request.args.get('query'),
                                                          request.args.get('cluster_size'),
                                                          request.args.get('grp_name'),
                                                          request.args.get('disp_name'),
                                                          request.args.get('author_name'),
                                                          request.args.get('pub_period', None)
                                                          )
    else:
        res = SearchController.top_products_by_frq_clustered(request.args.get('cluster_size'),
                                                             request.args.get('grp_name'),
                                                             request.args.get('disp_name'),
                                                             request.args.get('author_name'),
                                                             request.args.get('pub_period', None)
                                                             )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )
    return response


########################################################################################################################
#                                                                                                                      #
#                                                                                                                      #
#                                       App Prototype v3                                                               #
#                                                                                                                      #
#                                                                                                                      #
########################################################################################################################

@app.route('/v3/index')
def home3():
    return render_template('index3.html', keyword=request.args.get('query', ""),
                           delta_cloud=request.args.get('delta_cloud', "false"),
                           category_filter=",".join(request.args.getlist("cat[]", None)),
                           cluster=",".join(request.args.getlist("cluster", None)),
                           depth=request.args.get("depth", 0),
                           depth_config=list(Config.DEPTH.values()),
                           main_filter=",".join(request.args.getlist("main_cat[]", None))
                           )


@app.route('/v3/data', methods=['GET', 'POST'])
def charts_data_v3():
    res = SearchController.search_v3(
        request.args.get('query', None),
        request.args.get('cluster_size', 0),
        request.args.get('grp_name', None),
        request.args.get('disp_name', None),
        request.args.get('author_name', None),
        request.args.get('pub_period', None),
        request.args.get('uniq_keys', 1)
    )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )

    return response


@app.route('/v3/facets', methods=['GET', 'POST'])
def facets_v3():
    keywords = []
    if request.args.get('query', None):
        keywords = request.args.get('query').split(",")

    res = SearchController.facets(
        keywords,
        request.args.get('cluster_size', 0),
        request.args.get('grp_name', None),
        request.args.get('disp_name', None),
        request.args.get('author_name', None),
        request.args.get('pub_period', None)
    )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )

    return response


@app.route('/v3/top_items', methods=['GET'])
def top_items_v3():
    if request.args.get("query", None):
        res = SearchController.fetch_top_items_by_cluster(request.args.get('query'),
                                                          request.args.get('cluster_size'),
                                                          request.args.get('grp_name'),
                                                          request.args.get('disp_name'),
                                                          request.args.get('author_name'),
                                                          request.args.get('pub_period', None)
                                                          )
    else:
        res = SearchController.top_products_by_frq_clustered(request.args.get('cluster_size'),
                                                             request.args.get('grp_name'),
                                                             request.args.get('disp_name'),
                                                             request.args.get('author_name'),
                                                             request.args.get('pub_period', None)
                                                             )

    response = app.response_class(
        response=json.dumps(res),
        status=200,
        mimetype='application/json'
    )
    return response


########################################################################################################################
#                                                                                                                      #
#                                                                                                                      #
#                                       Start App                                                                      #
#                                                                                                                      #
#                                                                                                                      #
########################################################################################################################
if __name__ == '__main__':
    app.run()
