#===============================================================
# @author:  nityanarayan44@live.com
# @written: 08 December 2021
# @desc:    Routes for the Backend server
#===============================================================

# Import section with referecne of entry file or main file;
from __main__ import application
from flask import jsonify, render_template, url_for, request, redirect
# Local sample data import
from app.config.uiconfig import app_ui_config
from app import sample_data


# ==============================================================
# App Routes/Gateways
# ==============================================================
@application.route('/test', methods=['GET'])
def test():
    return '<h4>HELLO WORLD!</h4><hr/> it works!'

@application.route('/', methods=['GET'])
@application.route('/home', methods=['GET'])
@application.route('/dashboard', methods=['GET'])
def root():
    return render_template("dashboard.html", app_data=app_ui_config, data=sample_data.latest_data)


@application.route('/history', methods=['GET'])
def history():
    return render_template("history.html", app_data=app_ui_config, data=sample_data.history_data)


@application.route('/about', methods=['GET'])
def about():
    return render_template("about.html", app_data=app_ui_config, data=sample_data.latest_data)


@application.route('/get-notes', methods=['POST'])
def get_todo():
    print("KEY :: VALUE (from the received form data)")
    print([(key, val) for key, val in zip(request.form.keys(), request.form.values())])
    return redirect("/notes", code=302)


@application.route('/notes')
def info():
    return render_template("notes.html", app_data=app_ui_config)
 

@application.route('/sample-data')
def get_sample_data():
    return jsonify(app_ui_config)

    
# ==============================================================
# Error Handlers Starts
# ==============================================================
# 404 Handler; We can also pass the specific request errors codes to the decorator;
@application.errorhandler(404)
def not_found(err):
    return render_template("error.html", app_data=app_ui_config, error_data=err), 400


# Exception/Error handler; We can also pass the specific errors to the decorator;
@application.errorhandler(TypeError)
def server_error(err):
    application.logger.exception(err)
    return render_template("error.html", app_data=app_ui_config, error_data=err), 500


# Exception/Error handler; We can also pass the specific errors to the decorator;
@application.errorhandler(Exception)
def server_error(err):
    application.logger.exception(err)
    return render_template("error.html", app_data=app_ui_config, error_data=err), 500
# ==============================================================
# Error Handlers Ends
# ==============================================================

# Route For Sample data
@application.route('/data')
def get_data():
    data = {
        "reports": [
            {
                "build": "build_no",
                "created": "Imported 05052021T11:30:00:00IST",
                "platform": "Imported Win/Unix/Mac",
                "project_name": "project_name_1",
                "report_location_path": "path/to/report/location/index.html",
                "report_summary": {"pass": "50", "fail": "0", "ignored": "0", "skipped": "0"},
                "total_time": "35 min."
            },
            {
                "build": "build_no",
                "created": "Imported 05052021T11:30:00:00IST",
                "platform": "Imported Win/Unix/Mac",
                "project_name": "project_name_2",
                "report_location_path": "path/to/report/location/index.html",
                "report_summary": {"pass": "10", "fail": "2", "ignored": "0", "skipped": "0"},
                "total_time": "0.2345 secs."
            },
            {
                "build": "build_no",
                "created": "Imported 05052021T11:30:00:00IST",
                "platform": "Imported Win/Unix/Mac",
                "project_name": "project_name_3",
                "report_location_path": "path/to/report/location/index.html",
                "report_summary": {"pass": "100", "fail": "5", "ignored": "0", "skipped": "0"},
                "total_time": "5 days"
            }
        ]
    }
    return jsonify(data)

# ==============================================================
# Extra routes starts
# ==============================================================
@application.route('/sample1')
def sample1():
    return render_template("web-analytics-overview.html")


@application.route('/sample2')
def sample2():
    return render_template("web-analytics-real-time.html")


@application.route('/logo')
def get_logo():
    """
    Queries the snapshot data for both Serenity and JMeter projects from the MongoDB.
    Renders the Snapshot view of html
    :return: N/A
    """
    # set template directory of the Flask App to the  path set by the user as command line arg.
    return f'<html><head><title>Root</title><head><body><hr/> Welcome to the main page <hr/> ' \
           f'Building image from static public location: <br/> ' \
           f'<img src=\'{url_for("static", filename="images/logo.svg")}\' /> </body></html>'


