import json
from flask import request, session, Blueprint
from lexos.helpers import constants as constants
from lexos.managers import utility, session_manager as session_manager
from natsort import humansorted
from lexos.views.base import render

scrub_blueprint = Blueprint("scrub", __name__)


@scrub_blueprint.route("/scrub", methods=["GET"])
def scrub() -> str:
    """ Gets the scrub page.
    :return: The scrub page.
    """

    if "scrubbingoptions" not in session:
        session["scrubbingoptions"] = constants.DEFAULT_SCRUB_OPTIONS
    if "xmlhandlingoptions" not in session:
        session["xmlhandlingoptions"] = {
            "myselect": {"action": "", "attribute": ""}}
    utility.xml_handling_options()

    return render("scrub.html")


@scrub_blueprint.route("/scrub/download", methods=["GET"])
def download() -> str:
    """ Returns a download of the active files.
    :return: the zip files needs to be downloaded.
    """

    file_manager = utility.load_file_manager()
    return file_manager.zip_active_files("scrubbed-documents.zip")


@scrub_blueprint.route("/scrub/execute", methods=["POST"])
def execute() -> str:
    """ Scrubs the active documents.
    :return: A JSON object with previews of the scrubbed documents.
    """

    file_manager = utility.load_file_manager()

    session_manager.cache_alteration_files()
    session_manager.cache_scrub_options()

    # Save changes only if the "Apply Scrubbing" button is clicked.
    saving_changes = request.form["action"] == "apply"

    # Scrub.
    previews = file_manager.scrub_files(saving_changes=saving_changes)

    # Create the previews.
    previews = [[preview[1], preview[3]] for preview in previews]

    # Save the changes if requested.
    if saving_changes:
        utility.save_file_manager(file_manager)

    return json.dumps(previews)


@scrub_blueprint.route("/scrub/get-tag-options", methods=["GET"])
def get_tags_table() -> str:
    """ Gets the tags in the active documents.
    :return: The tags in the active documents.
    """

    utility.xml_handling_options()
    tags = humansorted(list(session["xmlhandlingoptions"].keys()))

    response = []
    for tag in tags:
        response.append([tag, session["xmlhandlingoptions"][tag]["action"],
                         session["xmlhandlingoptions"][tag]["attribute"]])

    return json.dumps(response)


@scrub_blueprint.route("/scrub/save-tag-options", methods=["POST"])
def xml() -> str:
    """ Sets the tag options.
    :return: None.
    """

    data = request.json
    utility.xml_handling_options(data)
    return ""

@@ -5,16 +5,17 @@
 rt json%0A
+%0A
 from fla
@@ -51,16 +51,64 @@
 lueprint
+, make_response%0Afrom natsort import humansorted%0A
 %0Afrom le
@@ -224,40 +224,8 @@
 ger%0A
-from natsort import humansorted%0A
 from
@@ -1005,27 +1005,46 @@
 nager()%0A
+%0A
     re
-turn 
+sponse = make_response(
 file_man
@@ -1065,16 +1065,25 @@
 e_files(
+%0A        
 %22scrubbe
@@ -1083,17 +1083,17 @@
 scrubbed
--
+_
 document
@@ -1099,16 +1099,249 @@
 ts.zip%22)
+)%0A%0A    # Disable download caching%0A    response.headers%5B%22Cache-Control%22%5D = %5C%0A        %22max-age=0, no-cache, no-store, must-revalidate%22%0A    response.headers%5B%22Expires%22%5D = 0%0A    response.headers%5B%22Pragma%22%5D = %22no-cache%22%0A%0A    return response
 %0A%0A%0A@scru

disable download caching