from mfr.core import RenderResult, get_file_extension
from mako.lookup import TemplateLookup
import json
from .configuration import config
import os

template = TemplateLookup(
    directories=['mfr_tabular/templates']
).get_template('tabular.mako')


CSS_FILES = [
    "slick.grid.css",
    "jquery-ui-1.8.16.custom.css",
    "examples.css",
    "slick-default-theme.css",
]

JS_FILES = [
    "jquery-1.7.min.js",
    "jquery.event.drag-2.2.js",
    "slick.core.js",
    "slick.grid.js",
]


def render_html(fp, src=None):
    """Render a tabular file to html
    :param fp: file pointer object
    :param src:
    :return: RenderResult object containing html and assets
    """

    try:
        columns, rows = populate_data(fp)
    except TypeError:
        return RenderResult("A matching renderer was not found or render "
                            + "requirements are not met")

    max_size = config.get('max_size')

    if len(columns) > max_size or len(rows) > max_size:
        return RenderResult("Table is too large")

    if len(columns) < 1 or len(rows) < 1:
        return RenderResult("Table is empty")

    content = template.render(
        columns=json.dumps(columns),
        rows=json.dumps(rows),
        # TODO(asmacdo) make this a title?
        writing="",
        # TODO(asmacdo) investigate
        STATIC_PATH="/mfr/mfr_tabular",
    )

    assets = {
        'css': find_assets('css'),
        'js': find_assets('js')
    }

    return RenderResult(content=content, assets=assets)


def find_assets(asset_type):
    """Create dictionary of js and css assets"""

    static_dir = "/static/mfr/mfr_tabular"
    files = os.listdir("mfr_tabular/static/{asset}".format(asset=asset_type))

    return ['{0}/{1}/{2}'.format(static_dir, asset_type, filename)
            for filename in files]


# TODO(asmacdo) better way of choosing the renderer
def populate_data(fp):
    """Determine the appropriate library and use it to populate rows and columns
    :param fp: file pointer
    :return: tuple of column headers and row data
    """

    ext = get_file_extension(fp.name)
    function_preference = config['tabular_libraries'].get(ext)

    for function in function_preference:
        try:
            imported = function()
            print("Trying " + imported.__name__)
            return imported(fp)
        except ImportError:
            print("Failed to import " + function.__name__)

@@ -249,249 +249,8 @@
 )%0A%0A%0A
-CSS_FILES = %5B%0A    %22slick.grid.css%22,%0A    %22jquery-ui-1.8.16.custom.css%22,%0A    %22examples.css%22,%0A    %22slick-default-theme.css%22,%0A%5D%0A%0AJS_FILES = %5B%0A    %22jquery-1.7.min.js%22,%0A    %22jquery.event.drag-2.2.js%22,%0A    %22slick.core.js%22,%0A    %22slick.grid.js%22,%0A%5D%0A%0A%0A
 def 

remove extra constants