[((18, 1, 18, 44), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((22, 1, 22, 40), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((23, 1, 23, 44), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((24, 1, 24, 49), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((29, 1, 29, 47), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((34, 1, 34, 45), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((39, 1, 39, 50), '__main__.application.route', 'application.route', (), '', False, 'from __main__ import application\n'), ((46, 1, 46, 28), '__main__.application.route', 'application.route', ({(46, 19, 46, 27): '"""/notes"""'}, {}), "('/notes')", False, 'from __main__ import application\n'), ((51, 1, 51, 34), '__main__.application.route', 'application.route', ({(51, 19, 51, 33): '"""/sample-data"""'}, {}), "('/sample-data')", False, 'from __main__ import application\n'), ((60, 1, 60, 30), '__main__.application.errorhandler', 'application.errorhandler', ({(60, 26, 60, 29): '(404)'}, {}), '(404)', False, 'from __main__ import application\n'), ((66, 1, 66, 36), '__main__.application.errorhandler', 'application.errorhandler', ({(66, 26, 66, 35): 'TypeError'}, {}), '(TypeError)', False, 'from __main__ import application\n'), ((73, 1, 73, 36), '__main__.application.errorhandler', 'application.errorhandler', ({(73, 26, 73, 35): 'Exception'}, {}), '(Exception)', False, 'from __main__ import application\n'), ((82, 1, 82, 27), '__main__.application.route', 'application.route', ({(82, 19, 82, 26): '"""/data"""'}, {}), "('/data')", False, 'from __main__ import application\n'), ((120, 1, 120, 30), '__main__.application.route', 'application.route', ({(120, 19, 120, 29): '"""/sample1"""'}, {}), "('/sample1')", False, 'from __main__ import application\n'), ((125, 1, 125, 30), '__main__.application.route', 'application.route', ({(125, 19, 125, 29): '"""/sample2"""'}, {}), "('/sample2')", False, 'from __main__ import application\n'), ((130, 1, 130, 27), '__main__.application.route', 'application.route', ({(130, 19, 130, 26): '"""/logo"""'}, {}), "('/logo')", False, 'from __main__ import application\n'), ((26, 11, 26, 98), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((31, 11, 31, 97), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((36, 11, 36, 94), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((43, 11, 43, 39), 'flask.redirect', 'redirect', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((48, 11, 48, 64), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((53, 11, 53, 33), 'flask.jsonify', 'jsonify', ({(53, 19, 53, 32): 'app_ui_config'}, {}), '(app_ui_config)', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((68, 4, 68, 37), '__main__.application.logger.exception', 'application.logger.exception', ({(68, 33, 68, 36): 'err'}, {}), '(err)', False, 'from __main__ import application\n'), ((75, 4, 75, 37), '__main__.application.logger.exception', 'application.logger.exception', ({(75, 33, 75, 36): 'err'}, {}), '(err)', False, 'from __main__ import application\n'), ((115, 11, 115, 24), 'flask.jsonify', 'jsonify', ({(115, 19, 115, 23): 'data'}, {}), '(data)', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((122, 11, 122, 57), 'flask.render_template', 'render_template', ({(122, 27, 122, 56): '"""web-analytics-overview.html"""'}, {}), "('web-analytics-overview.html')", False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((127, 11, 127, 58), 'flask.render_template', 'render_template', ({(127, 27, 127, 57): '"""web-analytics-real-time.html"""'}, {}), "('web-analytics-real-time.html')", False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((62, 11, 62, 80), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((69, 11, 69, 80), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((76, 11, 76, 80), 'flask.render_template', 'render_template', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((138, 12, 138, 57), 'flask.url_for', 'url_for', (), '', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((42, 42, 42, 61), 'flask.request.form.keys', 'request.form.keys', ({}, {}), '()', False, 'from flask import jsonify, render_template, url_for, request, redirect\n'), ((42, 63, 42, 84), 'flask.request.form.values', 'request.form.values', ({}, {}), '()', False, 'from flask import jsonify, render_template, url_for, request, redirect\n')]