import pkg_resources
import json


def render(
        word, data, interlinks, topn=10, threshold=0, edge=1, sep=False, d3path=""
):
    """
    Generates the HTML page displaying the graph
    :param word: string containing the word for which we build the graph
    :param data: dictionary containing the collected data
    :param interlinks: list of links to other generated pages
    :param topn: integer, number of neighbors to extract for each word
    :param threshold: float, value from which we start drawing edges between nodes
    :param edge: integer, width of edges in the graph
    :param sep: if this parameter is used, token is split by a separator
                (underscore), and only first part is shown in visualization
                (E.g. it is useful when PoS is attached to a word).
    :param d3path: path to the JS file with D3.js library
    :return: string to be written as an output
    """
    html_template = pkg_resources.resource_filename('vec2graph', 'data/genviz.html')
    html = open(html_template, 'r').read()
    return (
        html.replace("d3pathplaceholder", d3path)
            .replace("wordplaceholder", word)
            .replace("splithyphen", str(sep).lower())
            .replace("dataplaceholder", json.dumps(data).replace("\'", "\\u0027"))
            .replace("topn", str(topn))
            .replace("thresholdplaceholder", str(threshold))
            .replace("linksplaceholder", json.dumps(interlinks).replace("\'", "\\u0027"))
            .replace("linkstrokewidth", str(edge))
    )