from models import ESAModel
from controllers import PreviewHelper
from helper import QueryParser


def search(keywords, categories, cluster_size, depth, delta):
    if not len(keywords):
        data = ESAModel.fetch_clusters(cluster_size, None, None, None, None)
        data = PreviewHelper.generate_keywords(data, cluster_size, depth, delta)
    else:
        keywords = keywords.lower()
        data = ESAModel.search_query_filter(keywords.split(","), categories, cluster_size)
        data = PreviewHelper.generate_keywords_filtered(keywords, data, categories, depth, delta)
    return {
        "result": data
    }


def search_v2(keywords, cluster_size, group, disipline, author, pub_period):
    if pub_period:
        pub_period = pub_period.split(" - ")

    if not len(keywords):
        data = ESAModel.fetch_clusters(cluster_size, group, disipline, author, pub_period)
        data = QueryParser.generate_clusters(data)
    else:
        keywords = keywords.lower()
        data = ESAModel.search_query_by_cluster(keywords.split(","), cluster_size, group, disipline, author, pub_period)
        data = QueryParser.generate_clusters(data)
    return {
        "result": data
    }


def search_v3(keywords, cluster_size, group, disipline, author, pub_period, uniq_keys):
    if pub_period:
        pub_period = pub_period.split(" - ")

    if not len(keywords):
        data = ESAModel.fetch_clusters(cluster_size, group, disipline, author, pub_period)
        data = QueryParser.generate_clouds([], data, "false", uniq_keys)
    else:
        keywords = keywords.lower()
        data = ESAModel.search_query_by_cluster(keywords.split(","), cluster_size, group, disipline, author, pub_period)
        data = QueryParser.generate_clouds(keywords, data, "false", uniq_keys)
    return {
        "result": data
    }


def facets(keywords, cluster_size, group, discipline, author, pub_period):
    if pub_period:
        pub_period = pub_period.split(" - ")
    data = ESAModel.get_facets(keywords, cluster_size, group, discipline, author, pub_period)
    return {
        "result": data
    }


def get_docs(keywords, categories, cluster_size, filters):
    if not len(keywords):
        data = ESAModel.fetch_clusters(cluster_size, None, None, None, None)
    else:

        keywords = keywords.lower()
        data = ESAModel.search_query_filter(keywords.split(","), categories, cluster_size)
        data = PreviewHelper.filter_documents(data, filters)

    return {
        "result": data
    }


def get_doc(id):
    data = ESAModel.get(id)
    return {
        "result": data
    }


def fetch_top_items(keywords, categories, cluster_size):
    return ESAModel.search_query_filter(keywords.split(","), categories, cluster_size)


def fetch_top_items_by_cluster(keywords, cluster_size, group, discipline, author, pub_period):
    if pub_period:
        pub_period = pub_period.split(" - ")
    data = ESAModel.search_query_by_cluster(keywords.split(","), cluster_size, group, discipline, author, pub_period)
    return QueryParser.top_documents(data)


def top_products_by_frq():
    return ESAModel.search_query_filter("spectroscopy".split(","), None, 4)


def top_products_by_frq_clustered(cluster_size, group, discipline, author, pub_period):
    if pub_period:
        pub_period = pub_period.split(" - ")
    data = ESAModel.search_query_by_cluster(None, cluster_size, group, discipline, author, pub_period)

    return QueryParser.top_documents(data)
